summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/v2/marshaler.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/pelletier/go-toml/v2/marshaler.go')
-rw-r--r--vendor/github.com/pelletier/go-toml/v2/marshaler.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/vendor/github.com/pelletier/go-toml/v2/marshaler.go b/vendor/github.com/pelletier/go-toml/v2/marshaler.go
index 91f3b3c2..c14c1cea 100644
--- a/vendor/github.com/pelletier/go-toml/v2/marshaler.go
+++ b/vendor/github.com/pelletier/go-toml/v2/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
@@ -652,10 +653,19 @@ func (enc *Encoder) encodeStruct(b []byte, ctx encoderCtx, v reflect.Value) ([]b
}
func (enc *Encoder) encodeComment(indent int, comment string, b []byte) []byte {
- if comment != "" {
+ for len(comment) > 0 {
+ var line string
+ idx := strings.IndexByte(comment, '\n')
+ if idx >= 0 {
+ line = comment[:idx]
+ comment = comment[idx+1:]
+ } else {
+ line = comment
+ comment = ""
+ }
b = enc.indent(indent, b)
b = append(b, "# "...)
- b = append(b, comment...)
+ b = append(b, line...)
b = append(b, '\n')
}
return b
@@ -881,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...)