Encode: add comment struct tag (#711)

Similar to v1, add a `comment` struct that that makes the encoder emit a comment
before the annotated element, if permitted. Unlike v1, comments are compact by
default (and cannot be changed).

Fixes #595.
This commit is contained in:
Thomas Pelletier
2021-12-26 18:29:46 +01:00
committed by GitHub
parent 8ce5c3d78f
commit 5fd6e9cce0
2 changed files with 61 additions and 15 deletions
+27
View File
@@ -21,6 +21,12 @@ func TestMarshal(t *testing.T) {
A interface{} `toml:",inline"`
}
type comments struct {
One int
Two int `comment:"Before kv"`
Three []int `comment:"Before array"`
}
examples := []struct {
desc string
v interface{}
@@ -535,6 +541,27 @@ J = 42
K = 42
L = 2.2`,
},
{
desc: "comments",
v: struct {
Table comments `comment:"Before table"`
}{
Table: comments{
One: 1,
Two: 2,
Three: []int{1, 2, 3},
},
},
expected: `
# Before table
[Table]
One = 1
# Before kv
Two = 2
# Before array
Three = [1, 2, 3]
`,
},
}
for _, e := range examples {