I'm trying to learn to use YAML v3 unmarshaling with full access to the Node in a complex embedded struct.
The announcement post explains how to use yaml.Node, but doesn't give a serious example. And the documentation also doesn't show working with the Node. The primary purpose seems to be the excellent preservation of comments in YAML files.
For example, working with an extension of the announcement article, I have
package main
import(
"fmt"
"gopkg.in/yaml.v3"
"os"
)
func main() {
type Person struct {
Name string
Address yaml.Node
}
data := `
name: John Doe
address:
street: 123 E 3rd St # street is like an avenue
city: Denver # A city might be a town as well
state: CO # A state might be a province or administrative unit
zip: 81526 # zip might be "postal_code"
`
var person Person
err := yaml.Unmarshal([]byte(data), &person)
if (err != nil) {
fmt.Printf("Failed to unmarshall: %v", err)
os.Exit(1)
}
fmt.Printf("Marshalled person=%v", person)
}
But if I try to use the address items, I find them each listed as an array of Content inside the node; there's no actual useful information there. The comments are in there, but it's not clear what they're associated with.
Modify existing yaml file and add new data and comments also deals with the same territory, but doesn't show navigating the structure after unmarshalling into the struct.
How can I navigate the "Address" node once unmarshalled, and preserve the comments on marshalling again?