Skip to content

query: allow custom delimiter for nested structs - #191

Open
AdamMagued wants to merge 1 commit into
google:masterfrom
AdamMagued:nested-struct-delimiter
Open

AdamMagued wants to merge 1 commit into
google:masterfrom
AdamMagued:nested-struct-delimiter

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Currently, nested struct fields are scoped strictly using bracket notation (user[name]=acme&user[addr][postcode]=1234). While slices support custom delimiters via the del tag and named options (comma, space), nested struct scoping did not allow configuring a custom separator such as dot notation (user.name=acme&user.addr.postcode=1234).

This change adds support for configuring the delimiter used for scoping nested struct fields:

  • Specifying the del tag (e.g. del:".", del:"/") on a nested struct field uses that delimiter for scoping child fields.
  • Specifying the dot option in the url tag (e.g. url:"user,dot") sets the delimiter to ..
  • If a parent struct field defines a delimiter, nested sub-structs inherit that delimiter unless explicitly overridden.
  • When no delimiter is specified, the default bracket notation (scope[name]) remains unchanged.

Root Cause Analysis

In query/encode.go, reflectValue hardcoded bracket-based scoping when scope != "":

if scope != "" {
    name = scope + "[" + name + "]"
}

There was no mechanism to customize the separator for struct fields, unlike slices which inspect sf.Tag.Get("del") and tag options.

Verification Results

  • All existing test cases pass without modification.
  • Added unit tests covering:
    • Custom delimiter via del:"." struct tag.
    • Custom delimiter via url:",dot" option.
    • Custom delimiter with arbitrary separator (del:"/").
    • Multi-level nested structs matching the issue How to encode nested structure by my way? #79 example (user.name, user.addr.postcode, user.addr.city).
  • Test suite passes with 100.0% statement coverage:
    go test -v -cover ./... -> coverage: 100.0% of statements.
  • go vet ./... and gofmt run cleanly with zero warnings.

Fixes #79

@google-cla

google-cla Bot commented Oct 1, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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 google#79
@AdamMagued
AdamMagued force-pushed the nested-struct-delimiter branch from 7bda1d9 to a3cbb40 Compare October 4, 2026 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to encode nested structure by my way?

1 participant