From 4a8c84d1f8a1520466738d18515934678d9d97f9 Mon Sep 17 00:00:00 2001 From: FireIsGood Date: Wed, 22 May 2024 03:36:40 -0700 Subject: [PATCH] fix: yaml format Added some ignores --- source/_posts/yaml.md | 247 +++++++++++++++++++++--------------------- 1 file changed, 126 insertions(+), 121 deletions(-) diff --git a/source/_posts/yaml.md b/source/_posts/yaml.md index 41561bf..8f7ed0c 100644 --- a/source/_posts/yaml.md +++ b/source/_posts/yaml.md @@ -3,19 +3,17 @@ title: YAML date: 2020-12-29 18:26:55 background: bg-[#b42e28] tags: - - config - - format + - config + - format categories: - - Programming + - Programming intro: | - This is a quick reference cheat sheet for understanding and writing YAML format configuration files. + This is a quick reference cheat sheet for understanding and writing YAML format configuration files. plugins: - - copyCode + - copyCode --- -Getting Started ---------------- - +## Getting Started ### Introduction @@ -26,24 +24,26 @@ Getting Started - YAML is CASE sensitive - End your YAML file with the `.yaml` or `.yml` extension - YAML is a superset of JSON -- Ansible playbooks are YAML files -{.marker-round} - +- Ansible playbooks are YAML files {.marker-round} ### Scalar types {.row-span-2} + + ```yaml -n1: 1 # integer -n2: 1.234 # float +n1: 1 # integer +n2: 1.234 # float -s1: 'abc' # string -s2: "abc" # string -s3: abc # string +s1: 'abc' # string +s2: "abc" # string +s3: abc # string -b: false # boolean type +b: false # boolean type d: 2015-04-05 # date type ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "n1": 1, @@ -55,15 +55,19 @@ d: 2015-04-05 # date type "d": "2015-04-05" } ``` + Use spaces to indent. There must be space between the element parts. - ### Variables + ```yaml some_thing: &VAR_NAME foobar other_thing: *VAR_NAME ``` + #### ↓ Equivalent JSON + + ```json {.wrap} { "some_thing": "foobar", @@ -71,10 +75,8 @@ other_thing: *VAR_NAME } ``` - - - ### Comments + ```yaml # A single line comment example @@ -84,22 +86,22 @@ other_thing: *VAR_NAME # comment line 3 ``` - - ### Multiline strings + ```yaml description: | hello world ``` + #### ↓ Equivalent JSON + ```json {.wrap} -{"description": "hello\nworld\n"} +{ "description": "hello\nworld\n" } ``` - - ### Inheritance {.row-span-2} + ```yaml parent: &defaults a: 2 @@ -109,7 +111,9 @@ child: <<: *defaults b: 4 ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "parent": { @@ -123,18 +127,20 @@ child: } ``` - - ### Reference {.row-span-2} + ```yaml values: &ref - Will be - reused below - + other_values: i_am_ref: *ref ``` + #### ↓ Equivalent JSON + + ```json {.wrap} { "values": [ @@ -150,44 +156,43 @@ other_values: } ``` - ### Folded strings + ```yaml description: > - hello - world + hello world ``` + #### ↓ Equivalent JSON + ```json {.wrap} -{"description": "hello world\n"} +{ "description": "hello world\n" } ``` - - ### Two Documents + ```yaml --- document: this is doc 1 --- document: this is doc 2 ``` + YAML uses `---` to separate directives from document content. - - - - - -YAML Collections ------------ +## YAML Collections ### Sequence + ```yaml - Mark McGwire - Sammy Sosa - Ken Griffey ``` + #### ↓ Equivalent JSON + + ```json {.wrap} [ "Mark McGwire", @@ -196,14 +201,17 @@ YAML Collections ] ``` - ### Mapping + + ```yaml hr: 65 # Home runs avg: 0.278 # Batting average rbi: 147 # Runs Batted In ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "hr": 65, @@ -212,16 +220,17 @@ rbi: 147 # Runs Batted In } ``` - - ### Mapping to Sequences + ```yaml attributes: - a1 - a2 methods: [getter, setter] ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "attributes": ["a1", "a2"], @@ -229,11 +238,9 @@ methods: [getter, setter] } ``` - - - - ### Sequence of Mappings + + ```yaml children: - name: Jimmy Smith @@ -244,7 +251,10 @@ children: name: Sammy Sosa age: 12 ``` + #### ↓ Equivalent JSON + + ```json {.wrap} { "children": [ @@ -255,19 +265,22 @@ children: } ``` - ### Sequence of Sequences + + ```yaml my_sequences: - [1, 2, 3] - [4, 5, 6] - - + - - 7 - 8 - 9 - 0 ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "my_sequences": [ @@ -278,16 +291,15 @@ my_sequences: } ``` - ### Mapping of Mappings + ```yaml -Mark McGwire: {hr: 65, avg: 0.278} -Sammy Sosa: { - hr: 63, - avg: 0.288 - } +Mark McGwire: { hr: 65, avg: 0.278 } +Sammy Sosa: { hr: 63, avg: 0.288 } ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "Mark McGwire": { @@ -301,9 +313,8 @@ Sammy Sosa: { } ``` - - ### Nested Collections + ```yaml Jack: id: 1 @@ -312,9 +323,11 @@ Jack: hobby: - a - b - location: {country: "A", city: "A-A"} + location: { country: "A", city: "A-A" } ``` + #### ↓ Equivalent JSON + ```json {.wrap} { "Jack": { @@ -323,80 +336,77 @@ Jack: "salary": 25000, "hobby": ["a", "b"], "location": { - "country": "A", "city": "A-A" + "country": "A", + "city": "A-A" } } } ``` - ### Unordered Sets + ```yaml set1: !!set ? one ? two -set2: !!set {'one', "two"} +set2: !!set { "one", "two" } ``` + #### ↓ Equivalent JSON + ```json {.wrap} { - "set1": {"one": null, "two": null}, - "set2": {"one": null, "two": null} + "set1": { "one": null, "two": null }, + "set2": { "one": null, "two": null } } ``` + Sets are represented as a Mapping where each key is associated with a null value - ### Ordered Mappings + ```yaml ordered: !!omap -- Mark McGwire: 65 -- Sammy Sosa: 63 -- Ken Griffy: 58 + - Mark McGwire: 65 + - Sammy Sosa: 63 + - Ken Griffy: 58 ``` + #### ↓ Equivalent JSON + + ```json {.wrap} { "ordered": [ - {"Mark McGwire": 65}, - {"Sammy Sosa": 63}, - {"Ken Griffy": 58} + { "Mark McGwire": 65 }, + { "Sammy Sosa": 63 }, + { "Ken Griffy": 58 } ] } ``` - - - - - -YAML Reference --------------- +## YAML Reference ### Terms - Sequences aka arrays or lists - Scalars aka strings or numbers -- Mappings aka hashes or dictionaries -{.marker-round} +- Mappings aka hashes or dictionaries {.marker-round} Based on the YAML.org [refcard](https://yaml.org/refcard.html). - ### Document indicators | | | -|-------|---------------------| +| ----- | ------------------- | | `%` | Directive indicator | | `---` | Document header | | `...` | Document terminator | - - ### Collection indicators {.row-span-2} | | | -|------|---------------------------------| +| ---- | ------------------------------- | | `?` | Key indicator | | `:` | Value indicator | | `-` | Nested series entry indicator | @@ -404,39 +414,36 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). | `[]` | Surround in-line series branch | | `{}` | Surround in-line keyed branch | - ### Alias indicators | | | -|-----|-----------------| +| --- | --------------- | | `&` | Anchor property | | `*` | Alias indicator | - ### Special keys | | | -|------|---------------------------------| +| ---- | ------------------------------- | | `=` | Default "value" mapping key | | `<<` | Merge keys from another mapping | - ### Scalar indicators -| | | -|-------|----------------------------------------------------------------------------------------------| -| `''` | Surround in-line unescaped scalar | -| `"` | Surround in-line escaped scalar | -| `|` | Block scalar indicator | -| `>` | Folded scalar indicator | -| `-` | Strip chomp modifier (`|-` or `>-`) | -| `+` | Keep chomp modifier (`|+` or `>+`) | -| `1-9` | Explicit indentation modifier (`|1` or `>2`).
Modifiers can be combined (`|2-`, `>+1`) | - +| | | +| ----- | --------------------------------- | --------------------------------------------- | ----------- | +| `''` | Surround in-line unescaped scalar | +| `"` | Surround in-line escaped scalar | +| ` | ` | Block scalar indicator | +| `>` | Folded scalar indicator | +| `-` | Strip chomp modifier (` | -`or`>-`) | +| `+` | Keep chomp modifier (` | +`or`>+`) | +| `1-9` | Explicit indentation modifier (` | 1`or`>2`).
Modifiers can be combined (` | 2-`, `>+1`) | ### Tag Property (usually unspecified) {.col-span-2} + | | | -|----------|-------------------------------------------------------------| +| -------- | ----------------------------------------------------------- | | `none` | Unspecified tag (automatically resolved by application) | | `!` | Non-specific tag (by default, `!!map`/`!!seq`/`!!str`) | | `!foo` | Primary (by convention, means a local `!foo` tag) | @@ -446,22 +453,19 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). ### Misc indicators -| | | -|-----|-----------------------------| -| `#` | Throwaway comment indicator | +| | | +| ---------------- | ---------------------------- | +| `#` | Throwaway comment indicator | | \`@ | Both reserved for future use | - ### Core types (default automatic tags) {.row-span-2} | | | -|---------|------------------------------------------| +| ------- | ---------------------------------------- | | `!!map` | `{Hash table, dictionary, mapping}` | | `!!seq` | `{List, array, tuple, vector, sequence}` | | `!!str` | Unicode string | - - ### Escape Codes {.row-span-3} #### Numeric @@ -469,7 +473,8 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). - `\x12` (8-bit) - `\u1234` (16-bit) - `\U00102030` (32-bit) - {.cols-2 .marker-none} + +{.cols-2 .marker-none} #### Protective @@ -477,9 +482,11 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). - `\"` (") - `\ ` ( ) - `\` (TAB) - {.cols-3 .marker-none} + +{.cols-3 .marker-none} #### C + - `\0` (NUL) - `\a` (BEL) - `\b` (BS) @@ -488,7 +495,8 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). - `\r` (CR) - `\t` (TAB) - `\v` (VTAB) - {.cols-3 .marker-none} + +{.cols-3 .marker-none} #### Additional @@ -497,21 +505,20 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). - `\N` (NEL) - `\L` (LS) - `\P` (PS) - {.cols-3 .marker-none} - + +{.cols-3 .marker-none} + ### More types | | | -|----------|-----------------------------| +| -------- | --------------------------- | | `!!set` | `{cherries, plums, apples}` | | `!!omap` | `[one: 1, two: 2]` | - - ### Language Independent Scalar Types {.col-span-2} | | | -|---------------------------|--------------------------------------------| +| ------------------------- | ------------------------------------------ | | `{~, null}` | Null (no value). | | `[1234, 0x4D2, 02333]` | [Decimal int, Hexadecimal int, Octal int] | | `[1_230.15, 12.3015e+02]` | [Fixed float, Exponential float] | @@ -519,10 +526,8 @@ Based on the YAML.org [refcard](https://yaml.org/refcard.html). | `{Y, true, Yes, ON}` | Boolean true | | `{n, FALSE, No, off}` | Boolean false | +## Also see - -Also see --------- - [YAML Reference Card](https://yaml.org/refcard.html) _(yaml.org)_ - [Learn X in Y minutes](https://learnxinyminutes.com/docs/yaml/) _(learnxinyminutes.com)_ - [YAML lint online](http://www.yamllint.com/) _(yamllint.com)_