Home > other >  Parse yaml files with "---" in it
Parse yaml files with "---" in it

Time:01-31

I'm using https://github.com/go-yaml/yaml to parse yaml files:

type TestConfig struct {
   Test string `yaml:"test"`
}


yaml file:

test: 123

---

test: 456

But yaml.Unmarshal() only parses the first segment, how can I parse the rest of it?

CodePudding user response:

But yaml.Unmarshal() only parses the first segment, how can I parse the rest of it?

yaml.Unmarshal's doc says (emphasis mine):

Unmarshal decodes the first document found within the in byte slice and assigns decoded values into the out value.

If you want to decode a series of documents, call yaml.NewDecoder() on a stream of your data and then call your decoder's .Decode(...) multiple times. Use io.EOF to identify the end of records.

I usually use an infinite for loop with a break condition for this:

decoder := yaml.NewDecoder(bytes.NewBufferString(data))
for {
    var d Doc
    if err := decoder.Decode(&d); err != nil {
        if err == io.EOF {
            break
        }
        panic(fmt.Errorf("Document decode failed: %w", err))
    }
    fmt.Printf("% v\n", d)
}
fmt.Printf("All documents decoded")

(https://go.dev/play/p/01xdzDN0qB7)

CodePudding user response:

--- is the end of directives marker, i.e. it signals the end of the YAML directives and the beginning of the YAML document proper. Since it signals the beginning of the document, it also implicitly signals the end of the previous document, in other words, it can also be used as a document separator, and it is often used that way to separate multiple documents when streaming YAML instead of providing files or HTTP resources.

All this is to say that you have a YAML streaming containing two documents, but the README explicitly says:

Compatibility

[…] Multi-document unmarshalling is not yet implemented, […]

So, the answer to your question is:

But yaml.Unmarshal() only parses the first segment, how can I parse the rest of it?

You can't.

With this particular library, if you want multiple YAML documents, you need multiple files.

  •  Tags:  
  • Related