diff --git a/sheriff.go b/sheriff.go index 9885342..022a8d7 100644 --- a/sheriff.go +++ b/sheriff.go @@ -7,7 +7,7 @@ import ( "reflect" "strings" - version "github.com/hashicorp/go-version" + "github.com/hashicorp/go-version" ) // Options determine which struct fields are being added to the output map. @@ -217,6 +217,13 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) { } k := v.Kind() + switch k { + case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: + if v.IsNil() { + return val, nil + } + } + if k == reflect.Ptr { v = v.Elem() val = v.Interface() diff --git a/sheriff_test.go b/sheriff_test.go index 968e9d8..7b452fd 100644 --- a/sheriff_test.go +++ b/sheriff_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - version "github.com/hashicorp/go-version" + "github.com/hashicorp/go-version" "github.com/stretchr/testify/assert" ) @@ -713,3 +713,23 @@ func TestMarshal_EmptyInterface(t *testing.T) { _, err := Marshal(o, v) assert.NoError(t, err) } + +func TestMarshal_BooleanPtrMap(t *testing.T) { + tru := true + + toMarshal := map[string]*bool{ + "example": &tru, + "another": nil, + } + + marshalMap, err := Marshal(&Options{}, toMarshal) + assert.NoError(t, err) + + marshal, err := json.Marshal(marshalMap) + assert.NoError(t, err) + + expect, err := json.Marshal(toMarshal) + assert.NoError(t, err) + + assert.Equal(t, string(marshal), string(expect)) +}