yq/operators
2021-10-30 15:52:05 +11:00
..
add.md try updating add 2021-10-30 14:23:02 +11:00
alternative-default-value.md auto update docs poc 2021-10-30 14:40:51 +11:00
anchor-and-alias-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
assign-update.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
boolean-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
collect-into-array.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
comment-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
contains.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
create-collect-into-object.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
delete.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
document-index.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
encode-decode.md Added encode / decode docs 2021-10-30 15:49:10 +11:00
entries.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
env-variable-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
equals.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
file-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
flatten.md Added flatten doc 2021-10-30 15:52:05 +11:00
group-by.md added group-by doc 2021-10-30 14:43:45 +11:00
has.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
keys.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
length.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
multiply-merge.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
path.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
pipe.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
README.md auto update docs poc 2021-10-30 14:40:51 +11:00
recursive-descent-glob.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
reduce.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
select.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
sort-keys.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
split-into-documents.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
string-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
style.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
subtract.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
tag.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
traverse-read.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
union.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
unique.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
variable-operators.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00
with.md GitBook: [#206] deep update example 2021-10-30 03:14:39 +00:00

How it works

In yq expressions are made up of operators and pipes. A context of nodes is passed through the expression and each operation takes the context as input and returns a new context as output. That output is piped in as input for the next operation in the expression. To begin with, the context is set to the first yaml document of the first yaml file (if processing in sequence using eval).

Lets look at a couple of examples.

Example with a simple operator

Given a document like:

- [a]
- "cat"

with an expression:

.[] | length

yq will initially set the context as single node of the entire yaml document, an array of two elements.

- [a]
- "cat"

This gets piped into the splat operator .[] which will split out the context into a collection of two nodes [a] and "cat". Note that this is not a yaml array.

The length operator take no arguments, and will simply return the length of each matching node in the context. So for the context of [a] and "cat", it will return a new context of 1 and 3.

This being the last operation in the expression, the results will be printed out:

1
3

Example with an operator that takes arguments.

Given a document like:

a: cat
b: dog

with an expression:

.a = .b

The = operator takes two arguments, a lhs expression, which in this case is .a and rhs expression which is .b.

It pipes the current, lets call it 'root' context through the lhs expression of .a to return the node

cat

Note that this node holds not only its value 'cat', but comments and metadata too, including path and parent information.

The = operator then pipes the 'root' context through the rhs expression of .b to return the node

dog

Both sides have now been evaluated, so now the operator copies across the value from the RHS to the value on the LHS, and it returns the now updated context:

a: dog
b: dog

Relative update (e.g. |=)

There is another form of the = operator which we call the relative form. It's very similar to = but with one key difference when evaluating the RHS expression.

In the plain form, we pass in the 'root' level context to the RHS expression. In relative form, we pass in each result of the LHS to the RHS expression. Let's go through an example.

Given a document like:

a: 1
b: thing

with an expression:

.a |= . + 1

Similar to the = operator, |= takes two operands, the LHS and RHS.

It pipes the current context (the whole document) through the LHS expression of .a to get the node value:

1

Now it pipes that LHS context into the RHS expression . + 1 (whereas in the = plain form it piped the original document context into the RHS) to yield:

2

The assignment operator then copies across the value from the RHS to the value on the LHS, and it returns the now updated 'root' context:

a: 2
b: thing