Skip to content

Commit fc115ba

Browse files
byashimovmweibel
authored andcommitted
fix: panics on nil pointer
1 parent 44f66ae commit fc115ba

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

sheriff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Marshaller interface {
5656
// In all other cases we can't derive the type in a meaningful way and is therefore an `interface{}`.
5757
func Marshal(options *Options, data interface{}) (interface{}, error) {
5858
v := reflect.ValueOf(data)
59-
if !v.IsValid() {
59+
if !v.IsValid() || v.Kind() == reflect.Ptr && v.IsNil() {
6060
return data, nil
6161
}
6262
t := v.Type()

sheriff_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,10 @@ func TestMarshal_NilSlice(t *testing.T) {
747747

748748
assert.Equal(t, expect, string(jsonResult))
749749
}
750+
751+
func TestMarshal_NilPointer(t *testing.T) {
752+
var a *AModel
753+
v, err := Marshal(&Options{}, a)
754+
assert.Nil(t, v)
755+
assert.NoError(t, err)
756+
}

0 commit comments

Comments
 (0)