Skip to content

Instantly share code, notes, and snippets.

@erockdotdev
Last active January 10, 2022 19:58
Show Gist options
  • Save erockdotdev/2bf010d9d9c78dd8083735f670cee958 to your computer and use it in GitHub Desktop.
Save erockdotdev/2bf010d9d9c78dd8083735f670cee958 to your computer and use it in GitHub Desktop.
Writing YAML

YAML is a superset of JSON

The basic structure of YAML is a hash map -

hash map

key: value

Multi-line strings

key: >
  Lorem ipsum so you
  can tripsum dipsum

Sequences | equivalent to array

this YAML below

scalar: 
  - never
  - gonna
  - give
  - you
  - up

is equivalent to this JSON (see this to translate YAML to JSON)

{
  "scalar": [
    "never",
    "gonna",
    "give",
    "you",
    "up"
  ]
}

Items in sequesnce can can also be key value pairs

key:
  - stuff: "things"
  - other_stuff: 
      key: "value"

Anchors and Aliases

Anchors are identified by an & and alises by *

Anchors essentially are the process of setting a variable and alises are the process of using it

so &name Eric will set the name to Eric and later can be accesses as *name

Inject Alias

To save references to alias use >>

default: &default
  school: hogwarts
 
harry:
  <<: *default
  house: gryffindor
 
draco:
  <<: *default
  house: slytherin

Merge Multiple Maps

name: &harry_name
  first_name: Harry
  last_name: Potter
 
address: &harry_address
  street: 4, Privet Drive
  district: Little Whinging
  county: Surrey
  country: England
 
harry_data:
  <<: [*harry_name, *harry_address]

Note: Its is possible to merge maps, but not sequences

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