Skip to content

Commit

Permalink
Fix indentation of custom type arrays (#944)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse authored Apr 12, 2024
1 parent 86608d7 commit d00d2cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d00d2cc

Please sign in to comment.