Skip to content

query: omitempty ignores IsZero on named scalar and collection types #202

Open

Description

@Rajatendu1

Values documents that a type returning true from IsZero() bool is empty, but isEmptyValue returns from its kind switch before checking that method for named integers, strings, slices, and the other kinds in the switch. A custom sentinel value is therefore sent as a query parameter even when its type explicitly marks it empty.

Reproduced on current master, 854f39986c87d97512b6cc5c59ccee17a72b248e:

package main

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

type Limit int

func (l Limit) IsZero() bool { return l == -1 }

func main() {
    opts := struct {
        Limit Limit `url:"limit,omitempty"`
    }{Limit: -1}
    values, err := query.Values(opts)
    fmt.Printf("query=%q error=%v\n", values.Encode(), err)
}

Actual output:

query="limit=-1" error=<nil>

Expected from the documented IsZero rule: an empty query. I also reproduced this with a named string whose sentinel is "unset" and a named slice whose IsZero returns true for a sentinel element. Both sent the sentinel instead of omitting the field; a normal nonempty integer still encoded as expected.

This is separate from #198/#199, which concern traversal of embedded structs. It affects ordinary exported fields. #25 introduced the general IsZero contract, but the kind-specific early returns still bypass it here.

Validation before any source changes:

  • go test -race -cover ./... passes, with 100% statement coverage.
  • The standalone reproduction above still sends the sentinel on that unchanged baseline.

I can contribute a focused fix and regression tests. Before changing compatibility behavior, should IsZero add to the existing built-in emptiness rules, or take precedence over them? The former would preserve omission of ordinary zero values and additionally omit custom sentinel values; the latter would also let a custom type keep its underlying zero value when its method returns false.

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