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
diff --git a/query/encode.go b/query/encode.go
index 1642207..9f2a8ec 100644
--- a/query/encode.go
+++ b/query/encode.go
@@ -209,6 +209,11 @@
}
if sv.Kind() == reflect.Slice || sv.Kind() == reflect.Array {
+ if sv.Len() == 0 {
+ // skip if slice or array is empty
+ continue
+ }
+
var del string
if opts.Contains("comma") {
del = ","
diff --git a/query/encode_test.go b/query/encode_test.go
index 8487858..a94b44d 100644
--- a/query/encode_test.go
+++ b/query/encode_test.go
@@ -144,12 +144,32 @@
url.Values{},
},
{
+ struct{ V []string }{[]string{}},
+ url.Values{},
+ },
+ {
+ struct{ V []string }{[]string{""}},
+ url.Values{"V": {""}},
+ },
+ {
struct{ V []string }{[]string{"a", "b"}},
url.Values{"V": {"a", "b"}},
},
{
struct {
V []string `url:",comma"`
+ }{[]string{}},
+ url.Values{},
+ },
+ {
+ struct {
+ V []string `url:",comma"`
+ }{[]string{""}},
+ url.Values{"V": {""}},
+ },
+ {
+ struct {
+ V []string `url:",comma"`
}{[]string{"a", "b"}},
url.Values{"V": {"a,b"}},
},