Skip to content

Commit

Permalink
fix: Handle empty interface when marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
Canh Nguyen committed Sep 30, 2020
1 parent b3555cb commit d078d0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type Marshaller interface {
// In all other cases we can't derive the type in a meaningful way and is therefore an `interface{}`.
func Marshal(options *Options, data interface{}) (interface{}, error) {
v := reflect.ValueOf(data)
if !v.IsValid() {
return data, nil
}
t := v.Type()

// Initialise nestedGroupsMap,
Expand Down
12 changes: 12 additions & 0 deletions sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,15 @@ func TestMarshal_AliaString(t *testing.T) {
_, err := Marshal(&Options{}, &v)
assert.NoError(t, err)
}

type EmptyInterfaceStruct struct {
Data interface{} `json:"data"`
}

func TestMarshal_EmptyInterface(t *testing.T) {
v := EmptyInterfaceStruct{}
o := &Options{}

_, err := Marshal(o, v)
assert.NoError(t, err)
}

0 comments on commit d078d0d

Please sign in to comment.