Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(Enc): allows the caller to change the tag used to locate custom field aliases #15

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
36 changes: 30 additions & 6 deletions query/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ 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) {
return values(v, "url")
}

func values(v interface{}, tag string) (url.Values, error) {
values := make(url.Values)
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
Expand All @@ -128,14 +133,33 @@ func Values(v interface{}) (url.Values, error) {
return nil, fmt.Errorf("query: Values() expects struct input. Got %v", val.Kind())
}

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


type Enc struct{
tag string
}

func NewEncoder()*Enc{
return &Enc{tag: "url"}
}

// SetAliasTag changes the tag used to locate custom field aliases. The default tag is "url".
func(e *Enc)SetAliasTag(tag string)*Enc{
e.tag = tag
return e
}

func (e *Enc)Values(v interface{}) (url.Values, error){
return values(v, e.tag)
}

// 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(values url.Values, val reflect.Value, scope, tagSel string) error {
var embedded []reflect.Value

typ := val.Type()
Expand All @@ -146,7 +170,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(tagSel)
if tag == "-" {
continue
}
Expand Down Expand Up @@ -230,15 +254,15 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
}

if sv.Kind() == reflect.Struct {
reflectValue(values, sv, name)
reflectValue(values, sv, name, tagSel)
continue
}

values.Add(name, valueString(sv, opts))
}

for _, f := range embedded {
if err := reflectValue(values, f, scope); err != nil {
if err := reflectValue(values, f, scope, tagSel); err != nil {
return err
}
}
Expand Down
36 changes: 36 additions & 0 deletions query/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,42 @@ func TestValues_types(t *testing.T) {
}
}

func TestEnc(t *testing.T){
var tests = []struct{
in interface{}
want url.Values
}{
{
in: struct{
C []string `xyz:"a"`
}{
C: []string{"a", "b"},
},
want: url.Values{"a": {"a", "b"},},
},
{
in: struct{
C []string
}{
C: []string{"a", "b"},
},
want: url.Values{"C": {"a", "b"},},
},

}
for i, tt := range tests {
en := NewEncoder().SetAliasTag("xyz")
v, err := en.Values(tt.in)
if err != nil {
t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
}

if !reflect.DeepEqual(tt.want, v) {
t.Errorf("%d. Enc(%q) returned %v, want %v", i, tt.in, v, tt.want)
}
}
}

func TestValues_omitEmpty(t *testing.T) {
str := ""
s := struct {
Expand Down