From e635de9360de039a646a65e79f9056695dde515a Mon Sep 17 00:00:00 2001 From: Sami Chergui Date: Thu, 19 Nov 2026 17:12:20 +0100 Subject: [PATCH] query: check IsZero before kind switch in isEmptyValue (#202) Signed-off-by: Sami Chergui <83331159+Yshuro@users.noreply.github.com> --- query/encode.go | 37 ++++++++++++++++---------- query/encode_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/query/encode.go b/query/encode.go index ec5c0ee..7b82449 100644 --- a/query/encode.go +++ b/query/encode.go @@ -319,6 +319,29 @@ func valueString(v reflect.Value, opts tagOptions, sf reflect.StructField) strin // isEmptyValue checks if a value should be considered empty for the purposes // of omitting fields with the "omitempty" option. func isEmptyValue(v reflect.Value) bool { + if !v.IsValid() { + return true + } + + if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { + return v.IsNil() + } + + type zeroable interface { + IsZero() bool + } + + if v.CanInterface() { + if z, ok := v.Interface().(zeroable); ok { + return z.IsZero() + } + } + if v.CanAddr() { + if z, ok := v.Addr().Interface().(zeroable); ok { + return z.IsZero() + } + } + switch v.Kind() { case reflect.Array, reflect.Map, reflect.Slice, reflect.String: return v.Len() == 0 @@ -330,20 +353,6 @@ func isEmptyValue(v reflect.Value) bool { return v.Uint() == 0 case reflect.Float32, reflect.Float64: return v.Float() == 0 - case reflect.Interface, reflect.Ptr: - return v.IsNil() - } - - if !v.CanInterface() { - return false - } - - type zeroable interface { - IsZero() bool - } - - if z, ok := v.Interface().(zeroable); ok { - return z.IsZero() } return false diff --git a/query/encode_test.go b/query/encode_test.go index e65afa9..bd8f772 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -329,6 +329,18 @@ func TestValues_NestedTypes(t *testing.T) { } } +type customLimit int + +func (l customLimit) IsZero() bool { return l == -1 } + +type customString string + +func (s customString) IsZero() bool { return s == "unset" } + +type customSlice []string + +func (s customSlice) IsZero() bool { return len(s) == 1 && s[0] == "empty" } + func TestValues_OmitEmpty(t *testing.T) { str := "" @@ -362,6 +374,48 @@ func TestValues_OmitEmpty(t *testing.T) { }{&str}, url.Values{"V": {""}}, }, + { + struct { + Limit customLimit `url:"limit,omitempty"` + }{customLimit(-1)}, + url.Values{}, + }, + { + struct { + Limit customLimit `url:"limit,omitempty"` + }{customLimit(0)}, + url.Values{"limit": {"0"}}, + }, + { + struct { + Limit customLimit `url:"limit,omitempty"` + }{customLimit(10)}, + url.Values{"limit": {"10"}}, + }, + { + struct { + Str customString `url:"str,omitempty"` + }{customString("unset")}, + url.Values{}, + }, + { + struct { + Str customString `url:"str,omitempty"` + }{customString("hello")}, + url.Values{"str": {"hello"}}, + }, + { + struct { + Slice customSlice `url:"slice,omitempty"` + }{customSlice{"empty"}}, + url.Values{}, + }, + { + struct { + Slice customSlice `url:"slice,omitempty"` + }{customSlice{"a", "b"}}, + url.Values{"slice": {"a", "b"}}, + }, } for _, tt := range tests { @@ -768,6 +822,15 @@ func TestIsEmptyValue(t *testing.T) { {time.Time{}, true}, {time.Now(), false}, + // custom types with IsZero() method + {customLimit(-1), true}, + {customLimit(0), false}, + {customLimit(10), false}, + {customString("unset"), true}, + {customString("hello"), false}, + {customSlice{"empty"}, true}, + {customSlice{"a"}, false}, + // unknown type - always false unless a nil pointer, which are always empty. {(*struct{ int })(nil), true}, {struct{ int }{}, false},