Skip to content

Commit

Permalink
New JSON utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleu committed Aug 19, 2023
1 parent a8336b3 commit 4aae668
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 1 addition & 2 deletions app/controller/cutil/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func ParseFormAsChanges(rc *fasthttp.RequestCtx) (util.ValueMap, error) {
}

func parseJSONForm(rc *fasthttp.RequestCtx) (util.ValueMap, error) {
var ret any
err := util.FromJSON(rc.Request.Body(), &ret)
ret, err := util.FromJSONInterface(rc.Request.Body())
if err != nil {
return nil, errors.Wrap(err, "can't parse JSON body")
}
Expand Down
3 changes: 1 addition & 2 deletions app/lib/log/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func (e *customEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field)
out := b.Bytes()
b.Free()

data := util.ValueMap{}
err = util.FromJSON(out, &data)
data, err := util.FromJSONMap(out)
if err != nil {
return nil, errors.Wrap(err, "can't parse logging JSON")
}
Expand Down
12 changes: 12 additions & 0 deletions app/util/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ func FromJSON(msg json.RawMessage, tgt any) error {
return jsoniterParser.Unmarshal(msg, tgt)
}

func FromJSONString(msg json.RawMessage) (string, error) {
var tgt string
err := jsoniterParser.Unmarshal(msg, &tgt)
return tgt, err
}

func FromJSONMap(msg json.RawMessage) (ValueMap, error) {
var tgt ValueMap
err := jsoniterParser.Unmarshal(msg, &tgt)
return tgt, err
}

func FromJSONInterface(msg json.RawMessage) (any, error) {
var tgt any
err := FromJSON(msg, &tgt)
Expand Down

0 comments on commit 4aae668

Please sign in to comment.