diff --git a/pkg/yqlib/doc/operators/headers/style.md b/pkg/yqlib/doc/operators/headers/style.md index c31c328e..2042db75 100644 --- a/pkg/yqlib/doc/operators/headers/style.md +++ b/pkg/yqlib/doc/operators/headers/style.md @@ -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. + diff --git a/pkg/yqlib/doc/operators/style.md b/pkg/yqlib/doc/operators/style.md index b21a5b26..7ab4ef1e 100644 --- a/pkg/yqlib/doc/operators/style.md +++ b/pkg/yqlib/doc/operators/style.md @@ -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: @@ -45,6 +47,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -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 @@ -66,6 +80,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -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 @@ -86,6 +112,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -97,6 +129,12 @@ will output "b": "5" "c": "3.2" "e": "true" +"f": + - "1" + - "2" + - "3" +"g": + "something": "cool" ``` ## Set single quote style @@ -106,6 +144,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -117,6 +161,12 @@ a: 'cat' b: '5' c: '3.2' e: 'true' +f: + - '1' + - '2' + - '3' +g: + something: 'cool' ``` ## Set literal quote style @@ -126,6 +176,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -141,6 +197,16 @@ c: |- 3.2 e: |- true +f: + - |- + 1 + - |- + 2 + - |- + 3 +g: + something: |- + cool ``` ## Set folded quote style @@ -150,6 +216,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -165,6 +237,16 @@ c: >- 3.2 e: >- true +f: + - >- + 1 + - >- + 2 + - >- + 3 +g: + something: >- + cool ``` ## Set flow quote style @@ -174,6 +256,12 @@ a: cat b: 5 c: 3.2 e: true +f: + - 1 + - 2 + - 3 +g: + something: cool ``` then ```bash @@ -181,7 +269,7 @@ 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 @@ -189,10 +277,7 @@ Set empty (default) quote style, note the usage of `...` to match keys too. Note 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 @@ -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 diff --git a/pkg/yqlib/operator_style_test.go b/pkg/yqlib/operator_style_test.go index 9077f9a2..e20e93f2 100644 --- a/pkg/yqlib/operator_style_test.go +++ b/pkg/yqlib/operator_style_test.go @@ -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", }, }, { @@ -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: |- @@ -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: >- @@ -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", }, }, {