Skip to content

Commit d00d2cc

Browse files
Fix indentation of custom type arrays (#944)
Signed-off-by: Daniel Weiße <dw@edgeless.systems>
1 parent 86608d7 commit d00d2cc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

marshaler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,10 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
10251025

10261026
scratch = enc.commented(ctx.commented, scratch)
10271027

1028+
if enc.indentTables {
1029+
scratch = enc.indent(ctx.indent, scratch)
1030+
}
1031+
10281032
scratch = append(scratch, "[["...)
10291033

10301034
for i, k := range ctx.parentKey {

marshaler_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,43 @@ func TestMarshalCommented(t *testing.T) {
14991499
require.Equal(t, expected, string(out))
15001500
}
15011501

1502+
func TestMarshalIndentedCustomTypeArray(t *testing.T) {
1503+
c := struct {
1504+
Nested struct {
1505+
NestedArray []struct {
1506+
Value int
1507+
}
1508+
}
1509+
}{
1510+
Nested: struct {
1511+
NestedArray []struct {
1512+
Value int
1513+
}
1514+
}{
1515+
NestedArray: []struct {
1516+
Value int
1517+
}{
1518+
{Value: 1},
1519+
{Value: 2},
1520+
},
1521+
},
1522+
}
1523+
1524+
expected := `[Nested]
1525+
[[Nested.NestedArray]]
1526+
Value = 1
1527+
1528+
[[Nested.NestedArray]]
1529+
Value = 2
1530+
`
1531+
1532+
var buf bytes.Buffer
1533+
enc := toml.NewEncoder(&buf)
1534+
enc.SetIndentTables(true)
1535+
require.NoError(t, enc.Encode(c))
1536+
require.Equal(t, expected, buf.String())
1537+
}
1538+
15021539
func ExampleMarshal() {
15031540
type MyConfig struct {
15041541
Version int

0 commit comments

Comments
 (0)