This commit is contained in:
Christoph Stäbler 2022-05-24 10:18:27 +02:00 committed by GitHub
parent 451d8643ee
commit dee592eb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 27 additions and 27 deletions

View File

@ -47,7 +47,7 @@ b: dog
## Complex assignment, operator precedence rules ## Complex assignment, operator precedence rules
Just like math expression - `yq` expression have an order of precedence. The pipe `|` operator has a low order of precedence, so operators with higher precedence will get evalated first. Just like math expression - `yq` expression have an order of precedence. The pipe `|` operator has a low order of precedence, so operators with higher precedence will get evaluated first.
Most of the time, this is intuitively what you'd want, for instance `.a = "cat" | .b = "dog"` is effectively: `(.a = "cat") | (.b = "dog")`. Most of the time, this is intuitively what you'd want, for instance `.a = "cat" | .b = "dog"` is effectively: `(.a = "cat") | (.b = "dog")`.
@ -66,7 +66,7 @@ Lets say you had:
Lets say you wanted to update the `sally` entry to have fruit: 'mango'. The _incorrect_ way to do that is: Lets say you wanted to update the `sally` entry to have fruit: 'mango'. The _incorrect_ way to do that is:
`.[] | select(.name == "sally") | .fruit = "mango"`. `.[] | select(.name == "sally") | .fruit = "mango"`.
Becasue `|` has a low operator precedence, this will be evaluated (_incorrectly_) as : `(.[]) | (select(.name == "sally")) | (.fruit = "mango")`. What you'll see is only the updated segment returned: Because `|` has a low operator precedence, this will be evaluated (_incorrectly_) as : `(.[]) | (select(.name == "sally")) | (.fruit = "mango")`. What you'll see is only the updated segment returned:
```yaml ```yaml
name: sally name: sally

View File

@ -21,7 +21,7 @@ type CandidateNode struct {
Filename string Filename string
FileIndex int FileIndex int
// when performing op against all nodes given, this will treat all the nodes as one // when performing op against all nodes given, this will treat all the nodes as one
// (e.g. top level cross document merge). This property does not propegate to child nodes. // (e.g. top level cross document merge). This property does not propagate to child nodes.
EvaluateTogether bool EvaluateTogether bool
IsMapKey bool IsMapKey bool
} }

View File

@ -1,6 +1,6 @@
# Anchor and Alias Operators # Anchor and Alias Operators
Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalises a yaml file (dereference (or expands) aliases and remove anchor names). Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalizes a yaml file (dereference (or expands) aliases and remove anchor names).
`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag. `yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag.

View File

@ -107,13 +107,13 @@ c: fieldC
``` ```
then then
```bash ```bash
yq '(.a, .c) = "potatoe"' sample.yml yq '(.a, .c) = "potato"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: potatoe a: potato
b: fieldB b: fieldB
c: potatoe c: potato
``` ```
## Update string value ## Update string value
@ -151,7 +151,7 @@ a:
``` ```
## Update deeply selected results ## Update deeply selected results
Note that the LHS is wrapped in brackets! This is to ensure we dont first filter out the yaml and then update the snippet. Note that the LHS is wrapped in brackets! This is to ensure we don't first filter out the yaml and then update the snippet.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -4,7 +4,7 @@ Encode operators will take the piped in object structure and encode it as a stri
Note that you can optionally pass an indent value to the encode functions (see below). Note that you can optionally pass an indent value to the encode functions (see below).
These operators are useful to process yaml documents that have stringified embeded yaml/json/props in them. These operators are useful to process yaml documents that have stringified embedded yaml/json/props in them.
| Format | Decode (from string) | Encode (to string) | | Format | Decode (from string) | Encode (to string) |

View File

@ -81,7 +81,7 @@ true
false false
``` ```
## Dont match number ## Don't match number
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
- 3 - 3

View File

