Permalink
Browse files

alter BuildQuery to handle slices

  • Loading branch information...
1 parent 12c04a4 commit e43f3de04cbba5434ae5445eb7190a3839c514bb @jrperritt jrperritt committed Feb 12, 2015
Showing with 26 additions and 15 deletions.
  1. +6 −2 params.go
  2. +20 −13 params_test.go
View
@@ -145,8 +145,12 @@ func BuildQueryString(opts interface{}) (*url.URL, error) {
case reflect.Bool:
params.Add(tags[0], strconv.FormatBool(v.Bool()))
case reflect.Slice:
- switch f.Type {
- case reflect.TypeOf([]string{}):
+ switch v.Type().Elem() {
+ case reflect.TypeOf(0):
+ for i := 0; i < v.Len(); i++ {
+ params.Add(tags[0], strconv.FormatInt(v.Index(i).Int(), 10))
+ }
+ default:
for i := 0; i < v.Len(); i++ {
params.Add(tags[0], v.Index(i).String())
}
View
@@ -34,29 +34,36 @@ func TestMaybeInt(t *testing.T) {
}
func TestBuildQueryString(t *testing.T) {
+ type testVar string
opts := struct {
- J int `q:"j"`
- R string `q:"r,required"`
- C bool `q:"c"`
- S []string `q:"s"`
+ J int `q:"j"`
+ R string `q:"r,required"`
+ C bool `q:"c"`
+ S []string `q:"s"`
+ TS []testVar `q:"ts"`
+ TI []int `q:"ti"`
}{
- J: 2,
- R: "red",
- C: true,
- S: []string{"one", "two", "three"},
+ J: 2,
+ R: "red",
+ C: true,
+ S: []string{"one", "two", "three"},
+ TS: []testVar{"a", "b"},
+ TI: []int{1, 2},
}
- expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three"}
+ expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three&ti=1&ti=2&ts=a&ts=b"}
actual, err := BuildQueryString(&opts)
if err != nil {
t.Errorf("Error building query string: %v", err)
}
th.CheckDeepEquals(t, expected, actual)
opts = struct {
- J int `q:"j"`
- R string `q:"r,required"`
- C bool `q:"c"`
- S []string `q:"s"`
+ J int `q:"j"`
+ R string `q:"r,required"`
+ C bool `q:"c"`
+ S []string `q:"s"`
+ TS []testVar `q:"ts"`
+ TI []int `q:"ti"`
}{
J: 2,
C: true,

0 comments on commit e43f3de

Please sign in to comment.