Skip to content

Commit 040b323

Browse files
xuancanhCanh Nguyen
and
Canh Nguyen
authored
fix: Handle empty interface when marshalling (#26)
Co-authored-by: Canh Nguyen <[email protected]>
1 parent b3555cb commit 040b323

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

sheriff.go

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type Marshaller interface {
5252
// In all other cases we can't derive the type in a meaningful way and is therefore an `interface{}`.
5353
func Marshal(options *Options, data interface{}) (interface{}, error) {
5454
v := reflect.ValueOf(data)
55+
if !v.IsValid() {
56+
return data, nil
57+
}
5558
t := v.Type()
5659

5760
// Initialise nestedGroupsMap,

sheriff_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -663,3 +663,15 @@ func TestMarshal_AliaString(t *testing.T) {
663663
_, err := Marshal(&Options{}, &v)
664664
assert.NoError(t, err)
665665
}
666+
667+
type EmptyInterfaceStruct struct {
668+
Data interface{} `json:"data"`
669+
}
670+
671+
func TestMarshal_EmptyInterface(t *testing.T) {
672+
v := EmptyInterfaceStruct{}
673+
o := &Options{}
674+
675+
_, err := Marshal(o, v)
676+
assert.NoError(t, err)
677+
}

0 commit comments

Comments
 (0)