Skip to content

Commit

Permalink
added omitempty support
Browse files Browse the repository at this point in the history
  • Loading branch information
moogar0880 committed Jul 19, 2016
1 parent e5f753e commit f103366
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (d *decoder) value(val reflect.Value) error {
// pull out the qstring struct tag
elemField := elem.Field(i)
typField := typ.Field(i)
qstring := typField.Tag.Get(Tag)
qstring, _ := parseTag(typField.Tag.Get(Tag))
if qstring == "" {
// resolvable fields must have at least the `flag` struct tag
qstring = strings.ToLower(typField.Name)
Expand Down
4 changes: 2 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ func (e *encoder) value(val reflect.Value) (url.Values, error) {
// pull out the qstring struct tag
elemField := elem.Field(i)
typField := typ.Field(i)
qstring := typField.Tag.Get(Tag)
qstring, omit := parseTag(typField.Tag.Get(Tag))
if qstring == "" {
// resolvable fields must have at least the `flag` struct tag
qstring = strings.ToLower(typField.Name)
}

// determine if this is an unsettable field or was explicitly set to be
// ignored
if !elemField.CanSet() || qstring == "-" || isEmptyValue(elemField) {
if !elemField.CanSet() || qstring == "-" || (omit && isEmptyValue(elemField)) {
continue
}

Expand Down
3 changes: 2 additions & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func TestMarshallValues(t *testing.T) {
}

if len(values) != len(expected) {
t.Errorf("Expected %d fields, got %d", len(expected), len(values))
t.Errorf("Expected %d fields, got %d. Hidden is %q",
len(expected), len(values), values["hidden"])
}
}

Expand Down
18 changes: 18 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package qstring

import (
"strings"
)

// parseTag splits a struct field's qstring tag into its name and, if an
// optional omitempty option was provided, a boolean indicating this is
// returned
func parseTag(tag string) (string, bool) {
if idx := strings.Index(tag, ","); idx != -1 {
if tag[idx+1:] == "omitempty" {
return tag[:idx], true
}
return tag[:idx], false
}
return tag, false
}
57 changes: 57 additions & 0 deletions tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package qstring

import (
"testing"
)

func TestTagParsing(t *testing.T) {
testio := []struct {
inp string
output string
omit bool
}{
{inp: "name,omitempty", output: "name", omit: true},
{inp: "name", output: "name", omit: false},
{inp: "name,", output: "name", omit: false},
{inp: "name", output: "name", omit: false},
{inp: "", output: "", omit: false},
{inp: ",omitempty", output: "", omit: true},
{inp: "-", output: "-", omit: false},
}

var name string
var omit bool
for _, test := range testio {
name, omit = parseTag(test.inp)
if name != test.output {
t.Errorf("Expected tag name to be %q, got %q instead", test.output, name)
}

if omit != test.omit {
t.Errorf("Expected omitempty to be %t, got %t instead", test.omit, omit)
}
}
}

func TestOmitEmpty(t *testing.T) {
vis := 5
testio := []struct {
Visible int
Conditional int `qstring:"conditional,omitempty"`
omit bool
}{
{Visible: vis, Conditional: 5, omit: false},
{Visible: vis, Conditional: 0, omit: true},
}

for _, test := range testio {
values, _ := MarshalValues(&test)

_, found := values["conditional"]
if found && test.omit {
t.Errorf("%d was unexpectedly marshaled", test.Conditional)
} else if !found && !test.omit {
t.Errorf("%d was not marshaled, but should have been", test.Conditional)
}
}
}
6 changes: 6 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package qstring
import (
"reflect"
"testing"
"time"
)

func TestIsEmptyValue(t *testing.T) {
Expand All @@ -27,6 +28,11 @@ func TestIsEmptyValue(t *testing.T) {
{inp: reflect.ValueOf(&TestStruct{}), expected: false},
{inp: reflect.ValueOf(ts), expected: true},
{inp: reflect.ValueOf(nil), expected: false},
{inp: reflect.ValueOf(time.Time{}), expected: true},
{inp: reflect.ValueOf(time.Now()), expected: false},
{inp: reflect.ValueOf(*NewComparativeTime()), expected: true},
{inp: reflect.ValueOf(ComparativeTime{Operator: "=", Time: time.Now()}),
expected: false},
}

var result bool
Expand Down

0 comments on commit f103366

Please sign in to comment.