add logic and unit test for []string type in query params
diff --git a/params.go b/params.go
index 948783b..509a7c7 100644
--- a/params.go
+++ b/params.go
@@ -144,6 +144,13 @@
params.Add(tags[0], strconv.FormatInt(v.Int(), 10))
case reflect.Bool:
params.Add(tags[0], strconv.FormatBool(v.Bool()))
+ case reflect.Slice:
+ switch f.Type {
+ case reflect.TypeOf([]string{}):
+ for i := 0; i < v.Len(); i++ {
+ params.Add(tags[0], v.Index(i).String())
+ }
+ }
}
} else {
// Otherwise, the field is not set.
diff --git a/params_test.go b/params_test.go
index 4a2c9fe..f314a9e 100644
--- a/params_test.go
+++ b/params_test.go
@@ -35,15 +35,17 @@
func TestBuildQueryString(t *testing.T) {
opts := struct {
- J int `q:"j"`
- R string `q:"r,required"`
- C bool `q:"c"`
+ J int `q:"j"`
+ R string `q:"r,required"`
+ C bool `q:"c"`
+ S []string `q:"s"`
}{
J: 2,
R: "red",
C: true,
+ S: []string{"one", "two", "three"},
}
- expected := &url.URL{RawQuery: "c=true&j=2&r=red"}
+ expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three"}
actual, err := BuildQueryString(&opts)
if err != nil {
t.Errorf("Error building query string: %v", err)
@@ -51,9 +53,10 @@
th.CheckDeepEquals(t, expected, actual)
opts = struct {
- J int `q:"j"`
- R string `q:"r,required"`
- C bool `q:"c"`
+ J int `q:"j"`
+ R string `q:"r,required"`
+ C bool `q:"c"`
+ S []string `q:"s"`
}{
J: 2,
C: true,