Skip to content

Instantly share code, notes, and snippets.

@michfield
Last active December 16, 2015 16:19
Show Gist options
  • Save michfield/5462466 to your computer and use it in GitHub Desktop.
Save michfield/5462466 to your computer and use it in GitHub Desktop.

Using variables, also called repeated nodes in YAML, but in JSON variant of YAML format. I'm using this for the whole subtree. Great!

{
  "original": &VAR
  {
    "x": "Value x",
    "subtree":
    {
      "y": "Value Y"
    }
  },

  "copy": *VAR
}

Enter this in online YAML parser and you will see the light.

Also, this is fuckingly awesome! YAML merge key, but in JSON format. Again, put it in an online parser.

{
  "common": &COMMON
  {
    "username": "portly", 
    "adapter": "mysql", 
    "password": "666HELL666", 
    "host": "localhost", 
    "database": "rails_dev"
  }, 
  "production": *COMMON, 
  "testing": *COMMON,
  "custom":
  {
    <<: *COMMON,
    "username": "CHANGED FROM COMMON"
  }
}

You can also do this, often usable for default values:

{
  <<: { "username": "Default value" },
  "username": "New value"
}

Note that the last one doesn't work on Ruby parser. But the following stuff does, and obeys the order of execution:

{
  "dummy": &DEFAULT
  {
    "username": "Default value"
  },

  <<: *DEFAULT,
  "username": "New value"
}
@michfield
Copy link
Author

I love you YAML

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment