Skip to content
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

schema: add encoder.SetOmitEmptyDefault() method to properly encode ptrs #219

Open
wants to merge 1 commit into
base: main
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
19 changes: 15 additions & 4 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ type encoderFunc func(reflect.Value) string

// Encoder encodes values from a struct into url.Values.
type Encoder struct {
cache *cache
regenc map[reflect.Type]encoderFunc
cache *cache
regenc map[reflect.Type]encoderFunc
omitEmpty bool
}

// NewEncoder returns a new Encoder with defaults.
Expand Down Expand Up @@ -40,6 +41,16 @@ func (e *Encoder) SetAliasTag(tag string) {
e.cache.tag = tag
}

// SetOmitEmptyDefault allows to set omit empty behaviour by default.
// When pointers to values are used in fields it is required to use omitempty to avoid
// encoding them as 'null' (which are not decodable by Decoder). This function allows
// to make omitempty behaviour to be a default, rather than requiring to specify
// per field tag.
// The default is false, i.e. one should specify 'omitempty' in tags as usual.
func (e *Encoder) SetOmitEmptyDefault(omit bool) {
e.omitEmpty = omit
}

// isValidStructPointer test if input value is a valid struct pointer.
func isValidStructPointer(v reflect.Value) bool {
return v.Type().Kind() == reflect.Ptr && v.Elem().IsValid() && v.Elem().Type().Kind() == reflect.Struct
Expand Down Expand Up @@ -106,7 +117,7 @@ func (e *Encoder) encode(v reflect.Value, dst map[string][]string) error {
// Encode non-slice types and custom implementations immediately.
if encFunc != nil {
value := encFunc(v.Field(i))
if opts.Contains("omitempty") && isZero(v.Field(i)) {
if (e.omitEmpty || opts.Contains("omitempty")) && isZero(v.Field(i)) {
continue
}

Expand All @@ -132,7 +143,7 @@ func (e *Encoder) encode(v reflect.Value, dst map[string][]string) error {
}

// Encode a slice.
if v.Field(i).Len() == 0 && opts.Contains("omitempty") {
if v.Field(i).Len() == 0 && (e.omitEmpty || opts.Contains("omitempty")) {
continue
}

Expand Down
29 changes: 29 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,32 @@ func TestRegisterEncoderWithPtrType(t *testing.T) {
valExists(t, "DateStart", ss.DateStart.time.String(), vals)
valExists(t, "DateEnd", "", vals)
}

func TestPtrsWithDefaultOmitEmpty(t *testing.T) {
type S1 struct {
S *string `schema:"s"`
B *bool `schema:"b"`
}

ss := S1{} // all fields are nil

// default encoder encodes ptrs as "null", which are not decodable
// so one has to use omitempty tags
encoder := NewEncoder()

vals := map[string][]string{}
err := encoder.Encode(ss, vals)

noError(t, err)
valsLength(t, 2, vals)
valExists(t, "s", "null", vals)
valExists(t, "b", "null", vals)

// however, if encoder.SetOmitEmptyDefault(true) is used such fields should be ignored
vals = map[string][]string{}
encoder.SetOmitEmptyDefault(true)
err = encoder.Encode(ss, vals)

noError(t, err)
valsLength(t, 0, vals)
}
Loading