Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode: fix indentation when marshalling slices as array tables #944

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading