Skip to content

Commit 5e42369

Browse files
authored
fix omitempty panic for uncomparable fields (#57)
1 parent 1a71822 commit 5e42369

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

encoder_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,13 +1534,18 @@ func TestEncoderEmbedModes(t *testing.T) {
15341534

15351535
func TestOmitEmpty(t *testing.T) {
15361536

1537+
type NotComparable struct {
1538+
Slice []string
1539+
}
1540+
15371541
type Test struct {
15381542
String string `form:",omitempty"`
15391543
Array []string `form:",omitempty"`
15401544
Map map[string]string `form:",omitempty"`
15411545
String2 string `form:"str,omitempty"`
15421546
Array2 []string `form:"arr,omitempty"`
15431547
Map2 map[string]string `form:"map,omitempty"`
1548+
NotComparable `form:",omitempty"`
15441549
}
15451550

15461551
var tst Test

util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func hasValue(field reflect.Value) bool {
5151
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
5252
return !field.IsNil()
5353
default:
54-
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
54+
if !field.IsValid() {
55+
return false
56+
}
57+
if !field.Type().Comparable() {
58+
return true
59+
}
60+
return field.Interface() != reflect.Zero(field.Type()).Interface()
5561
}
5662
}

0 commit comments

Comments
 (0)