Updating style docs

This commit is contained in:
Mike Farah 2024-02-09 10:45:49 +11:00
parent bf2bc29e47
commit 24bd563680
3 changed files with 138 additions and 24 deletions

View File

@ -1,3 +1,5 @@
# Style # Style
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style) The style operator can be used to get or set the style of nodes (e.g. string style, yaml style).
Use this to control the formatting of the document in yaml.

View File

@ -1,6 +1,8 @@
# Style # Style
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style) The style operator can be used to get or set the style of nodes (e.g. string style, yaml style).
Use this to control the formatting of the document in yaml.
## Update and set style of a particular node (simple) ## Update and set style of a particular node (simple)
Given a sample.yml file of: Given a sample.yml file of:
@ -45,6 +47,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -57,6 +65,12 @@ a: !!str cat
b: !!int 5 b: !!int 5
c: !!float 3.2 c: !!float 3.2
e: !!bool true e: !!bool true
f: !!seq
- !!int 1
- !!int 2
- !!int 3
g: !!map
something: !!str cool
``` ```
## Set double quote style ## Set double quote style
@ -66,6 +80,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -77,6 +97,12 @@ a: "cat"
b: "5" b: "5"
c: "3.2" c: "3.2"
e: "true" e: "true"
f:
- "1"
- "2"
- "3"
g:
something: "cool"
``` ```
## Set double quote style on map keys too ## Set double quote style on map keys too
@ -86,6 +112,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -97,6 +129,12 @@ will output
"b": "5" "b": "5"
"c": "3.2" "c": "3.2"
"e": "true" "e": "true"
"f":
- "1"
- "2"
- "3"
"g":
"something": "cool"
``` ```
## Set single quote style ## Set single quote style
@ -106,6 +144,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -117,6 +161,12 @@ a: 'cat'
b: '5' b: '5'
c: '3.2' c: '3.2'
e: 'true' e: 'true'
f:
- '1'
- '2'
- '3'
g:
something: 'cool'
``` ```
## Set literal quote style ## Set literal quote style
@ -126,6 +176,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -141,6 +197,16 @@ c: |-
3.2 3.2
e: |- e: |-
true true
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
cool
``` ```
## Set folded quote style ## Set folded quote style
@ -150,6 +216,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -165,6 +237,16 @@ c: >-
3.2 3.2
e: >- e: >-
true true
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
cool
``` ```
## Set flow quote style ## Set flow quote style
@ -174,6 +256,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
then then
```bash ```bash
@ -181,7 +269,7 @@ yq '.. style="flow"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: cat, b: 5, c: 3.2, e: true} {a: cat, b: 5, c: 3.2, e: true, f: [1, 2, 3], g: {something: cool}}
``` ```
## Reset style - or pretty print ## Reset style - or pretty print
@ -189,10 +277,7 @@ Set empty (default) quote style, note the usage of `...` to match keys too. Note
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat {a: cat, "b": 5, 'c': 3.2, "e": true, f: [1,2,3], "g": { something: "cool"} }
"b": 5
'c': 3.2
"e": true
``` ```
then then
```bash ```bash
@ -204,6 +289,12 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
f:
- 1
- 2
- 3
g:
something: cool
``` ```
## Set style relatively with assign-update ## Set style relatively with assign-update

View File

@ -23,26 +23,26 @@ var styleOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Set tagged style", description: "Set tagged style",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `.. style="tagged"`, expression: `.. style="tagged"`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::!!map\na: !!str cat\nb: !!int 5\nc: !!float 3.2\ne: !!bool true\n", "D0, P[], (!!map)::!!map\na: !!str cat\nb: !!int 5\nc: !!float 3.2\ne: !!bool true\nf: !!seq\n - !!int 1\n - !!int 2\n - !!int 3\ng: !!map\n something: !!str cool\n",
}, },
}, },
{ {
description: "Set double quote style", description: "Set double quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `.. style="double"`, expression: `.. style="double"`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::a: \"cat\"\nb: \"5\"\nc: \"3.2\"\ne: \"true\"\n", "D0, P[], (!!map)::a: \"cat\"\nb: \"5\"\nc: \"3.2\"\ne: \"true\"\nf:\n - \"1\"\n - \"2\"\n - \"3\"\ng:\n something: \"cool\"\n",
}, },
}, },
{ {
description: "Set double quote style on map keys too", description: "Set double quote style on map keys too",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `... style="double"`, expression: `... style="double"`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::\"a\": \"cat\"\n\"b\": \"5\"\n\"c\": \"3.2\"\n\"e\": \"true\"\n", "D0, P[], (!!map)::\"a\": \"cat\"\n\"b\": \"5\"\n\"c\": \"3.2\"\n\"e\": \"true\"\n\"f\":\n - \"1\"\n - \"2\"\n - \"3\"\n\"g\":\n \"something\": \"cool\"\n",
}, },
}, },
{ {
@ -55,15 +55,15 @@ var styleOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Set single quote style", description: "Set single quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `.. style="single"`, expression: `.. style="single"`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::a: 'cat'\nb: '5'\nc: '3.2'\ne: 'true'\n", "D0, P[], (!!map)::a: 'cat'\nb: '5'\nc: '3.2'\ne: 'true'\nf:\n - '1'\n - '2'\n - '3'\ng:\n something: 'cool'\n",
}, },
}, },
{ {
description: "Set literal quote style", description: "Set literal quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `.. style="literal"`, expression: `.. style="literal"`,
expected: []string{ expected: []string{
`D0, P[], (!!map)::a: |- `D0, P[], (!!map)::a: |-
@ -74,12 +74,22 @@ c: |-
3.2 3.2
e: |- e: |-
true true
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
cool
`, `,
}, },
}, },
{ {
description: "Set folded quote style", description: "Set folded quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `.. style="folded"`, expression: `.. style="folded"`,
expected: []string{ expected: []string{
`D0, P[], (!!map)::a: >- `D0, P[], (!!map)::a: >-
@ -90,24 +100,35 @@ c: >-
3.2 3.2
e: >- e: >-
true true
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
cool
`, `,
}, },
}, },
{ {
description: "Set flow quote style", description: "Set flow quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true, f: [1,2,3], g: { something: cool}}`,
expression: `.. style="flow"`, expression: `.. style="flow"`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{a: cat, b: 5, c: 3.2, e: true}\n", "D0, P[], (!!map)::{a: cat, b: 5, c: 3.2, e: true, f: [1, 2, 3], g: {something: cool}}\n",
}, },
}, },
{ {
description: "Reset style - or pretty print", description: "Reset style - or pretty print",
subdescription: "Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.", subdescription: "Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.",
document: `{a: cat, "b": 5, 'c': 3.2, "e": true}`, dontFormatInputForDoc: true,
expression: `... style=""`, document: `{a: cat, "b": 5, 'c': 3.2, "e": true, f: [1,2,3], "g": { something: "cool"} }`,
expression: `... style=""`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::a: cat\nb: 5\nc: 3.2\ne: true\n", "D0, P[], (!!map)::a: cat\nb: 5\nc: 3.2\ne: true\nf:\n - 1\n - 2\n - 3\ng:\n something: cool\n",
}, },
}, },
{ {