Skip to content

Commit 5afcd79

Browse files
committed
allow specifying param name in "url" field tag
1 parent 19ef3dc commit 5afcd79

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

query/encode.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ func Values(v interface{}) (url.Values, error) {
1919
typ := val.Type()
2020
for i := 0; i < typ.NumField(); i++ {
2121
sf := typ.Field(i)
22-
sv := val.Field(i)
2322

24-
values.Add(sf.Name, fmt.Sprint(sv.Interface()))
23+
name := sf.Tag.Get("url")
24+
if name == "" {
25+
name = sf.Name
26+
}
27+
28+
sv := val.Field(i)
29+
values.Add(name, fmt.Sprint(sv.Interface()))
2530
}
2631

2732
return values, nil

query/encode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestValues(t *testing.T) {
1111
s := struct {
12-
A string
12+
A string `url:"a"`
1313
B int
1414
}{"abc", 1}
1515
v, err := Values(s)
@@ -18,7 +18,7 @@ func TestValues(t *testing.T) {
1818
}
1919

2020
want := url.Values{
21-
"A": {"abc"},
21+
"a": {"abc"},
2222
"B": {"1"},
2323
}
2424
if !reflect.DeepEqual(want, v) {

0 commit comments

Comments
 (0)