go-yaml / yaml

YAML support for the Go language.
Other
6.9k stars 1.06k forks source link

headComment and footComment for multiple documents not consistent #801

Open MisterMX opened 3 years ago

MisterMX commented 3 years ago

When encoding a multi-document file, the headComment and footComment are not associated properly with the document root.

Example code:

package main

import (
    "bytes"
    "io"
    "log"

    yaml "gopkg.in/yaml.v3"
)

const testYaml = `---
# head1

ab: cd

# foot1
---
# head2

ab: cd

# foot2
`

func main() {
    buf := bytes.NewBufferString(testYaml)
    dec := yaml.NewDecoder(buf)

    var node yaml.Node

    for {
        if err := dec.Decode(&node); err != nil {
            if err != io.EOF {
                log.Fatal(err)
            }
            break
        }

        log.Println("Head: ", node.HeadComment)
        log.Println("Foot: ", node.FootComment)
        log.Println("---")
    }

    log.Println("Done")
}

Output:

Head:  
Foot:  # head2
---
Head:  
Foot:  # foot2
---
Done
rriemann commented 2 months ago

I also ran into this issue when splitting yaml documents from helmfile.