Skip to content

query: omitempty is ignored on anonymous struct fields #198

Closed

Description

@Alifarid0011

An anonymous struct field tagged url:",omitempty" is always traversed, even when its IsZero method returns true. Giving the same field an explicit URL name correctly omits it.

Reproduction on master (965d79f2113ea0ff039d29828a08a616a0223d48), Go 1.26.8, linux/amd64:

package main

import (
    "fmt"
    "github.com/google/go-querystring/query"
)

type Filter struct {
    Search string `url:"search"`
}

func (f Filter) IsZero() bool { return f.Search == "" }

func main() {
    v, err := query.Values(struct {
        Filter `url:",omitempty"`
    }{})
    fmt.Printf("%q, %v\n", v.Encode(), err)
}

Actual: "search=", <nil>.
Expected: "", <nil>, consistent with the documented omitempty behavior for types implementing IsZero.

reflectValue queues anonymous structs for recursive traversal before reaching its omitempty check. I am preparing a small fix that checks omission before that step, with regression tests and preservation of exported fields promoted from unexported embedded structs.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions