Skip to content

Commit

Permalink
Properly encode struct fields having toml tags without a name
Browse files Browse the repository at this point in the history
  • Loading branch information
cespare committed Mar 3, 2016
1 parent 0e5f512 commit 1946733
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 3 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,14 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value) {
continue
}

keyName := sft.Tag.Get("toml")
if keyName == "-" {
tag := sft.Tag.Get("toml")
if tag == "-" {
continue
}
keyName, opts := getOptions(tag)
if keyName == "" {
keyName = sft.Name
}

keyName, opts := getOptions(keyName)
if _, ok := opts["omitempty"]; ok && isEmpty(sf) {
continue
} else if _, ok := opts["omitzero"]; ok && isZero(sf) {
Expand Down
10 changes: 10 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,16 @@ unsigned = 5
encodeExpected(t, "simple with omitzero, non-zero", value, expected, nil)
}

func TestEncodeOmitemptyWithEmptyName(t *testing.T) {
type simple struct {
S []int `toml:",omitempty"`
}
v := simple{[]int{1, 2, 3}}
expected := "S = [1, 2, 3]\n"
encodeExpected(t, "simple with omitempty, no name, non-empty field",
v, expected, nil)
}

func TestEncodeAnonymousStructPointerField(t *testing.T) {
type Sub struct{}
type simple struct {
Expand Down

0 comments on commit 1946733

Please sign in to comment.