Skip to content

Commit

Permalink
Clean up, remove zero as 'empty' and add 'omitzero' option
Browse files Browse the repository at this point in the history
  • Loading branch information
bbuck committed May 1, 2015
1 parent d918309 commit aa708eb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
37 changes: 25 additions & 12 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,10 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value) {
keyName = sft.Name
}

opts := strings.Split(keyName, ",")
keyName = opts[0]
omitEmpty := false
if len(opts) > 1 {
for _, opt := range opts[1:] {
if opt == "omitempty" {
omitEmpty = true
}
}
}
if omitEmpty && isEmpty(sf) {
keyName, opts := getOptions(keyName)
if _, ok := opts["omitempty"]; ok && isEmpty(sf) {
continue
} else if _, ok := opts["omitzero"]; ok && isZero(sf) {
continue
}

Expand Down Expand Up @@ -448,7 +441,20 @@ func tomlArrayType(rv reflect.Value) tomlType {
return firstType
}

func isEmpty(rv reflect.Value) bool {
func getOptions(keyName string) (string, map[string]struct{}) {
opts := make(map[string]struct{})
ss := strings.Split(keyName, ",")
name := ss[0]
if len(ss) > 1 {
for _, opt := range ss {
opts[opt] = struct{}{}
}
}

return name, opts
}

func isZero(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if rv.Int() == 0 {
Expand All @@ -462,6 +468,13 @@ func isEmpty(rv reflect.Value) bool {
if rv.Float() == 0.0 {
return true
}
}

return false
}

func isEmpty(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.String:
if len(strings.TrimSpace(rv.String())) == 0 {
return true
Expand Down
22 changes: 22 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,28 @@ func TestEncodeWithOmitEmpty(t *testing.T) {
encodeExpected(t, "simple with omitempty, not empty", value, expected, nil)
}

func TestEncodeWithOmitZero(t *testing.T) {
type simple struct {
Number int `toml:"number,omitzero"`
Real float64 `toml:"real,omitzero"`
Unsigned uint `toml:"unsigned,omitzero"`
}

value := simple{0, 0.0, uint(0)}
expected := ""

encodeExpected(t, "simple with omitzero, all zero", value, expected, nil)

value.Number = 10
value.Real = 20
value.Unsigned = 5
expected = `number = 10
real = 20.0
unsigned = 5
`
encodeExpected(t, "simple with omitzero, non-zero", value, expected, nil)
}

func encodeExpected(
t *testing.T, label string, val interface{}, wantStr string, wantErr error,
) {
Expand Down

0 comments on commit aa708eb

Please sign in to comment.