mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-14 04:25:36 +00:00
auto update docs poc
This commit is contained in:
parent
ab732e0cc6
commit
a178934061
@ -1,12 +1,10 @@
|
||||
# Operators
|
||||
|
||||
## Operators
|
||||
# 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
|
||||
## Example with a simple operator
|
||||
|
||||
Given a document like:
|
||||
|
||||
@ -54,9 +52,9 @@ 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`.
|
||||
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
|
||||
It pipes the current, lets call it 'root' context through the `lhs` expression of `.a` to return the node
|
||||
|
||||
```yaml
|
||||
cat
|
||||
@ -78,7 +76,6 @@ 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.
|
||||
@ -106,6 +103,7 @@ It pipes the current context (the whole document) through the LHS expression of
|
||||
|
||||
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
|
||||
```
|
||||
@ -115,4 +113,4 @@ The assignment operator then copies across the value from the RHS to the value o
|
||||
```yaml
|
||||
a: 2
|
||||
b: thing
|
||||
```
|
||||
```
|
@ -3,102 +3,73 @@
|
||||
This operator is used to provide alternative (or default) values when a particular expression is either null or false.
|
||||
|
||||
## LHS is defined
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a: bridge
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a // "hello"' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
bridge
|
||||
```
|
||||
|
||||
## LHS is not defined
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
{}
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a // "hello"' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
hello
|
||||
```
|
||||
|
||||
## LHS is null
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a: ~
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a // "hello"' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
hello
|
||||
```
|
||||
|
||||
## LHS is false
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a: false
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a // "hello"' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
hello
|
||||
```
|
||||
|
||||
## RHS is an expression
|
||||
|
||||
Given a sample.yml file of:
|
||||
|
||||
```yaml
|
||||
a: false
|
||||
b: cat
|
||||
```
|
||||
|
||||
then
|
||||
|
||||
```bash
|
||||
yq eval '.a // .b' sample.yml
|
||||
```
|
||||
|
||||
will output
|
||||
|
||||
```yaml
|
||||
cat
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user