From a3cbb40456d62ae13720e9e906b0b9f4155e63b2 Mon Sep 17 00:00:00 2001 From: AdamMagued Date: Fri, 2 Oct 2026 01:29:29 +0300 Subject: [PATCH] query: allow custom delimiter for nested structs Support specifying a custom delimiter for nested struct scoping via the "del" struct tag or the "dot" option. By default, nested structs are scoped with brackets ("user[name]=..."). When "del" is set on a struct field (or "dot" option is provided), child fields use the specified delimiter for scoping (e.g. "user.name=..."). Fixes #79 --- query/encode.go | 29 ++++++++++++--- query/encode_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 5 deletions(-) diff --git a/query/encode.go b/query/encode.go index ffb2097..481e5da 100644 --- a/query/encode.go +++ b/query/encode.go @@ -118,6 +118,13 @@ type Encoder interface { // // "user[name]=acme&user[addr][postcode]=1234&user[addr][city]=SFO" // +// Including the "del" struct tag (separate from the "url" tag) or the "dot" +// option will use that delimiter instead of brackets for scoping nested +// struct fields. For example: +// +// // Field encoded as "user.name=acme&user.addr.postcode=1234&user.addr.city=SFO" +// User User `url:"user,dot"` +// // All other values are encoded using their default string representation. // // Multiple fields that encode to the same URL parameter name will be included @@ -141,14 +148,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(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(values url.Values, val reflect.Value, scope, scopeDel string) error { var embedded []reflect.Value typ := val.Type() @@ -178,8 +185,20 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error { name = sf.Name } + fieldDel := sf.Tag.Get("del") + if fieldDel == "" && opts.Contains("dot") { + fieldDel = "." + } + if fieldDel == "" { + fieldDel = scopeDel + } + if scope != "" { - name = scope + "[" + name + "]" + if scopeDel != "" { + name = scope + scopeDel + name + } else { + name = scope + "[" + name + "]" + } } if opts.Contains("omitempty") && isEmptyValue(sv) { @@ -262,7 +281,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(values, sv, name, fieldDel); err != nil { return err } continue @@ -272,7 +291,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(values, f, scope, scopeDel); err != nil { return err } } diff --git a/query/encode_test.go b/query/encode_test.go index 241e4f1..88b83e6 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -318,6 +318,92 @@ func TestValues_NestedTypes(t *testing.T) { "nest[ptr][value]": {"v"}, }, }, + + // nested struct with custom delimiter (del:"." tag) + { + struct { + Nest Nested `url:"nest" del:"."` + }{ + Nested{ + A: SubNested{ + Value: "v", + }, + }, + }, + url.Values{ + "nest.a.value": {"v"}, + "nest.b": {""}, + }, + }, + + // nested struct with dot option + { + struct { + Nest Nested `url:"nest,dot"` + }{ + Nested{ + A: SubNested{ + Value: "v", + }, + }, + }, + url.Values{ + "nest.a.value": {"v"}, + "nest.b": {""}, + }, + }, + + // nested struct with arbitrary delimiter + { + struct { + Nest Nested `url:"nest" del:"/"` + }{ + Nested{ + A: SubNested{ + Value: "v", + }, + }, + }, + url.Values{ + "nest/a/value": {"v"}, + "nest/b": {""}, + }, + }, + + // issue #79 example: user.name, user.addr.postcode, user.addr.city + { + struct { + User struct { + Name string `url:"name"` + Addr struct { + Postcode string `url:"postcode"` + City string `url:"city"` + } `url:"addr"` + } `url:"user,dot"` + }{ + struct { + Name string `url:"name"` + Addr struct { + Postcode string `url:"postcode"` + City string `url:"city"` + } `url:"addr"` + }{ + Name: "acme", + Addr: struct { + Postcode string `url:"postcode"` + City string `url:"city"` + }{ + Postcode: "1234", + City: "SFO", + }, + }, + }, + url.Values{ + "user.name": {"acme"}, + "user.addr.postcode": {"1234"}, + "user.addr.city": {"SFO"}, + }, + }, { nil, url.Values{},