From 08413c715da9e6d1220384b9719f688cec7240ee Mon Sep 17 00:00:00 2001 From: Matt Lord Date: Mon, 18 Sep 2023 21:25:31 -0400 Subject: [PATCH] Improve JSON output Signed-off-by: Matt Lord --- go/cmd/vtctldclient/cli/json.go | 7 ++++--- .../command/vreplication/common/cancel.go | 2 +- .../command/vreplication/common/complete.go | 2 +- .../vtctldclient/command/vreplication/common/show.go | 2 +- .../command/vreplication/common/switchtraffic.go | 2 +- .../command/vreplication/common/update.go | 2 +- .../command/vreplication/common/utils.go | 2 +- .../vtctldclient/command/vreplication/vdiff/vdiff.go | 12 ++++++------ .../command/vreplication/workflow/delete.go | 2 +- .../command/vreplication/workflow/get.go | 2 +- .../command/vreplication/workflow/show.go | 4 ++-- .../command/vreplication/workflow/state.go | 2 +- .../command/vreplication/workflow/update.go | 2 +- 13 files changed, 22 insertions(+), 21 deletions(-) diff --git a/go/cmd/vtctldclient/cli/json.go b/go/cmd/vtctldclient/cli/json.go index d2c62e03b35..4d524302c17 100644 --- a/go/cmd/vtctldclient/cli/json.go +++ b/go/cmd/vtctldclient/cli/json.go @@ -60,14 +60,15 @@ func MarshalJSON(obj any) ([]byte, error) { } } -// MarshalJSONCompact works the same as MarshalJSON but elides zero value elements. -func MarshalJSONCompact(obj any) ([]byte, error) { +// MarshalJSONPretty works the same as MarshalJSON but elides zero value +// elements and uses ENUM names instead of numbers. +func MarshalJSONPretty(obj any) ([]byte, error) { switch obj := obj.(type) { case proto.Message: m := protojson.MarshalOptions{ Multiline: true, Indent: JSONindent, - UseEnumNumbers: true, + UseEnumNumbers: false, UseProtoNames: true, EmitUnpopulated: false, // elide zero value elements } diff --git a/go/cmd/vtctldclient/command/vreplication/common/cancel.go b/go/cmd/vtctldclient/command/vreplication/common/cancel.go index 9187d1ca4fa..5db27746eb5 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/cancel.go +++ b/go/cmd/vtctldclient/command/vreplication/common/cancel.go @@ -70,7 +70,7 @@ func commandCancel(cmd *cobra.Command, args []string) error { sort.Slice(resp.Details, func(i, j int) bool { return resp.Details[i].Tablet.String() < resp.Details[j].Tablet.String() }) - output, err = cli.MarshalJSONCompact(resp) + output, err = cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/common/complete.go b/go/cmd/vtctldclient/command/vreplication/common/complete.go index 027503d8781..b87c423b829 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/complete.go +++ b/go/cmd/vtctldclient/command/vreplication/common/complete.go @@ -53,7 +53,7 @@ func commandComplete(cmd *cobra.Command, args []string) error { var output []byte if format == "json" { - output, err = cli.MarshalJSONCompact(resp) + output, err = cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/common/show.go b/go/cmd/vtctldclient/command/vreplication/common/show.go index e650aa24509..eaf0b5af29e 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/show.go +++ b/go/cmd/vtctldclient/command/vreplication/common/show.go @@ -51,7 +51,7 @@ func commandShow(cmd *cobra.Command, args []string) error { return err } - data, err := cli.MarshalJSON(resp) + data, err := cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/common/switchtraffic.go b/go/cmd/vtctldclient/command/vreplication/common/switchtraffic.go index 0ba3ab595bb..019367fe82b 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/switchtraffic.go +++ b/go/cmd/vtctldclient/command/vreplication/common/switchtraffic.go @@ -106,7 +106,7 @@ func commandSwitchTraffic(cmd *cobra.Command, args []string) error { var output []byte if format == "json" { - output, err = cli.MarshalJSONCompact(resp) + output, err = cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/common/update.go b/go/cmd/vtctldclient/command/vreplication/common/update.go index 21fb6281f62..6beecb58ffa 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/update.go +++ b/go/cmd/vtctldclient/command/vreplication/common/update.go @@ -159,7 +159,7 @@ func commandUpdateState(cmd *cobra.Command, args []string) error { return resp.Details[i].Tablet.String() < resp.Details[j].Tablet.String() }) - data, err := cli.MarshalJSON(resp) + data, err := cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/common/utils.go b/go/cmd/vtctldclient/command/vreplication/common/utils.go index 6ac5d200cb5..61d27451cc4 100644 --- a/go/cmd/vtctldclient/command/vreplication/common/utils.go +++ b/go/cmd/vtctldclient/command/vreplication/common/utils.go @@ -150,7 +150,7 @@ func OutputStatusResponse(resp *vtctldatapb.WorkflowStatusResponse, format strin var output []byte var err error if format == "json" { - output, err = cli.MarshalJSON(resp) + output, err = cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff.go b/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff.go index 459bde6e1fe..92f3ce934c1 100644 --- a/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff.go +++ b/go/cmd/vtctldclient/command/vreplication/vdiff/vdiff.go @@ -291,7 +291,7 @@ func commandVDiffCreate(cmd *cobra.Command, args []string) error { } else { var data []byte if format == "json" { - data, err = cli.MarshalJSON(resp) + data, err = cli.MarshalJSONPretty(resp) if err != nil { return err } @@ -323,7 +323,7 @@ func commandVDiffDelete(cmd *cobra.Command, args []string) error { var data []byte if format == "json" { - data, err = cli.MarshalJSON(resp) + data, err = cli.MarshalJSONPretty(resp) if err != nil { return err } @@ -355,7 +355,7 @@ func commandVDiffResume(cmd *cobra.Command, args []string) error { var data []byte if format == "json" { - data, err = cli.MarshalJSON(resp) + data, err = cli.MarshalJSONPretty(resp) if err != nil { return err } @@ -507,7 +507,7 @@ func displayShowRecent(format, keyspace, workflowName, subCommand string, resp * return err } if format == "json" { - jsonText, err := json.MarshalIndent(recent, cli.JSONprefix, cli.JSONindent) + jsonText, err := cli.MarshalJSONPretty(recent) if err != nil { return err } @@ -553,7 +553,7 @@ func displayShowSingleSummary(format, keyspace, workflowName, uuid string, resp } state = summary.State if format == "json" { - jsonText, err := json.MarshalIndent(summary, cli.JSONprefix, cli.JSONindent) + jsonText, err := cli.MarshalJSONPretty(summary) if err != nil { return state, err } @@ -838,7 +838,7 @@ func commandVDiffStop(cmd *cobra.Command, args []string) error { var data []byte if format == "json" { - data, err = cli.MarshalJSON(resp) + data, err = cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/workflow/delete.go b/go/cmd/vtctldclient/command/vreplication/workflow/delete.go index 2707c8b7ba5..7713a5aa3e5 100644 --- a/go/cmd/vtctldclient/command/vreplication/workflow/delete.go +++ b/go/cmd/vtctldclient/command/vreplication/workflow/delete.go @@ -66,7 +66,7 @@ func commandWorkflowDelete(cmd *cobra.Command, args []string) error { return resp.Details[i].Tablet.String() < resp.Details[j].Tablet.String() }) - data, err := cli.MarshalJSON(resp) + data, err := cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/workflow/get.go b/go/cmd/vtctldclient/command/vreplication/workflow/get.go index 8dd8ba4eee1..95cc80ffbbb 100644 --- a/go/cmd/vtctldclient/command/vreplication/workflow/get.go +++ b/go/cmd/vtctldclient/command/vreplication/workflow/get.go @@ -55,7 +55,7 @@ func commandGetWorkflows(cmd *cobra.Command, args []string) error { return err } - data, err := cli.MarshalJSON(resp) + data, err := cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/workflow/show.go b/go/cmd/vtctldclient/command/vreplication/workflow/show.go index e85a582a20d..6f80e821559 100644 --- a/go/cmd/vtctldclient/command/vreplication/workflow/show.go +++ b/go/cmd/vtctldclient/command/vreplication/workflow/show.go @@ -71,9 +71,9 @@ func commandWorkflowShow(cmd *cobra.Command, args []string) error { for i, wf := range resp.Workflows { Names[i] = wf.Name } - data, err = cli.MarshalJSON(Names) + data, err = cli.MarshalJSONPretty(Names) } else { - data, err = cli.MarshalJSON(resp) + data, err = cli.MarshalJSONPretty(resp) } if err != nil { return err diff --git a/go/cmd/vtctldclient/command/vreplication/workflow/state.go b/go/cmd/vtctldclient/command/vreplication/workflow/state.go index bc304790a96..573c504d9d5 100644 --- a/go/cmd/vtctldclient/command/vreplication/workflow/state.go +++ b/go/cmd/vtctldclient/command/vreplication/workflow/state.go @@ -95,7 +95,7 @@ func commandWorkflowUpdateState(cmd *cobra.Command, args []string) error { return resp.Details[i].Tablet.String() < resp.Details[j].Tablet.String() }) - data, err := cli.MarshalJSON(resp) + data, err := cli.MarshalJSONPretty(resp) if err != nil { return err } diff --git a/go/cmd/vtctldclient/command/vreplication/workflow/update.go b/go/cmd/vtctldclient/command/vreplication/workflow/update.go index d76f6516d12..ca366b42bd8 100644 --- a/go/cmd/vtctldclient/command/vreplication/workflow/update.go +++ b/go/cmd/vtctldclient/command/vreplication/workflow/update.go @@ -115,7 +115,7 @@ func commandWorkflowUpdate(cmd *cobra.Command, args []string) error { return resp.Details[i].Tablet.String() < resp.Details[j].Tablet.String() }) - data, err := cli.MarshalJSON(resp) + data, err := cli.MarshalJSONPretty(resp) if err != nil { return err }