「YAML」- YAML Ain’t Markup Language | 学习笔记

认识

官网:https://yaml.org/
文档:https://yaml.org/spec/

YAML is a human-friendly data serialization language for all programming languages. YAML™ (rhymes with “camel”) is a human-friendly, cross language, Unicode based data serialization language designed around the common native data structures of agile programming languages. It is broadly useful for programming needs ranging from configuration files to Internet messaging to object persistence to data auditing. Together with the Unicode standard for characters, the YAML specification provides all the information necessary to understand YAML Version 1.2 and to creating programs that process YAML information. 简而言之,我们将 YAML 视为 JSON 的另种形式。

性质

注释 Comment

其使用井号(#)注释行;

(1)支持单行注释;(2)允许行末注释;

apiVersion: v1
kind: Secret
# Specifying the name of the deployment
metadata:
  name: mysql-root-pass
type: Opaque
stringData:
  password: mysql123 # Define the commands you want to run in this script

不支持多行注释,若需要使用多行注释:

# The object below is an example that represents a
# user's name, phone number and age

文档开始符

---

多行字符串 Multiline Strings

syntax – How do I break a string in YAML over multiple lines? – Stack Overflow

在 YAML 中,其提供多种创建多行字符串的方法:

  • |, >
  • |-, >-
  • |+, >+

实际上,在具体应用中,我们仅涉及特定场景,所以该处仅列举我们常用的几种方式。

针对配置文件,需要保持“原样”,我们通常使用如下方式:

Key: |
  this is my very very very
  long string

→ this is my very very very\nlong string\n

针对多行文本,需要换行,所以我们通常使用如下方式:

Key: >
  this is my very very very
  long string

→ this is my very very very long string\n

数组

- elementl
- element2
- element3

字典、复杂字典

# 简单字典
key: value

# 复杂字典
martin:
  name: Martin Developer
  job: Developer
  skill: Elite

数组与字典嵌套

- martin:
    name: Martin D’ vloper
    job: Developer
    skills:
      - python
      - perl
      - pascal
- tabitha:
    name: Tabitha Bitumen
    job: Developer
    skills:
      - lisp
      - fortran
      - erlang

注意事项

1)如果在值中包含冒号(:),需要使用引号:foo: "bar: example"
2)如果值以花括号({)开始,需要使用引号:foo: "{barexample}"

Multi Documents

document: this is document 1
---
document: this is document 2

Anchors & Aliases

services:
  web:
    image: "nginx"
    environment: &env
      - DEBUG=true
      - API_VERSION=v1
  redis:
    image: "redis:alpine"
    environment: *env

# services.redis.environment 将复用(复制)services.web.environment 的取值

使用先前的配置,并对其进行覆盖:

services:
  redis01:
    image: "redis:alpine"
    healthcheck: &health-check
      test: ["CMD", "redis-cli", "ping"]
      timeout: 10s
      retries: 3
      start_interval: 11111h
  redis02:
    image: "redis:alpine"
    healthcheck:
      <<: *health-check
      start_interval: 22222h
      start_period: 10s

# 引用先前配置,
# 且将 start_interval: 11111h 重写为 start_interval: 22222h
# 并增加 start_period: 10s 配置

注意,YAML anchors and aliases (& and *) are primarily designed for reusing entire data structures (like maps or sequences) or scalar values, not for concatenating arrays in a way that merges their contents. When an alias references an anchored array, it essentially copies the entire array at that point in the document. 所以,其暂时无法用来合并数组。

参考

YAML – Wikipedia
YAML 快速入门
YAML Ain’t Markup Language (YAML™) Version 1.2 3rd Edition, Patched at 2009-10-01
JS-YAML 在线示例:http://nodeca.github.io/js-yaml
How do you do block comments in YAML?