Skip to content

Commit

Permalink
Support encoding of pointers to embedded structs (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangra authored Jan 23, 2024
1 parent 34765b4 commit 2ca21fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 2ca21fb

Please sign in to comment.