1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00
yq/pkg/yqlib/doc/Boolean Operators.md
2021-05-14 14:29:55 +10:00

2.5 KiB

The or and and operators take two parameters and return a boolean result.

not flips a boolean from true to false, or vice versa.

any will return true if there are any true values in a array sequence, and all will return true if all elements in an array are true.

These are most commonly used with the select operator to filter particular nodes.

OR example

Running

yq eval --null-input 'true or false'

will output

true

AND example

Running

yq eval --null-input 'true and false'

will output

false

Matching nodes with select, equals and or

Given a sample.yml file of:

- a: bird
  b: dog
- a: frog
  b: bird
- a: cat
  b: fly

then

yq eval '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml

will output

- a: bird
  b: dog
- a: cat
  b: fly

ANY returns true if any boolean in a given array is true

Given a sample.yml file of:

- false
- true

then

yq eval 'any' sample.yml

will output

true

ANY returns true if any boolean in a given array is true

Given a sample.yml file of:

- false
- true

then

yq eval 'any' sample.yml

will output

true

ANY returns false for an empty array

Given a sample.yml file of:

[]

then

yq eval 'any' sample.yml

will output

false

ALL returns true if all booleans in a given array are true

Given a sample.yml file of:

- true
- true

then

yq eval 'all' sample.yml

will output

true

ANY returns true for an empty array

Given a sample.yml file of:

[]

then

yq eval 'all' sample.yml

will output

true

Not true is false

Running

yq eval --null-input 'true | not'

will output

false

Not false is true

Running

yq eval --null-input 'false | not'

will output

true

String values considered to be true

Running

yq eval --null-input '"cat" | not'

will output

false

Empty string value considered to be true

Running

yq eval --null-input '"" | not'

will output

false

Numbers are considered to be true

Running

yq eval --null-input '1 | not'

will output

false

Zero is considered to be true

Running

yq eval --null-input '0 | not'

will output

false

Null is considered to be false

Running

yq eval --null-input '~ | not'

will output

true