Skip to content

Commit

Permalink
omitempty: don't call value receiver IsZero for nil ptrs
Browse files Browse the repository at this point in the history
fixes #101
  • Loading branch information
guregu committed Jul 18, 2019
1 parent 305af82 commit 019aa7b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ func isZero(rv reflect.Value) bool {
// use IsZero for supported types
if rv.CanInterface() {
if zeroer, ok := rv.Interface().(isZeroer); ok {
if rv.Kind() == reflect.Ptr && rv.IsNil() {
if _, cantCall := rv.Type().Elem().MethodByName("IsZero"); cantCall {
// can't call a value method on a nil pointer type
return true
}
}
return zeroer.IsZero()
}
}
Expand Down
6 changes: 4 additions & 2 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ var itemEncodingTests = []struct {
{
name: "omitempty",
in: struct {
A bool `dynamo:",omitempty"`
Other bool
A bool `dynamo:",omitempty"`
B *bool `dynamo:",omitempty"`
NilTime *time.Time `dynamo:",omitempty"`
Other bool
}{
Other: true,
},
Expand Down

0 comments on commit 019aa7b

Please sign in to comment.