Skip to content

Commit

Permalink
fix: empty map to empty json (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: mrekhi <mandeep.rekhi@nokia.com>
Co-authored-by: Michael Weibel <307427+mweibel@users.noreply.github.com>
  • Loading branch information
3 people committed May 9, 2023
1 parent 5dbdcfe commit 7d7ab59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) {
if k == reflect.Map {
mapKeys := v.MapKeys()
if len(mapKeys) == 0 {
return nil, nil
return val, nil
}
if mapKeys[0].Kind() != reflect.String {
return nil, MarshalInvalidTypeError{t: mapKeys[0].Kind(), data: val}
Expand Down
22 changes: 21 additions & 1 deletion sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,33 @@ func TestMarshal_EmptyMap(t *testing.T) {
assert.NoError(t, err)

expected, err := json.Marshal(map[string]interface{}{
"a_map": nil,
"a_map": make(map[string]interface{}),
})
assert.NoError(t, err)

assert.Equal(t, string(expected), string(actual))
}

func TestMarshal_EmptyMapJson(t *testing.T) {
emp := EmptyMapTest{
AMap: make(map[string]string),
}
o := &Options{
Groups: []string{"test"},
}

actualMap, err := Marshal(o, emp)
assert.NoError(t, err)

actual, err := json.Marshal(actualMap)
assert.NoError(t, err)

expected, err := json.Marshal(emp)
assert.NoError(t, err)

assert.Equal(t, string(expected), string(actual))
}

type TestMarshal_Embedded struct {
Foo string `json:"foo" groups:"test"`
}
Expand Down

0 comments on commit 7d7ab59

Please sign in to comment.