Skip to content

Commit

Permalink
Encode: fix ignored indent of array tables (#889)
Browse files Browse the repository at this point in the history
Fixes #888
  • Loading branch information
pelletier committed Aug 28, 2023
1 parent bb026ca commit 4040373
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
38 changes: 38 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 4040373

Please sign in to comment.