Skip to content

feat: support user use specify tag #94

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Options struct {
}

opt := Options{ "foo", true, 2 }
v, _ := query.Values(opt)
v, _ := query.Values("",opt)
fmt.Print(v.Encode()) // will output: "q=foo&all=true&page=2"
```

Expand Down
15 changes: 9 additions & 6 deletions query/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ type Encoder interface {
//
// Multiple fields that encode to the same URL parameter name will be included
// as multiple URL values of the same name.
func Values(v interface{}) (url.Values, error) {
func Values(userTag string, v interface{}) (url.Values, error) {
if userTag == "" {
userTag = "url" // default tag
}
values := make(url.Values)
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
Expand All @@ -141,14 +144,14 @@ func Values(v interface{}) (url.Values, error) {
return nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind())
}

err := reflectValue(values, val, "")
err := reflectValue(userTag, values, val, "")
return values, err
}

// reflectValue populates the values parameter from the struct fields in val.
// Embedded structs are followed recursively (using the rules defined in the
// Values function documentation) breadth-first.
func reflectValue(values url.Values, val reflect.Value, scope string) error {
func reflectValue(userTag string, values url.Values, val reflect.Value, scope string) error {
var embedded []reflect.Value

typ := val.Type()
Expand All @@ -159,7 +162,7 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
}

sv := val.Field(i)
tag := sf.Tag.Get("url")
tag := sf.Tag.Get(userTag)
if tag == "-" {
continue
}
Expand Down Expand Up @@ -257,7 +260,7 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
}

if sv.Kind() == reflect.Struct {
if err := reflectValue(values, sv, name); err != nil {
if err := reflectValue(userTag, values, sv, name); err != nil {
return err
}
continue
Expand All @@ -267,7 +270,7 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
}

for _, f := range embedded {
if err := reflectValue(values, f, scope); err != nil {
if err := reflectValue(userTag, values, f, scope); err != nil {
return err
}
}
Expand Down
54 changes: 31 additions & 23 deletions query/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

// test that Values(input) matches want. If not, report an error on t.
func testValue(t *testing.T, input interface{}, want url.Values) {
v, err := Values(input)
func testValue(t *testing.T, userTag string, input interface{}, want url.Values) {
v, err := Values(userTag, input)
if err != nil {
t.Errorf("Values(%q) returned error: %v", input, err)
}
Expand All @@ -28,31 +28,34 @@ func testValue(t *testing.T, input interface{}, want url.Values) {

func TestValues_BasicTypes(t *testing.T) {
tests := []struct {
myTag string
input interface{}
want url.Values
}{
// zero values
{struct{ V string }{}, url.Values{"V": {""}}},
{struct{ V int }{}, url.Values{"V": {"0"}}},
{struct{ V uint }{}, url.Values{"V": {"0"}}},
{struct{ V float32 }{}, url.Values{"V": {"0"}}},
{struct{ V bool }{}, url.Values{"V": {"false"}}},
{"userTag", struct{ V string }{}, url.Values{"V": {""}}},
{"userTag", struct{ V int }{}, url.Values{"V": {"0"}}},
{"userTag", struct{ V uint }{}, url.Values{"V": {"0"}}},
{"userTag", struct{ V float32 }{}, url.Values{"V": {"0"}}},
{"userTag", struct{ V bool }{}, url.Values{"V": {"false"}}},

// simple non-zero values
{struct{ V string }{"v"}, url.Values{"V": {"v"}}},
{struct{ V int }{1}, url.Values{"V": {"1"}}},
{struct{ V uint }{1}, url.Values{"V": {"1"}}},
{struct{ V float32 }{0.1}, url.Values{"V": {"0.1"}}},
{struct{ V bool }{true}, url.Values{"V": {"true"}}},
{"userTag", struct{ V string }{"v"}, url.Values{"V": {"v"}}},
{"userTag", struct{ V int }{1}, url.Values{"V": {"1"}}},
{"userTag", struct{ V uint }{1}, url.Values{"V": {"1"}}},
{"userTag", struct{ V float32 }{0.1}, url.Values{"V": {"0.1"}}},
{"userTag", struct{ V bool }{true}, url.Values{"V": {"true"}}},

// bool-specific options
{
"url",
struct {
V bool `url:",int"`
}{false},
url.Values{"V": {"0"}},
},
{
"url",
struct {
V bool `url:",int"`
}{true},
Expand All @@ -61,30 +64,35 @@ func TestValues_BasicTypes(t *testing.T) {

// time values
{
"",
struct {
V time.Time
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
url.Values{"V": {"2000-01-01T12:34:56Z"}},
},
{
"url",
struct {
V time.Time `url:",unix"`
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
url.Values{"V": {"946730096"}},
},
{
"url",
struct {
V time.Time `url:",unixmilli"`
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
url.Values{"V": {"946730096000"}},
},
{
"url",
struct {
V time.Time `url:",unixnano"`
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
url.Values{"V": {"946730096000000000"}},
},
{
"url",
struct {
V time.Time `layout:"2006-01-02"`
}{time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)},
Expand All @@ -93,7 +101,7 @@ func TestValues_BasicTypes(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, tt.myTag, tt.input, tt.want)
}
}

Expand Down Expand Up @@ -129,7 +137,7 @@ func TestValues_Pointers(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "url", tt.input, tt.want)
}
}

Expand Down Expand Up @@ -268,7 +276,7 @@ func TestValues_Slices(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "url", tt.input, tt.want)
}
}

Expand Down Expand Up @@ -325,7 +333,7 @@ func TestValues_NestedTypes(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "url", tt.input, tt.want)
}
}

Expand Down Expand Up @@ -365,7 +373,7 @@ func TestValues_OmitEmpty(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "url", tt.input, tt.want)
}
}

Expand Down Expand Up @@ -420,12 +428,12 @@ func TestValues_EmbeddedStructs(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "url", tt.input, tt.want)
}
}

func TestValues_InvalidInput(t *testing.T) {
_, err := Values("")
_, err := Values("", "")
if err == nil {
t.Errorf("expected Values() to return an error on invalid input")
}
Expand Down Expand Up @@ -480,7 +488,7 @@ func TestValues_CustomEncodingSlice(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "", tt.input, tt.want)
}
}

Expand All @@ -504,7 +512,7 @@ func TestValues_CustomEncoding_Error(t *testing.T) {
},
}
for _, tt := range tests {
_, err := Values(tt.input)
_, err := Values("", tt.input)
if err == nil {
t.Errorf("Values(%q) did not return expected encoding error", tt.input)
}
Expand Down Expand Up @@ -574,7 +582,7 @@ func TestValues_CustomEncodingInt(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "", tt.input, tt.want)
}
}

Expand Down Expand Up @@ -657,7 +665,7 @@ func TestValues_CustomEncodingPointer(t *testing.T) {
}

for _, tt := range tests {
testValue(t, tt.input, tt.want)
testValue(t, "", tt.input, tt.want)
}
}

Expand Down