Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slice regression caused by #32 #34

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) {
k := v.Kind()

switch k {
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
case reflect.Interface, reflect.Map, reflect.Ptr:
if v.IsNil() {
return val, nil
}
Expand Down
18 changes: 16 additions & 2 deletions sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ func TestMarshal_ArrayOfInterfaceable(t *testing.T) {
type TestInlineStruct struct {
// explicitly testing unexported fields
// golangci-lint complains about it and that's ok to ignore.
tableName struct{ Test string } `json:"-"` //nolint
tableNameWithTag struct{ Test string } `json:"foo"` //nolint
tableName struct{ Test string } `json:"-"` //nolint
tableNameWithTag struct{ Test string } `json:"foo"` //nolint

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

assert.Equal(t, string(marshal), string(expect))
}

func TestMarshal_NilSlice(t *testing.T) {
var stringSlice []string // nil slice

marshalSlice, err := Marshal(&Options{}, stringSlice)
assert.NoError(t, err)

jsonResult, err := json.Marshal(marshalSlice)
assert.NoError(t, err)

expect := "[]"

assert.Equal(t, expect, string(jsonResult))
}