From 2377ac4bc04c4d1fff969c02c1ec02bde202166b Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Fri, 8 Apr 2022 09:25:54 -0400 Subject: [PATCH] encode: fix embedded interfaces (#753) Resolve marshaling regression when handling an embedded interface in a struct. Fixes #752 --- marshaler.go | 4 +++- marshaler_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/marshaler.go b/marshaler.go index 5d120d4..d401ad6 100644 --- a/marshaler.go +++ b/marshaler.go @@ -600,7 +600,9 @@ func walkStruct(ctx encoderCtx, t *table, v reflect.Value) { if k == "" { if fieldType.Anonymous { - walkStruct(ctx, t, f) + if fieldType.Type.Kind() == reflect.Struct { + walkStruct(ctx, t, f) + } continue } else { k = fieldType.Name diff --git a/marshaler_test.go b/marshaler_test.go index 3b720f6..a9c7673 100644 --- a/marshaler_test.go +++ b/marshaler_test.go @@ -947,6 +947,22 @@ func TestIssue678(t *testing.T) { require.Equal(t, cfg, cfg2) } +func TestIssue752(t *testing.T) { + type Fooer interface { + Foo() string + } + + type Container struct { + Fooer + } + + c := Container{} + + out, err := toml.Marshal(c) + require.NoError(t, err) + require.Equal(t, "", string(out)) +} + func TestMarshalNestedAnonymousStructs(t *testing.T) { type Embedded struct { Value string `toml:"value" json:"value"`