Skip to content

Commit 934da17

Browse files
committed
better handling of empty values in slices/arrays
if the slice or array has no values, skip it entirely. This fixes an issue where a struct field with a custom delimiter was included in the returned url.Values with an empty string rather than being omitted entirely (as is the case when there is no custom delimiter) fixes #57
1 parent b6e79f1 commit 934da17

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

query/encode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
209209
}
210210

211211
if sv.Kind() == reflect.Slice || sv.Kind() == reflect.Array {
212+
if sv.Len() == 0 {
213+
// skip if slice or array is empty
214+
continue
215+
}
216+
212217
var del string
213218
if opts.Contains("comma") {
214219
del = ","

query/encode_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,30 @@ func TestValues_Slices(t *testing.T) {
143143
struct{ V []string }{},
144144
url.Values{},
145145
},
146+
{
147+
struct{ V []string }{[]string{}},
148+
url.Values{},
149+
},
150+
{
151+
struct{ V []string }{[]string{""}},
152+
url.Values{"V": {""}},
153+
},
146154
{
147155
struct{ V []string }{[]string{"a", "b"}},
148156
url.Values{"V": {"a", "b"}},
149157
},
158+
{
159+
struct {
160+
V []string `url:",comma"`
161+
}{[]string{}},
162+
url.Values{},
163+
},
164+
{
165+
struct {
166+
V []string `url:",comma"`
167+
}{[]string{""}},
168+
url.Values{"V": {""}},
169+
},
150170
{
151171
struct {
152172
V []string `url:",comma"`

0 commit comments

Comments
 (0)