Skip to content

Commit

Permalink
Merge pull request #1 from neilgarb/stringer
Browse files Browse the repository at this point in the history
Support `fmt.Stringer`s in makeURLValues()
  • Loading branch information
neilgarb authored May 19, 2018
2 parents 250dc95 + 32fe992 commit 34a211c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 15 additions & 10 deletions util_internal_test.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -26,14 +30,15 @@ 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)
if err != nil {
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)
Expand Down

0 comments on commit 34a211c

Please sign in to comment.