Support encoding of pointers to embedded structs (#924)
This commit is contained in:
@@ -707,6 +707,8 @@ func walkStruct(ctx encoderCtx, t *table, v reflect.Value) {
|
||||
if fieldType.Anonymous {
|
||||
if fieldType.Type.Kind() == reflect.Struct {
|
||||
walkStruct(ctx, t, f)
|
||||
} else if fieldType.Type.Kind() == reflect.Pointer && !f.IsNil() && f.Elem().Kind() == reflect.Struct {
|
||||
walkStruct(ctx, t, f.Elem())
|
||||
}
|
||||
continue
|
||||
} else {
|
||||
|
||||
@@ -1304,6 +1304,38 @@ value = ''
|
||||
require.Equal(t, expected, string(result))
|
||||
}
|
||||
|
||||
func TestMarshalNestedAnonymousStructs_PointerEmbedded(t *testing.T) {
|
||||
type Embedded struct {
|
||||
Value string `toml:"value" json:"value"`
|
||||
Omitted string `toml:"omitted,omitempty"`
|
||||
Ptr *string `toml:"ptr"`
|
||||
}
|
||||
|
||||
type Named struct {
|
||||
Value string `toml:"value" json:"value"`
|
||||
}
|
||||
|
||||
type Doc struct {
|
||||
*Embedded
|
||||
*Named `toml:"named" json:"named"`
|
||||
Anonymous struct {
|
||||
*Embedded
|
||||
Value *string `toml:"value" json:"value"`
|
||||
} `toml:"anonymous,omitempty" json:"anonymous,omitempty"`
|
||||
}
|
||||
|
||||
doc := &Doc{
|
||||
Embedded: &Embedded{Value: "foo"},
|
||||
}
|
||||
|
||||
expected := `value = 'foo'
|
||||
`
|
||||
|
||||
result, err := toml.Marshal(doc)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, string(result))
|
||||
}
|
||||
|
||||
func TestLocalTime(t *testing.T) {
|
||||
v := map[string]toml.LocalTime{
|
||||
"a": {
|
||||
|
||||
Reference in New Issue
Block a user