08 Oct 2010

Yaml !!merge


There are two interesting constructs I came across in the database.yml in the typo source. One of them is for creating and using a named block, but the other one was a lot more interesting technique to grab parts of the Yaml tree and merge it into another part of the tree, which works in combination with the named blocks to result in a DRY configuration file like this:

login: &login
    adapter: mysql
    host: localhost
    username: root
    password:

  development:
    database: typo_dev
    <<: *login

  test:
    database: typo_tests
    <<: *login

  production:
    database: typo
    <<: *login

OK, so the login: &login part creates a named block and *login simply uses that block. This is not incredibly difficult so far, but what on earth are those “«:” ? Because I haven’t seen them before, for a moment I was wondering if this is some sort of metaprogramming magic, where the « method/operator will somehow get called and execute with the block given to it. To find out how it works, I went to read the YAML 1.2 specification, but interestingly this syntax wasn’t anywhere in the main doc. As it turns out, « is actually a shorthand for a tag, namely !!merge, and is mentioned in a separate document named _Language-Independent Types for YAML™ _

There are a few other tags mentioned here as well. In ruby, collection types seem to create a custom YAML::PrivateType (or Syck::PrivateType in 1.9) object and shove the data in its @value. I don’t really see much use for these types in ruby now.

Scalar types on the other hand are better integrated, but tags like !!binary, !!value or **!!yaml **still create the PrivateType objects, so I guess they are not  supported - and these are all only recommendations according to the document.


blog comments powered by Disqus