@ -4,7 +4,7 @@ Use `eval` to dynamically process an expression - for instance from an environme
`eval` takes a single argument, and evaluates that as a `yq` expression. Any valid expression can be used, beit a path `.a.b.c | select(. == "cat")`, or an update `.a.b.c = "gogo"`. `eval` takes a single argument, and evaluates that as a `yq` expression. Any valid expression can be used, beit a path `.a.b.c | select(. == "cat")`, or an update `.a.b.c = "gogo"`.
Tip: This can be useful way parameterise complex scripts. Tip: This can be useful way parameterize complex scripts.
{% hint style="warning" %} {% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20; Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;

View File

@ -1,6 +1,6 @@
# Anchor and Alias Operators # Anchor and Alias Operators
Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalises a yaml file (dereference (or expands) aliases and remove anchor names). Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalizes a yaml file (dereference (or expands) aliases and remove anchor names).
`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag. `yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag.

View File

@ -4,7 +4,7 @@ Encode operators will take the piped in object structure and encode it as a stri
Note that you can optionally pass an indent value to the encode functions (see below). Note that you can optionally pass an indent value to the encode functions (see below).
These operators are useful to process yaml documents that have stringified embeded yaml/json/props in them. These operators are useful to process yaml documents that have stringified embedded yaml/json/props in them.
| Format | Decode (from string) | Encode (to string) | | Format | Decode (from string) | Encode (to string) |

View File

@ -4,4 +4,4 @@ Use `eval` to dynamically process an expression - for instance from an environme
`eval` takes a single argument, and evaluates that as a `yq` expression. Any valid expression can be used, beit a path `.a.b.c | select(. == "cat")`, or an update `.a.b.c = "gogo"`. `eval` takes a single argument, and evaluates that as a `yq` expression. Any valid expression can be used, beit a path `.a.b.c | select(. == "cat")`, or an update `.a.b.c = "gogo"`.
Tip: This can be useful way parameterise complex scripts. Tip: This can be useful way parameterize complex scripts.

View File

@ -19,4 +19,4 @@ On the RHS there is `<init>`, the starting value of the accumulator and `<block>
## yq vs jq syntax ## yq vs jq syntax
Reduce syntax in `yq` is a little different from `jq` - as `yq` (currently) isn't as sophisticated as `jq` and its only supports infix notation (e.g. a + b, where the operator is in the middle of the two parameters) - where as `jq` uses a mix of infix notation with _prefix_ notation (e.g. `reduce a b` is like writing `+ a b`). Reduce syntax in `yq` is a little different from `jq` - as `yq` (currently) isn't as sophisticated as `jq` and its only supports infix notation (e.g. a + b, where the operator is in the middle of the two parameters) - where as `jq` uses a mix of infix notation with _prefix_ notation (e.g. `reduce a b` is like writing `+ a b`).
To that end, the reduce operator is called `ireduce` for backwards compatability if a `jq` like prefix version of `reduce` is ever added. To that end, the reduce operator is called `ireduce` for backwards compatibility if a `jq` like prefix version of `reduce` is ever added.

View File

@ -19,7 +19,7 @@ On the RHS there is `<init>`, the starting value of the accumulator and `<block>
## yq vs jq syntax ## yq vs jq syntax
Reduce syntax in `yq` is a little different from `jq` - as `yq` (currently) isn't as sophisticated as `jq` and its only supports infix notation (e.g. a + b, where the operator is in the middle of the two parameters) - where as `jq` uses a mix of infix notation with _prefix_ notation (e.g. `reduce a b` is like writing `+ a b`). Reduce syntax in `yq` is a little different from `jq` - as `yq` (currently) isn't as sophisticated as `jq` and its only supports infix notation (e.g. a + b, where the operator is in the middle of the two parameters) - where as `jq` uses a mix of infix notation with _prefix_ notation (e.g. `reduce a b` is like writing `+ a b`).
To that end, the reduce operator is called `ireduce` for backwards compatability if a `jq` like prefix version of `reduce` is ever added. To that end, the reduce operator is called `ireduce` for backwards compatibility if a `jq` like prefix version of `reduce` is ever added.
{% hint style="warning" %} {% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20; Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;

View File

@ -266,7 +266,7 @@ will output
``` ```
## Test using regex ## Test using regex
Like jq'q equivalent, this works like match but only returns true/false instead of full match details Like jq's equivalent, this works like match but only returns true/false instead of full match details
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -46,7 +46,7 @@ Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicB
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
"posts": "posts":
- "title": Frist psot - "title": First post
"author": anon "author": anon
- "title": A well-written article - "title": A well-written article
"author": person1 "author": person1
@ -60,7 +60,7 @@ yq '.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]
``` ```
will output will output
```yaml ```yaml
title: Frist psot title: First post
author: Anonymous Coward author: Anonymous Coward
title: A well-written article title: A well-written article
author: Person McPherson author: Person McPherson

View File

@ -101,9 +101,9 @@ var assignOperatorScenarios = []expressionScenario{
{ {
description: "Updated multiple paths", description: "Updated multiple paths",
document: `{a: fieldA, b: fieldB, c: fieldC}`, document: `{a: fieldA, b: fieldB, c: fieldC}`,
expression: `(.a, .c) = "potatoe"`, expression: `(.a, .c) = "potato"`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{a: potatoe, b: fieldB, c: potatoe}\n", "D0, P[], (doc)::{a: potato, b: fieldB, c: potato}\n",
}, },
}, },
{ {
@ -149,7 +149,7 @@ var assignOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Update deeply selected results", description: "Update deeply selected results",
subdescription: "Note that the LHS is wrapped in brackets! This is to ensure we dont first filter out the yaml and then update the snippet.", subdescription: "Note that the LHS is wrapped in brackets! This is to ensure we don't first filter out the yaml and then update the snippet.",
document: `{a: {b: apple, c: cactus}}`, document: `{a: {b: apple, c: cactus}}`,
expression: `(.a[] | select(. == "apple")) = "frog"`, expression: `(.a[] | select(. == "apple")) = "frog"`,
expected: []string{ expected: []string{

View File

@ -130,7 +130,7 @@ var equalsOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
description: "Dont match number", description: "Don't match number",
document: `[3, 4, 5]`, document: `[3, 4, 5]`,
expression: `.[] | (. != 4)`, expression: `.[] | (. != 4)`,
expected: []string{ expected: []string{
@ -165,7 +165,7 @@ var equalsOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
description: "Non exisitant key doesn't equal a value", description: "Non existent key doesn't equal a value",
document: "a: frog", document: "a: frog",
expression: `select(.b != "thing")`, expression: `select(.b != "thing")`,
expected: []string{ expected: []string{

View File

@ -165,7 +165,7 @@ var stringsOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Test using regex", description: "Test using regex",
subdescription: "Like jq'q equivalent, this works like match but only returns true/false instead of full match details", subdescription: "Like jq's equivalent, this works like match but only returns true/false instead of full match details",
document: `["cat", "dog"]`, document: `["cat", "dog"]`,
expression: `.[] | test("at")`, expression: `.[] | test("at")`,
expected: []string{ expected: []string{

View File

@ -39,13 +39,13 @@ var variableOperatorScenarios = []expressionScenario{
{ {
description: "Using variables as a lookup", description: "Using variables as a lookup",
subdescription: "Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...)", subdescription: "Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...)",
document: `{"posts": [{"title": "Frist psot", "author": "anon"}, document: `{"posts": [{"title": "First post", "author": "anon"},
{"title": "A well-written article", "author": "person1"}], {"title": "A well-written article", "author": "person1"}],
"realnames": {"anon": "Anonymous Coward", "realnames": {"anon": "Anonymous Coward",
"person1": "Person McPherson"}}`, "person1": "Person McPherson"}}`,
expression: `.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}`, expression: `.realnames as $names | .posts[] | {"title":.title, "author": $names[.author]}`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::title: \"Frist psot\"\nauthor: \"Anonymous Coward\"\n", "D0, P[], (!!map)::title: \"First post\"\nauthor: \"Anonymous Coward\"\n",
"D0, P[], (!!map)::title: \"A well-written article\"\nauthor: \"Person McPherson\"\n", "D0, P[], (!!map)::title: \"A well-written article\"\nauthor: \"Person McPherson\"\n",
}, },
}, },