Skip to content

Commit

Permalink
fix: Handle empty interface when marshalling (liip#26)
Browse files Browse the repository at this point in the history
Co-authored-by: Canh Nguyen <[email protected]>
  • Loading branch information
2 people authored and kevinlynx committed Jul 28, 2021
1 parent f959765 commit c435f42
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 c435f42

Please sign in to comment.