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: support comment on array tables #776

Merged
merged 1 commit into from May 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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