Skip to content

Commit

Permalink
Encode embedded *structs
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Dec 20, 2013
1 parent 8f07d18 commit efc01ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,12 @@ func (enc *Encoder) eStruct(key Key, rv reflect.Value) error {
f := rt.Field(i)
frv := rv.Field(i)
if f.Anonymous {
addFields(frv.Type(), frv, f.Index)
t := frv.Type()
if t.Kind() == reflect.Ptr {
t = t.Elem()
frv = frv.Elem()
}
addFields(t, frv, f.Index)
} else if isStructOrMap(frv) {
fieldsSub = append(fieldsSub, append(startingIndex, f.Index...))
} else {
Expand Down
10 changes: 10 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,22 @@ ArrayOfMixedSlices = [[1, 2], ["a", "b"]]`,
input: struct{ Embedded }{Embedded{1}},
wantOutput: "_int = 1",
},
"embedded *struct": {
input: struct{ *Embedded }{&Embedded{1}},
wantOutput: "_int = 1",
},
"nested embedded struct": {
input: struct {
Struct struct{ Embedded } `toml:"_struct"`
}{struct{ Embedded }{Embedded{1}}},
wantOutput: "[_struct]\n _int = 1",
},
"nested embedded *struct": {
input: struct {
Struct struct{ *Embedded } `toml:"_struct"`
}{struct{ *Embedded }{&Embedded{1}}},
wantOutput: "[_struct]\n _int = 1",
},
"array of tables": {
input: struct {
Structs []*struct{ Int int } `toml:"struct"`
Expand Down

0 comments on commit efc01ff

Please sign in to comment.