Skip to content

Commit

Permalink
Updating style docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 8, 2024
1 parent bf2bc29 commit 24bd563
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 24 deletions.
4 changes: 3 additions & 1 deletion pkg/yqlib/doc/operators/headers/style.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 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.

103 changes: 97 additions & 6 deletions pkg/yqlib/doc/operators/style.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 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)
Given a sample.yml file of:
Expand Down Expand Up @@ -45,6 +47,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
Expand All @@ -57,6 +65,12 @@ a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
f: !!seq
- !!int 1
- !!int 2
- !!int 3
g: !!map
something: !!str cool
```
## Set double quote style
Expand All @@ -66,6 +80,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
Expand All @@ -77,6 +97,12 @@ a: "cat"
b: "5"
c: "3.2"
e: "true"
f:
- "1"
- "2"
- "3"
g:
something: "cool"
```
## Set double quote style on map keys too
Expand All @@ -86,6 +112,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
Expand All @@ -97,6 +129,12 @@ will output
"b": "5"
"c": "3.2"
"e": "true"
"f":
- "1"
- "2"
- "3"
"g":
"something": "cool"
```
## Set single quote style
Expand All @@ -106,6 +144,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
Expand All @@ -117,6 +161,12 @@ a: 'cat'
b: '5'
c: '3.2'
e: 'true'
f:
- '1'
- '2'
- '3'
g:
something: 'cool'
```
## Set literal quote style
Expand All @@ -126,6 +176,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
Expand All @@ -141,6 +197,16 @@ c: |-
3.2
e: |-
true
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
cool
```
## Set folded quote style
Expand All @@ -150,6 +216,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
Expand All @@ -165,6 +237,16 @@ c: >-
3.2
e: >-
true
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
cool
```
## Set flow quote style
Expand All @@ -174,25 +256,28 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
then
```bash
yq '.. style="flow"' sample.yml
```
will output
```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
Set empty (default) quote style, note the usage of `...` to match keys too. Note that there is a `--prettyPrint/-P` short flag for this.

Given a sample.yml file of:
```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"} }
```
then
```bash
Expand All @@ -204,6 +289,12 @@ a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: cool
```
## Set style relatively with assign-update
Expand Down
55 changes: 38 additions & 17 deletions pkg/yqlib/operator_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ var styleOperatorScenarios = []expressionScenario{
},
{
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"`,
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",
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"`,
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",
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"`,
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",
},
},
{
Expand All @@ -55,15 +55,15 @@ var styleOperatorScenarios = []expressionScenario{
},
{
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"`,
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",
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"`,
expected: []string{
`D0, P[], (!!map)::a: |-
Expand All @@ -74,12 +74,22 @@ c: |-
3.2
e: |-
true
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
cool
`,
},
},
{
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"`,
expected: []string{
`D0, P[], (!!map)::a: >-
Expand All @@ -90,24 +100,35 @@ c: >-
3.2
e: >-
true
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
cool
`,
},
},
{
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"`,
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",
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}`,
expression: `... style=""`,
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.",
dontFormatInputForDoc: true,
document: `{a: cat, "b": 5, 'c': 3.2, "e": true, f: [1,2,3], "g": { something: "cool"} }`,
expression: `... style=""`,
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",
},
},
{
Expand Down

0 comments on commit 24bd563

Please sign in to comment.