Example
---
person_table:
- &person001
fname: homer
lname: simpson
role: dad
age: 33
- &person002
fname: marge
lname: simpson
role: mom
age: 34
- &person003
fname: peter
lname: griffin
role: dad
age: 34
Problem
- developer wishes to express a table structure in YAML, where each row is referenced by a compact row identifier
Solution
- use YAML anchors, by assigning an anchor identifier to each row in the table
- in YAML, reusable "transclusion identifiers" are called anchors and aliases
- in YAML, reusable "transclusion identifiers" consist of alphanumeric tokens preceeded by an ampersand or asterisk
Rationale
- YAML anchors and aliases allow for increased data normalization
- YAML anchors and aliases enforce
DRY
(Don't repeat yourself)
- in this example, a table structure can be designed and preserved which closely coincides with a database
Pitfalls
- YAML anchors must be declared before they can be referenced by aliases
- YAML anchors must be unique throughout the document
- failure to specify unique anchors will cause an error on
yaml.load()
- not all YAML parsers reliably support anchors and aliases
See also
Stackoverflow YAML