diff --git a/pkg/yqlib/doc/Style Operator.md b/pkg/yqlib/doc/Style Operator.md index 1d0b38fd..f0080286 100644 --- a/pkg/yqlib/doc/Style Operator.md +++ b/pkg/yqlib/doc/Style Operator.md @@ -1,17 +1,149 @@ - +The style operator can be used to get or set the style of nodes (e.g. string style, yaml style) ## Examples -### Example 0 +### Set tagged style Given a sample.yml file of: ```yaml a: cat +b: 5 +c: 3.2 +e: true ``` then ```bash -yq eval '.a style="single"' sample.yml +yq eval '.. style="tagged"' sample.yml ``` will output ```yaml -{a: 'cat'} +!!map +a: !!str cat +b: !!int 5 +c: !!float 3.2 +e: !!bool true +``` + +### Set double quote style +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +``` +then +```bash +yq eval '.. style="double"' sample.yml +``` +will output +```yaml +a: "cat" +b: "5" +c: "3.2" +e: "true" +``` + +### Set single quote style +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +``` +then +```bash +yq eval '.. style="single"' sample.yml +``` +will output +```yaml +a: 'cat' +b: '5' +c: '3.2' +e: 'true' +``` + +### Set literal quote style +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +``` +then +```bash +yq eval '.. style="literal"' sample.yml +``` +will output +```yaml +a: |- + cat +b: |- + 5 +c: |- + 3.2 +e: |- + true +``` + +### Set folded quote style +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +``` +then +```bash +yq eval '.. style="folded"' sample.yml +``` +will output +```yaml +a: >- + cat +b: >- + 5 +c: >- + 3.2 +e: >- + true +``` + +### Set flow quote style +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +``` +then +```bash +yq eval '.. style="flow"' sample.yml +``` +will output +```yaml +{a: cat, b: 5, c: 3.2, e: true} +``` + +### Set empty (default) quote style +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +``` +then +```bash +yq eval '.. style=""' sample.yml +``` +will output +```yaml +a: cat +b: 5 +c: 3.2 +e: true ``` ### Set style using a path @@ -26,10 +158,11 @@ yq eval '.a style=.b' sample.yml ``` will output ```yaml -{a: "cat", b: double} +a: "cat" +b: double ``` -### Example 2 +### Example 8 Given a sample.yml file of: ```yaml a: cat @@ -45,7 +178,7 @@ a: cat b: dog ``` -### Example 3 +### Example 9 Given a sample.yml file of: ```yaml a: cat @@ -57,12 +190,12 @@ yq eval '.. | style' sample.yml ``` will output ```yaml -flow -double -single + + + ``` -### Example 4 +### Example 10 Given a sample.yml file of: ```yaml a: cat diff --git a/pkg/yqlib/doc/headers/Style Operator.md b/pkg/yqlib/doc/headers/Style Operator.md new file mode 100644 index 00000000..08fc4d23 --- /dev/null +++ b/pkg/yqlib/doc/headers/Style Operator.md @@ -0,0 +1 @@ +The style operator can be used to get or set the style of nodes (e.g. string style, yaml style) \ No newline at end of file diff --git a/pkg/yqlib/operatory_style.go b/pkg/yqlib/operator_style.go similarity index 100% rename from pkg/yqlib/operatory_style.go rename to pkg/yqlib/operator_style.go diff --git a/pkg/yqlib/operator_style_test.go b/pkg/yqlib/operator_style_test.go new file mode 100644 index 00000000..1bdaf983 --- /dev/null +++ b/pkg/yqlib/operator_style_test.go @@ -0,0 +1,98 @@ +package yqlib + +import ( + "testing" +) + +var styleOperatorScenarios = []expressionScenario{ + { + description: "Set tagged style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style="tagged"`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + description: "Set double quote style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style="double"`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + description: "Set single quote style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style="single"`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + description: "Set literal quote style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style="literal"`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + description: "Set folded quote style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style="folded"`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + description: "Set flow quote style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style="flow"`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + description: "Set empty (default) quote style", + document: `{a: cat, b: 5, c: 3.2, e: true}`, + expression: `.. style=""`, + expected: []string{ + "D0, P[], (doc)::{a: 'cat'}\n", + }, + }, + { + skipDoc: true, + document: `{a: cat, b: double}`, + expression: `.a style=.b`, + expected: []string{ + "D0, P[], (doc)::{a: \"cat\", b: double}\n", + }, + }, + { + description: "Read style", + document: `{a: "cat", b: 'thing'}`, + expression: `.. | style`, + expected: []string{ + "D0, P[], (!!str)::flow\n", + "D0, P[a], (!!str)::double\n", + "D0, P[b], (!!str)::single\n", + }, + }, + { + skipDoc: true, + document: `a: cat`, + expression: `.. | style`, + expected: []string{ + "D0, P[], (!!str)::\"\"\n", + "D0, P[a], (!!str)::\"\"\n", + }, + }, +} + +func TestStyleOperatorScenarios(t *testing.T) { + for _, tt := range styleOperatorScenarios { + testScenario(t, &tt) + } + documentScenarios(t, "Style Operator", styleOperatorScenarios) +} diff --git a/pkg/yqlib/operatory_style_test.go b/pkg/yqlib/operatory_style_test.go deleted file mode 100644 index 28909899..00000000 --- a/pkg/yqlib/operatory_style_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package yqlib - -import ( - "testing" -) - -var styleOperatorScenarios = []expressionScenario{ - { - document: `{a: cat}`, - expression: `.a style="single"`, - expected: []string{ - "D0, P[], (doc)::{a: 'cat'}\n", - }, - }, - { - description: "Set style using a path", - document: `{a: cat, b: double}`, - expression: `.a style=.b`, - expected: []string{ - "D0, P[], (doc)::{a: \"cat\", b: double}\n", - }, - }, - { - document: `{a: "cat", b: 'dog'}`, - expression: `.. style=""`, - expected: []string{ - "D0, P[], (!!map)::a: cat\nb: dog\n", - }, - }, - { - document: `{a: "cat", b: 'thing'}`, - expression: `.. | style`, - expected: []string{ - "D0, P[], (!!str)::flow\n", - "D0, P[a], (!!str)::double\n", - "D0, P[b], (!!str)::single\n", - }, - }, - { - document: `a: cat`, - expression: `.. | style`, - expected: []string{ - "D0, P[], (!!str)::\"\"\n", - "D0, P[a], (!!str)::\"\"\n", - }, - }, -} - -func TestStyleOperatorScenarios(t *testing.T) { - for _, tt := range styleOperatorScenarios { - testScenario(t, &tt) - } - documentScenarios(t, "Style Operator", styleOperatorScenarios) -}