Skip to content
Open
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
37 changes: 23 additions & 14 deletions query/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
63 changes: 63 additions & 0 deletions query/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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},
Expand Down