Skip to content

Commit

Permalink
Improve JSON output
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Sep 19, 2023
1 parent ebc117d commit 08413c7
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 21 deletions.
7 changes: 4 additions & 3 deletions go/cmd/vtctldclient/cli/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/vreplication/common/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/vreplication/common/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/vreplication/common/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/vreplication/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions go/cmd/vtctldclient/command/vreplication/vdiff/vdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/vreplication/workflow/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/vtctldclient/command/vreplication/workflow/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/vreplication/workflow/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 08413c7

Please sign in to comment.