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: Marshal panics on nil pointer #46

Merged
merged 1 commit into from
Mar 6, 2024
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 @@ -56,7 +56,7 @@ type Marshaller interface {
// In all other cases we can't derive the type in a meaningful way and is therefore an `interface{}`.
func Marshal(options *Options, data interface{}) (interface{}, error) {
v := reflect.ValueOf(data)
if !v.IsValid() {
if !v.IsValid() || v.Kind() == reflect.Ptr && v.IsNil() {
return data, nil
}
t := v.Type()
Expand Down
7 changes: 7 additions & 0 deletions sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,10 @@ func TestMarshal_NilSlice(t *testing.T) {

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

func TestMarshal_NilPointer(t *testing.T) {
var a *AModel
v, err := Marshal(&Options{}, a)
assert.Nil(t, v)
assert.NoError(t, err)
}