Anchors and merge keys
The task: a file declares database defaults once and merges them into two entries. Decode it so each entry holds the defaults plus its own keys.
The type and the file
type DB struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Pool int `yaml:"pool"`
}Full source: docs/examples/decoding/anchors/example_test.go
// primary and replica merge the defaults with "<<", and replica overrides the host.
const src = `defaults: &db
host: db.internal
port: 5432
primary:
<<: *db
pool: 20
replica:
<<: *db
host: replica.internal
`Full source: docs/examples/decoding/anchors/example_test.go
The default merges nothing
var dbs map[string]DB
err := codec.Unmarshal([]byte(src), &dbs)
fmt.Printf("%+v %v\n", dbs["primary"], err)
fmt.Printf("%+v\n", dbs["replica"])Full source: docs/examples/decoding/anchors/example_test.go
{Host: Port:0 Pool:20} <nil>
{Host:replica.internal Port:0 Pool:0}Full source: docs/examples/decoding/anchors/example_test.go
There is no error, and neither entry has a port. The merge key is a YAML 1.1
type. Under 1.2, the default, a bare << is an ordinary key spelled <<. DB
has no field for it, so the decoder skips it with the defaults it holds.
Decode with WithMergeKeys
var dbs map[string]DB
err := codec.Unmarshal([]byte(src), &dbs,
codec.WithParserOptions(parser.WithMergeKeys()),
)
fmt.Printf("%+v %v\n", dbs["primary"], err)
fmt.Printf("%+v\n", dbs["replica"])Full source: docs/examples/decoding/anchors/example_test.go
{Host:db.internal Port:5432 Pool:20} <nil>
{Host:replica.internal Port:5432 Pool:0}Full source: docs/examples/decoding/anchors/example_test.go
parser.WithMergeKeys resolves a bare << as a merge key at every version,
and changes nothing else: yes and 0100 still resolve under 1.2.
codec.WithParserOptions passes it to the parser the decode runs. A key the
entry writes itself wins over a merged one, so the replica keeps its own host.
Aliases build their own value
// Two aliases of one anchor.
const tags = `base: &b
tags: [stable]
first: *b
second: *b
`Full source: docs/examples/decoding/anchors/example_test.go
var v map[string]map[string][]string
if err := codec.Unmarshal([]byte(tags), &v); err != nil {
fmt.Println(err)
return
}
v["first"]["tags"][0] = "changed"
fmt.Println(v["base"]["tags"], v["first"]["tags"], v["second"]["tags"])Full source: docs/examples/decoding/anchors/example_test.go
[stable] [changed] [stable]Full source: docs/examples/decoding/anchors/example_test.go
By default each alias decodes to a value of its own, as if the anchored node
were written out again at each alias. Writing through first leaves base
and second alone.
Decode with ShareAliases
var v map[string]map[string][]string
if err := codec.Unmarshal([]byte(tags), &v, codec.ShareAliases()); err != nil {
fmt.Println(err)
return
}
v["first"]["tags"][0] = "changed"
fmt.Println(v["base"]["tags"], v["first"]["tags"], v["second"]["tags"])Full source: docs/examples/decoding/anchors/example_test.go
[changed] [changed] [changed]Full source: docs/examples/decoding/anchors/example_test.go
Under codec.ShareAliases, the anchor and its aliases hold one Go value, so
the write shows through all three names. Use it when the value goes back
through Marshal: the encoder writes *b only where one value stands under
two names. A shared value is also built once, however many aliases name it.
Variations
- Spell the merge out.
!!merge <<: *dbmerges under 1.2 with no option, and so does a document whose%YAML 1.1line selects 1.1. - Catch the silent miss. Under the default,
codec.DisallowUnknownField()rejects the bare<<asunknown field "<<"at line 6. - Into
any. Under the default,<<stays in the decoded map as a key whose value is the defaults mapping.
The rules in full: Anchors, aliases and merge keys.