YAML Reference Card

Our projects contain a lot of configuration. So much, in fact, that our parsers have been struggling with the size of our files and causing sites to time out :s. Therefore, the exemplar team have created a reference YAML. We write some default configurations in our reference YAML and then our main configuration can use these defaults and override them.

This has significantly improved our performance for Exemplar. However, just recently I came across this page: yaml.org/refcard.html which pretty much said what we were trying to achieve is (or should be) already available in our YAML parsing. I thought I’d share my findings with you as a lot of people don’t seem to know of these features.

Aliasing

here’s an example of repetitiveness in YAMLs:

bill-to:
  given: Chris
  family: Dumars
  address:
    lines: |
      458 Walman Dr.
      Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to:
  given: Chris
  family: Dumars
  address:
    lines: |
      458 Walman Dr.
      Suite #292
    city: Royal Oak
    state: MI
    postal: 48046

Here’s how to write the above example with aliases:

bill-to: &id001
  given: Chris
  family: Dumars
  address:
    lines: |
      458 Walman Dr.
      Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to: *id001

And even better… you can override previous values, or even add extras by ‘merging’ an anchor:

bill-to: &id001
  given: Chris
  family: Dumars
  address:
    lines: |
      458 Walman Dr.
      Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to:
  <<: *id001
  given: Sam

This would have been a fantastic alternative to what we have already created, however, YAML to PHP parsers don’t seem to like the merging syntax :(

Leave a Reply

You must be logged in to post a comment.