Fix indentation of custom type arrays (#944)

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
Daniel Weiße
2024-04-12 16:42:12 +02:00
committed by GitHub
parent 86608d7fca
commit d00d2cca6e
2 changed files with 41 additions and 0 deletions
+4
View File
@@ -1025,6 +1025,10 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
scratch = enc.commented(ctx.commented, scratch)
if enc.indentTables {
scratch = enc.indent(ctx.indent, scratch)
}
scratch = append(scratch, "[["...)
for i, k := range ctx.parentKey {
+37
View File
@@ -1499,6 +1499,43 @@ func TestMarshalCommented(t *testing.T) {
require.Equal(t, expected, string(out))
}
func TestMarshalIndentedCustomTypeArray(t *testing.T) {
c := struct {
Nested struct {
NestedArray []struct {
Value int
}
}
}{
Nested: struct {
NestedArray []struct {
Value int
}
}{
NestedArray: []struct {
Value int
}{
{Value: 1},
{Value: 2},
},
},
}
expected := `[Nested]
[[Nested.NestedArray]]
Value = 1
[[Nested.NestedArray]]
Value = 2
`
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
enc.SetIndentTables(true)
require.NoError(t, enc.Encode(c))
require.Equal(t, expected, buf.String())
}
func ExampleMarshal() {
type MyConfig struct {
Version int