Ask questionsHead comments in list elements misplaced
The following code using go 1.13.3 misplaces HeadComment entries to the underlying node (but not consistently). In particular, head comments in lists of lists items are moved as head comments in each sublist's first node, whereas head comments in normal list items are not consistently preserved (e.g., see the difference between comment 3 and comment 4 in the snippet below)
The following code
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v3"
)
var (
sourceYaml = `
l:
# comment 1
- - a
- b: "2"
- v
# comment 2
- - k
- m
- v
# comment 3
- a
# comment 4
- b: "2"
`
)
func main() {
yamlNode := yaml.Node{}
err := yaml.Unmarshal([]byte(sourceYaml), &yamlNode)
if err != nil {
log.Fatalf("error: %v", err)
}
out, err := yaml.Marshal(&yamlNode)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
}
yields
l:
- # comment 1
- a
- b: "2"
- v
- # comment 2
- k
- m
- v
# comment 3
- a
- # comment 4
b: "2"
Related questions