Skip to content

Commit cd01545

Browse files
simaotwxmweibel
authored andcommitted
Fix slice regression caused by #32
Slices that are nil are empty and valid. The change introduced by #32 which fixes #31 has changed how nil slices are marshaled (`null` instead of `[]`). Fix this regression by removing slices from the check.
1 parent a143a78 commit cd01545

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

sheriff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) {
218218
k := v.Kind()
219219

220220
switch k {
221-
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
221+
case reflect.Interface, reflect.Map, reflect.Ptr:
222222
if v.IsNil() {
223223
return val, nil
224224
}

sheriff_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ func TestMarshal_ArrayOfInterfaceable(t *testing.T) {
630630
type TestInlineStruct struct {
631631
// explicitly testing unexported fields
632632
// golangci-lint complains about it and that's ok to ignore.
633-
tableName struct{ Test string } `json:"-"` //nolint
634-
tableNameWithTag struct{ Test string } `json:"foo"` //nolint
633+
tableName struct{ Test string } `json:"-"` //nolint
634+
tableNameWithTag struct{ Test string } `json:"foo"` //nolint
635635

636636
Field string `json:"field"`
637637
Field2 *string `json:"field2"`
@@ -733,3 +733,17 @@ func TestMarshal_BooleanPtrMap(t *testing.T) {
733733

734734
assert.Equal(t, string(marshal), string(expect))
735735
}
736+
737+
func TestMarshal_NilSlice(t *testing.T) {
738+
var stringSlice []string // nil slice
739+
740+
marshalSlice, err := Marshal(&Options{}, stringSlice)
741+
assert.NoError(t, err)
742+
743+
jsonResult, err := json.Marshal(marshalSlice)
744+
assert.NoError(t, err)
745+
746+
expect := "[]"
747+
748+
assert.Equal(t, expect, string(jsonResult))
749+
}

0 commit comments

Comments
 (0)