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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user