Skip to content

Commit

Permalink
fix omitempty panic for uncomparable fields (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxfv authored Jul 8, 2021
1 parent 1a71822 commit 5e42369
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,13 +1534,18 @@ func TestEncoderEmbedModes(t *testing.T) {

func TestOmitEmpty(t *testing.T) {

type NotComparable struct {
Slice []string
}

type Test struct {
String string `form:",omitempty"`
Array []string `form:",omitempty"`
Map map[string]string `form:",omitempty"`
String2 string `form:"str,omitempty"`
Array2 []string `form:"arr,omitempty"`
Map2 map[string]string `form:"map,omitempty"`
NotComparable `form:",omitempty"`
}

var tst Test
Expand Down
8 changes: 7 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func hasValue(field reflect.Value) bool {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil()
default:
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
if !field.IsValid() {
return false
}
if !field.Type().Comparable() {
return true
}
return field.Interface() != reflect.Zero(field.Type()).Interface()
}
}

0 comments on commit 5e42369

Please sign in to comment.