diff --git a/util.go b/util.go index 1f8616f..0c16095 100644 --- a/util.go +++ b/util.go @@ -23,6 +23,12 @@ func makeURLValues(v interface{}) (url.Values, error) { fieldValue := valElem.Field(i) + stringer, ok := fieldValue.Interface().(fmt.Stringer) + if ok { + values.Set(urlTag, stringer.String()) + continue + } + k := fieldValue.Kind() var s string switch k { diff --git a/util_internal_test.go b/util_internal_test.go index 639c604..171df5a 100644 --- a/util_internal_test.go +++ b/util_internal_test.go @@ -1,20 +1,24 @@ package luno -import "testing" +import ( + "testing" + "time" +) func TestMakeURLValues(t *testing.T) { type S string type Req struct { - S string `url:"s"` - I int `url:"i"` - I64 int64 `url:"i64"` - F32 float32 `url:"f32"` - F64 float64 `url:"f64"` - B bool `url:"b"` - ABy []byte `url:"aby"` - TS S `url:"ts"` + S string `url:"s"` + I int `url:"i"` + I64 int64 `url:"i64"` + F32 float32 `url:"f32"` + F64 float64 `url:"f64"` + B bool `url:"b"` + ABy []byte `url:"aby"` + TS S `url:"ts"` + T time.Time `url:"t"` // implements fmt.Stringer } r := Req{ @@ -26,6 +30,7 @@ func TestMakeURLValues(t *testing.T) { B: true, ABy: []byte("foo"), TS: S("foo"), + T: time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC), } v, err := makeURLValues(&r) @@ -33,7 +38,7 @@ func TestMakeURLValues(t *testing.T) { t.Errorf("Expected success, got %v", err) return } - exp := "aby=foo&b=true&f32=42.4200&f64=42.4200&i=42&i64=42&s=foo&ts=foo" + exp := "aby=foo&b=true&f32=42.4200&f64=42.4200&i=42&i64=42&s=foo&t=2018-01-01+00%3A00%3A00+%2B0000+UTC&ts=foo" act := v.Encode() if act != exp { t.Errorf("Expected %q, got %q", exp, act)