diff --git a/marshaler.go b/marshaler.go index 8317884d..c14c1cea 100644 --- a/marshaler.go +++ b/marshaler.go @@ -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 @@ -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...) diff --git a/unmarshaler_test.go b/unmarshaler_test.go index d425790b..9af5fb35 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -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