Skip to content

Commit

Permalink
fix flatten json assign with error
Browse files Browse the repository at this point in the history
  • Loading branch information
amyangfei committed Jan 1, 2019
1 parent ee9ff40 commit e357bb3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions plugins/parser/json/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ func flatten(top bool, flatMap map[string]interface{}, nested interface{}, prefi
case map[string]interface{}:
for k, v := range nested.(map[string]interface{}) {
newKey := enkey(top, prefix, k, separator)
assign(newKey, v)
err := assign(newKey, v)
if err != nil {
return errors.Trace(err)
}
}
case []interface{}:
for i, v := range nested.([]interface{}) {
newKey := enkey(top, prefix, strconv.Itoa(i), separator)
assign(newKey, v)
err := assign(newKey, v)
if err != nil {
return errors.Trace(err)
}
}
default:
return ErrorInvalidInput
Expand Down
3 changes: 2 additions & 1 deletion plugins/parser/json/flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func TestFlattenError(t *testing.T) {
_, err = FlattenString(`{"a": "b"}`, prefix, separator)
assert.Equal(t, ErrorInvalidInput, errors.Cause(err))

gofail.Enable("github.com/amyangfei/go-logster/plugins/parser/json/FlattenError1", `return(false)`)
gofail.Enable("github.com/amyangfei/go-logster/plugins/parser/json/FlattenError2", `return(true)`)
_, err = FlattenString(`{"a": "b"}`, prefix, separator)
_, err = Flatten(map[string]interface{}{"a": []interface{}{1, 2}}, prefix, separator)
assert.Equal(t, ErrorInvalidInput, errors.Cause(err))
}

0 comments on commit e357bb3

Please sign in to comment.