Encode: fix ignored indent of array tables (#889)

Fixes #888
This commit is contained in:
Thomas Pelletier
2023-08-28 09:52:11 -04:00
committed by GitHub
parent bb026cae89
commit 4040373cfd
2 changed files with 42 additions and 0 deletions
+4
View File
@@ -983,6 +983,10 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
scratch = append(scratch, "]]\n"...)
ctx.skipTableHeader = true
if enc.indentTables {
ctx.indent++
}
b = enc.encodeComment(ctx.indent, ctx.options.comment, b)
for i := 0; i < v.Len(); i++ {
+38
View File
@@ -1201,6 +1201,44 @@ randomize = true
require.Equal(t, expected, buf.String())
}
func TestMarhsalIssue888(t *testing.T) {
type Thing struct {
FieldA string `comment:"my field A"`
FieldB string `comment:"my field B"`
}
type Cfg struct {
Custom []Thing
}
buf := new(bytes.Buffer)
config := Cfg{
Custom: []Thing{
{FieldA: "field a 1", FieldB: "field b 1"},
{FieldA: "field a 2", FieldB: "field b 2"},
},
}
encoder := toml.NewEncoder(buf).SetIndentTables(true)
encoder.Encode(config)
expected := `[[Custom]]
# my field A
FieldA = 'field a 1'
# my field B
FieldB = 'field b 1'
[[Custom]]
# my field A
FieldA = 'field a 2'
# my field B
FieldB = 'field b 2'
`
require.Equal(t, expected, buf.String())
}
func TestMarshalNestedAnonymousStructs(t *testing.T) {
type Embedded struct {
Value string `toml:"value" json:"value"`