Update how isZero handles Pointers (#181)
* Update how isZero handles Pointers
This commit modifies the isZero function so it checks to see if
a pointer is actually set rather than if it points to a zero value.
* Adding a loop to BuildQueryString to efficiently determine parameter type
* Moving reflect.Ptr to top of BuildQueryString loop
diff --git a/params.go b/params.go
index b7f9508..e484fe1 100644
--- a/params.go
+++ b/params.go
@@ -232,7 +232,7 @@
if v.IsNil() {
return true
}
- return isZero(v.Elem())
+ return false
case reflect.Func, reflect.Map, reflect.Slice:
return v.IsNil()
case reflect.Array:
@@ -307,7 +307,11 @@
// if the field is set, add it to the slice of query pieces
if !isZero(v) {
+ loop:
switch v.Kind() {
+ case reflect.Ptr:
+ v = v.Elem()
+ goto loop
case reflect.String:
params.Add(tags[0], v.String())
case reflect.Int:
diff --git a/testing/params_test.go b/testing/params_test.go
index 90f3fad..937ff8b 100644
--- a/testing/params_test.go
+++ b/testing/params_test.go
@@ -35,6 +35,7 @@
func TestBuildQueryString(t *testing.T) {
type testVar string
+ iFalse := false
opts := struct {
J int `q:"j"`
R string `q:"r,required"`
@@ -42,6 +43,7 @@
S []string `q:"s"`
TS []testVar `q:"ts"`
TI []int `q:"ti"`
+ F *bool `q:"f"`
}{
J: 2,
R: "red",
@@ -49,8 +51,9 @@
S: []string{"one", "two", "three"},
TS: []testVar{"a", "b"},
TI: []int{1, 2},
+ F: &iFalse,
}
- expected := &url.URL{RawQuery: "c=true&j=2&r=red&s=one&s=two&s=three&ti=1&ti=2&ts=a&ts=b"}
+ expected := &url.URL{RawQuery: "c=true&f=false&j=2&r=red&s=one&s=two&s=three&ti=1&ti=2&ts=a&ts=b"}
actual, err := gophercloud.BuildQueryString(&opts)
if err != nil {
t.Errorf("Error building query string: %v", err)
@@ -64,6 +67,7 @@
S []string `q:"s"`
TS []testVar `q:"ts"`
TI []int `q:"ti"`
+ F *bool `q:"f"`
}{
J: 2,
C: true,