Skip to content

introduce combination of "brackets" and "numbered" option on slices #35

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

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
27 changes: 18 additions & 9 deletions query/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type Encoder interface {
// have "[]" appended to the value name. "numbered" will append a number to
// the end of each incidence of the value name, example:
// name0=value0&name1=value1, etc.
// The combination of both "brackets" and "numbered" option signals that the
// multiple URL values should have indexed brackets, example:
// name[0]=value0&name[1]=value1, etc.
//
// Anonymous struct fields are usually encoded as if their inner exported
// fields were fields in the outer struct, subject to the standard Go
Expand Down Expand Up @@ -189,8 +192,6 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
del = ' '
} else if opts.Contains("semicolon") {
del = ';'
} else if opts.Contains("brackets") {
name = name + "[]"
}

if del != 0 {
Expand All @@ -205,14 +206,22 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error {
s.WriteString(valueString(sv.Index(i), opts))
}
values.Add(name, s.String())
} else {
for i := 0; i < sv.Len(); i++ {
k := name
if opts.Contains("numbered") {
k = fmt.Sprintf("%s%d", name, i)
}
values.Add(k, valueString(sv.Index(i), opts))
continue
}

b := opts.Contains("brackets")
n := opts.Contains("numbered")
if b && !n {
name = fmt.Sprintf("%s[]", name)
}
for i := 0; i < sv.Len(); i++ {
k := name
if b && n {
k = fmt.Sprintf("%s[%d]", name, i)
} else if n {
k = fmt.Sprintf("%s%d", name, i)
}
values.Add(k, valueString(sv.Index(i), opts))
}
continue
}
Expand Down
28 changes: 16 additions & 12 deletions query/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func TestValues_types(t *testing.T) {
I []string `url:",brackets"`
J []string `url:",semicolon"`
K []string `url:",numbered"`
L []string `url:",brackets,numbered"`
}{
A: []string{"a", "b"},
B: []string{"a", "b"},
Expand All @@ -93,20 +94,23 @@ func TestValues_types(t *testing.T) {
I: []string{"a", "b"},
J: []string{"a", "b"},
K: []string{"a", "b"},
L: []string{"a", "b"},
},
url.Values{
"A": {"a", "b"},
"B": {"a,b"},
"C": {"a b"},
"D": {"a", "b"},
"E": {"a,b"},
"F": {"a b"},
"G": {"string string"},
"H": {"1 0"},
"I[]": {"a", "b"},
"J": {"a;b"},
"K0": {"a"},
"K1": {"b"},
"A": {"a", "b"},
"B": {"a,b"},
"C": {"a b"},
"D": {"a", "b"},
"E": {"a,b"},
"F": {"a b"},
"G": {"string string"},
"H": {"1 0"},
"I[]": {"a", "b"},
"J": {"a;b"},
"K0": {"a"},
"K1": {"b"},
"L[0]": {"a"},
"L[1]": {"b"},
},
},
{
Expand Down