Skip to content

Commit

Permalink
Encode: support comment on array tables (#776)
Browse files Browse the repository at this point in the history
Fixes #774
  • Loading branch information
pelletier committed May 10, 2022
1 parent b2e0231 commit 627dade
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion marshaler.go
Expand Up @@ -128,7 +128,8 @@ func (enc *Encoder) SetIndentTables(indent bool) *Encoder {
//
// In addition to the "toml" tag struct tag, a "comment" tag can be used to emit
// a TOML comment before the value being annotated. Comments are ignored inside
// inline tables.
// inline tables. For array tables, the comment is only present before the first
// element of the array.
func (enc *Encoder) Encode(v interface{}) error {
var (
b []byte
Expand Down Expand Up @@ -890,6 +891,8 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect.
scratch = append(scratch, "]]\n"...)
ctx.skipTableHeader = true

b = enc.encodeComment(ctx.indent, ctx.options.comment, b)

for i := 0; i < v.Len(); i++ {
b = append(b, scratch...)

Expand Down
24 changes: 24 additions & 0 deletions unmarshaler_test.go
Expand Up @@ -2399,6 +2399,30 @@ func TestIssue772(t *testing.T) {
require.Equal(t, "reach-masterdev-", config.FileHandling.FilePattern)
}

func TestIssue774(t *testing.T) {
type ScpData struct {
Host string `json:"host"`
}

type GenConfig struct {
SCP []ScpData `toml:"scp" comment:"Array of Secure Copy Configurations"`
}

c := &GenConfig{}
c.SCP = []ScpData{{Host: "main.domain.com"}}

b, err := toml.Marshal(c)
require.NoError(t, err)

expected := `# Array of Secure Copy Configurations
[[scp]]
Host = 'main.domain.com'
`

require.Equal(t, expected, string(b))
}

func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {
desc string
Expand Down

0 comments on commit 627dade

Please sign in to comment.