#
ドキュメント

Document

自分のための備忘録です。

YAML

Ref

YAMLとJSON

  • YAMLJSONのスーパーセット
  • JSONでできることはYAMLでできる

基本

The basic structure of a YAML file is a hash map and consists of one or more key-value pairs.

https://circleci.com/docs/2.0/writing-yaml/

key:value

基本サンプル(YAMLとJSON)

employees:
  - yamada
  - tanaka
  - sato
{
  "employees": [
    "yamada", 
    "tanaka", 
    "sato"
  ]
}
employees:
  - employee1: '0001' # ''で囲まないと0001 は 1と解釈
    name: yamada
  - employee2: '0002'
    name: tanaka
  - employee3: '0003'
    name: sato
{
  "employees": [
    {
      "employee1": "0001", 
      "name": "yamada"
    }, 
    {
      "employee2": "0002", 
      "name": "tanaka"
    }, 
    {
      "employee3": "0003", 
      "name": "sato"
    }
  ]
}

複数行

  • >または|を使用

複数行サンプル

引用元:https://qiita.com/jerrywdlee/items/d5d31c10617ec7342d56

foo: |
  bar
  baz
{
  "foo": "bar\nbaz"
}
foo: >
  bar
  baz
{
  "foo": "bar baz"
}

アンカー&エイリアス(Anchors and alias)

To DRY up your config.yml, use anchors and aliases. Anchors are identified by an & character, and aliases by an * character.

https://circleci.com/docs/2.0/writing-yaml/

song:
  - &name Al
  - You
  - can
  - call
  - me
  - *name
song:
  - Al
  - You
  - can
  - call
  - me
  - Al

サンプル

引用:https://circleci.com/docs/2.0/writing-yaml/

default: &default
  school: hogwarts

harry:
  <<: *default
  house: gryffindor

draco:
  <<: *default
{
  "default": {
    "school": "hogwarts"
  }, 
  "draco": {
    "house": "slytherin", 
    "school": "hogwarts"
  }, 
  "harry": {
    "house": "gryffindor", 
    "school": "hogwarts"
  }
}
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]
{
  "harry_data": {
    "county": "Surrey", 
    "first_name": "Harry", 
    "last_name": "Potter", 
    "street": "4, Privet Drive", 
    "district": "Little Whinging", 
    "country": "England"
  }, 
  "name": {
    "first_name": "Harry", 
    "last_name": "Potter"
  }, 
  "address": {
    "county": "Surrey", 
    "country": "England", 
    "street": "4, Privet Drive", 
    "district": "Little Whinging"
  }
}

Yaml to JSON サンプル

例1

x: "x"
y: "y"
z:
 - zX: 'zX'
   zY: 'zY'
 - zX: 'zX'
   zY: 'zY'
{
  "x": "x",
  "y": "y",
  "z": [
    {
      "zX": "zX",
      "zY": "zY"
    },
    {
      "zX": "zX",
      "zY": "zY"
    }
  ]
}

例2

vertexes:
  num: 15
  digit: 3
  nodes:
    - name: "a"
      position:
        x: 10
        y: 20
        z: 30
      color: "red"
    - name: "b"
      position:
        x: 15
        y: 8
        z: 30
      color: "blue"
{
  "vertexes": {
    "num": 15,
    "digit": 3,
    "nodes": [
      {
        "name": "a",
        "position": {
          "x": 10,
          "y": 20,
          "z": 30
        },
        "color": "red"
      },
      {
        "name": "b",
        "position": {
          "x": 15,
          "y": 8,
          "z": 30
        },
        "color": "blue"
      }
    ]
  }
}