Skip to content

Commit

Permalink
fix: Unmarshal error on meroxa api --json (#171)
Browse files Browse the repository at this point in the history
* Revert "Allow output JSON only for `meroxa api` (#169)"

This reverts commit 7540b0c.

* fix(api): Unmarhal arrays with —json

Co-authored-by: Lovro Mažgon <[email protected]>

Co-authored-by: Lovro Mažgon <[email protected]>
  • Loading branch information
raulb and lovromazgon authored Aug 2, 2021
1 parent d977ca0 commit bf7673b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
20 changes: 5 additions & 15 deletions cmd/meroxa/root/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ type API struct {
Path string
Body string
}

flags struct {
JSON bool `long:"json" short:"" usage:"output json"`
}
}

func (a *API) Usage() string {
Expand Down Expand Up @@ -112,20 +108,14 @@ func (a *API) Execute(ctx context.Context) error {
prettyJSON.Write(b)
}

// Print headers unless JSON only
if !a.flags.JSON {
a.logger.Infof(ctx, "> %s %s", a.args.Method, a.args.Path)
a.logger.Infof(ctx, "< %s %s", resp.Status, resp.Proto)
for k, v := range resp.Header {
a.logger.Infof(ctx, "< %s %s", k, strings.Join(v, " "))
}
a.logger.Infof(ctx, "> %s %s", a.args.Method, a.args.Path)
a.logger.Infof(ctx, "< %s %s", resp.Status, resp.Proto)
for k, v := range resp.Header {
a.logger.Infof(ctx, "< %s %s", k, strings.Join(v, " "))
}

a.logger.Info(ctx, prettyJSON.String())
a.logger.JSON(ctx, prettyJSON.String())

return nil
}

func (a *API) Flags() []builder.Flag {
return builder.BuildFlags(&a.flags)
}
5 changes: 5 additions & 0 deletions log/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type jsonLogger struct {
}

func (l *jsonLogger) JSON(ctx context.Context, data interface{}) {
if raw, ok := data.(string); ok {
l.l.Print(raw)
return
}

p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
l.l.Printf("could not marshal JSON: %s", err.Error())
Expand Down

0 comments on commit bf7673b

Please sign in to comment.