1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00
This commit is contained in:
Mike Farah
2020-12-27 22:29:00 +11:00
parent 7b54bead5e
commit 4e1a1025c3
24 changed files with 332 additions and 268 deletions

View File

@@ -8,8 +8,11 @@ Use `+=` as append assign for things like increment. `.a += .x` is equivalent to
## Concatenate and assign arrays ## Concatenate and assign arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {val: thing, b: [cat, dog]} a:
'': null val: thing
b:
- cat
- dog
``` ```
then then
```bash ```bash
@@ -17,16 +20,23 @@ yq eval '.a.b += ["cow"]' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {val: thing, b: [cat, dog, cow]} a:
'': null val: thing
b:
- cat
- dog
- cow
``` ```
## Concatenate arrays ## Concatenate arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [1, 2] a:
b: [3, 4] - 1
'': null - 2
b:
- 3
- 4
``` ```
then then
```bash ```bash
@@ -34,14 +44,18 @@ yq eval '.a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2, 3, 4] - 1
- 2
- 3
- 4
``` ```
## Concatenate null to array ## Concatenate null to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [1, 2] a:
'': null - 1
- 2
``` ```
then then
```bash ```bash
@@ -49,15 +63,18 @@ yq eval '.a + null' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2] - 1
- 2
``` ```
## Add object to array ## Add object to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [1, 2] a:
c: {cat: meow} - 1
'': null - 2
c:
cat: meow
``` ```
then then
```bash ```bash
@@ -65,14 +82,17 @@ yq eval '.a + .c' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2, {cat: meow}] - 1
- 2
- cat: meow
``` ```
## Add string to array ## Add string to array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [1, 2] a:
'': null - 1
- 2
``` ```
then then
```bash ```bash
@@ -80,15 +100,20 @@ yq eval '.a + "hello"' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2, hello] - 1
- 2
- hello
``` ```
## Update array (append) ## Update array (append)
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [1, 2] a:
b: [3, 4] - 1
'': null - 2
b:
- 3
- 4
``` ```
then then
```bash ```bash
@@ -96,8 +121,13 @@ yq eval '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: [1, 2, 3, 4] a:
b: [3, 4] - 1
'': null - 2
- 3
- 4
b:
- 3
- 4
``` ```

View File

@@ -4,7 +4,6 @@ This operator is used to provide alternative (or default) values when a particul
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: bridge a: bridge
'': null
``` ```
then then
```bash ```bash
@@ -33,7 +32,6 @@ hello
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: ~ a: ~
'': null
``` ```
then then
```bash ```bash
@@ -48,7 +46,6 @@ hello
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: false a: false
'': null
``` ```
then then
```bash ```bash
@@ -64,7 +61,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: false a: false
b: cat b: cat
'': null
``` ```
then then
```bash ```bash

View File

@@ -7,7 +7,6 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchor
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: &billyBob cat a: &billyBob cat
'': null
``` ```
then then
```bash ```bash
@@ -22,7 +21,6 @@ billyBob
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -31,7 +29,6 @@ yq eval '.a anchor = "foobar"' sample.yml
will output will output
```yaml ```yaml
a: &foobar cat a: &foobar cat
'': null
``` ```
## Get alias ## Get alias
@@ -39,7 +36,6 @@ Given a sample.yml file of:
```yaml ```yaml
b: &billyBob meow b: &billyBob meow
a: *billyBob a: *billyBob
'': null
``` ```
then then
```bash ```bash
@@ -55,7 +51,6 @@ Given a sample.yml file of:
```yaml ```yaml
b: &meow purr b: &meow purr
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -65,14 +60,14 @@ will output
```yaml ```yaml
b: &meow purr b: &meow purr
a: *meow a: *meow
'': null
``` ```
## Explode alias and anchor ## Explode alias and anchor
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
f: {a: &a cat, b: *a} f:
'': null a: &a cat
b: *a
``` ```
then then
```bash ```bash
@@ -80,15 +75,15 @@ yq eval 'explode(.f)' sample.yml
``` ```
will output will output
```yaml ```yaml
f: {a: cat, b: cat} f:
'': null a: cat
b: cat
``` ```
## Explode with no aliases or anchors ## Explode with no aliases or anchors
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: mike a: mike
'': null
``` ```
then then
```bash ```bash
@@ -97,14 +92,14 @@ yq eval 'explode(.a)' sample.yml
will output will output
```yaml ```yaml
a: mike a: mike
'': null
``` ```
## Explode with alias keys ## Explode with alias keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
f: {a: &a cat, *a: b} f:
'': null a: &a cat
*a: b
``` ```
then then
```bash ```bash
@@ -112,8 +107,9 @@ yq eval 'explode(.f)' sample.yml
``` ```
will output will output
```yaml ```yaml
f: {a: cat, cat: b} f:
'': null a: cat
cat: b
``` ```
## Explode with merge anchors ## Explode with merge anchors
@@ -129,13 +125,14 @@ bar: &bar
c: bar_c c: bar_c
foobarList: foobarList:
b: foobarList_b b: foobarList_b
!!merge <<: [*foo, *bar] !!merge <<:
- *foo
- *bar
c: foobarList_c c: foobarList_c
foobar: foobar:
c: foobar_c c: foobar_c
!!merge <<: *foo !!merge <<: *foo
thing: foobar_thing thing: foobar_thing
'': null
``` ```
then then
```bash ```bash
@@ -160,6 +157,5 @@ foobar:
c: foo_c c: foo_c
a: foo_a a: foo_a
thing: foobar_thing thing: foobar_thing
'': null
``` ```

View File

@@ -20,8 +20,9 @@ x: frog
## Update node to be the child value ## Update node to be the child value
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: {g: foof}} a:
'': null b:
g: foof
``` ```
then then
```bash ```bash
@@ -29,16 +30,16 @@ yq eval '.a |= .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {g: foof} a:
'': null g: foof
``` ```
## Update node to be the sibling value ## Update node to be the sibling value
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: child} a:
b: child
b: sibling b: sibling
'': null
``` ```
then then
```bash ```bash
@@ -48,7 +49,6 @@ will output
```yaml ```yaml
a: sibling a: sibling
b: sibling b: sibling
'': null
``` ```
## Updated multiple paths ## Updated multiple paths
@@ -57,7 +57,6 @@ Given a sample.yml file of:
a: fieldA a: fieldA
b: fieldB b: fieldB
c: fieldC c: fieldC
'': null
``` ```
then then
```bash ```bash
@@ -68,14 +67,13 @@ will output
a: potatoe a: potatoe
b: fieldB b: fieldB
c: potatoe c: potatoe
'': null
``` ```
## Update string value ## Update string value
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: apple} a:
'': null b: apple
``` ```
then then
```bash ```bash
@@ -83,8 +81,8 @@ yq eval '.a.b = "frog"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {b: frog} a:
'': null b: frog
``` ```
## Update string value via |= ## Update string value via |=
@@ -92,8 +90,8 @@ Note there is no difference between `=` and `|=` when the RHS is a scalar
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: apple} a:
'': null b: apple
``` ```
then then
```bash ```bash
@@ -101,15 +99,16 @@ yq eval '.a.b |= "frog"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {b: frog} a:
'': null b: frog
``` ```
## Update selected results ## Update selected results
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: apple, c: cactus} a:
'': null b: apple
c: cactus
``` ```
then then
```bash ```bash
@@ -117,8 +116,9 @@ yq eval '(.a[] | select(. == "apple")) = "frog"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {b: frog, c: cactus} a:
'': null b: frog
c: cactus
``` ```
## Update array values ## Update array values

View File

@@ -24,13 +24,10 @@ Given a sample.yml file of:
```yaml ```yaml
- a: bird - a: bird
b: dog b: dog
'': null
- a: frog - a: frog
b: bird b: bird
'': null
- a: cat - a: cat
b: fly b: fly
'': null
``` ```
then then
```bash ```bash
@@ -40,10 +37,8 @@ will output
```yaml ```yaml
- a: bird - a: bird
b: dog b: dog
'': null
- a: cat - a: cat
b: fly b: fly
'': null
``` ```
## Not true is false ## Not true is false

View File

@@ -28,7 +28,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
b: dog b: dog
'': null
``` ```
then then
```bash ```bash

View File

@@ -13,7 +13,6 @@ will output
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
name: Mike name: Mike
'': null
``` ```
then then
```bash ```bash
@@ -23,15 +22,15 @@ will output
```yaml ```yaml
wrap: wrap:
name: Mike name: Mike
'': null
``` ```
## Using splat to create multiple objects ## Using splat to create multiple objects
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
name: Mike name: Mike
pets: [cat, dog] pets:
'': null - cat
- dog
``` ```
then then
```bash ```bash
@@ -47,12 +46,14 @@ Mike: dog
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
name: Mike name: Mike
pets: [cat, dog] pets:
'': null - cat
- dog
--- ---
name: Rosey name: Rosey
pets: [monkey, sheep] pets:
'': null - monkey
- sheep
``` ```
then then
```bash ```bash

View File

@@ -3,7 +3,6 @@ Use these comment operators to set or retrieve comments.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -12,14 +11,12 @@ yq eval '.a lineComment="single"' sample.yml
will output will output
```yaml ```yaml
a: cat # single a: cat # single
'': null
``` ```
## Set head comment ## Set head comment
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -30,14 +27,12 @@ will output
# single # single
a: cat a: cat
'': null
``` ```
## Set foot comment, using an expression ## Set foot comment, using an expression
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -46,7 +41,6 @@ yq eval '. footComment=.a' sample.yml
will output will output
```yaml ```yaml
a: cat a: cat
'': null
# cat # cat
``` ```
@@ -56,7 +50,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: cat # comment a: cat # comment
b: dog # leave this b: dog # leave this
'': null
``` ```
then then
```bash ```bash
@@ -66,14 +59,12 @@ will output
```yaml ```yaml
a: cat a: cat
b: dog # leave this b: dog # leave this
'': null
``` ```
## Remove all comments ## Remove all comments
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat # comment a: cat # comment
'': null
``` ```
then then
```bash ```bash
@@ -81,15 +72,13 @@ yq eval '.. comments=""' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat # comment a: cat
'': null
``` ```
## Get line comment ## Get line comment
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat # meow a: cat # meow
'': null
``` ```
then then
```bash ```bash
@@ -104,7 +93,6 @@ meow
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat # meow a: cat # meow
'': null
``` ```
then then
```bash ```bash
@@ -119,7 +107,6 @@ will output
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat # meow a: cat # meow
'': null
``` ```
then then
```bash ```bash

View File

@@ -4,7 +4,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
b: dog b: dog
'': null
``` ```
then then
```bash ```bash
@@ -13,14 +12,14 @@ yq eval 'del(.b)' sample.yml
will output will output
```yaml ```yaml
a: cat a: cat
'': null
``` ```
## Delete nested entry in map ## Delete nested entry in map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {a1: fred, a2: frood} a:
'': null a1: fred
a2: frood
``` ```
then then
```bash ```bash
@@ -28,8 +27,8 @@ yq eval 'del(.a.a1)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a2: frood} a:
'': null a2: frood
``` ```
## Delete entry in array ## Delete entry in array
@@ -54,7 +53,6 @@ Given a sample.yml file of:
```yaml ```yaml
- a: cat - a: cat
b: dog b: dog
'': null
``` ```
then then
```bash ```bash
@@ -63,7 +61,6 @@ yq eval 'del(.[0].a)' sample.yml
will output will output
```yaml ```yaml
- b: dog - b: dog
'': null
``` ```
## Delete no matches ## Delete no matches
@@ -71,7 +68,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
b: dog b: dog
'': null
``` ```
then then
```bash ```bash
@@ -81,7 +77,6 @@ will output
```yaml ```yaml
a: cat a: cat
b: dog b: dog
'': null
``` ```
## Delete matching entries ## Delete matching entries
@@ -90,7 +85,6 @@ Given a sample.yml file of:
a: cat a: cat
b: dog b: dog
c: bat c: bat
'': null
``` ```
then then
```bash ```bash
@@ -99,6 +93,5 @@ yq eval 'del( .[] | select(. == "*at") )' sample.yml
will output will output
```yaml ```yaml
b: dog b: dog
'': null
``` ```

View File

@@ -3,10 +3,8 @@ Use the `documentIndex` operator to select nodes of a particular document.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
--- ---
a: frog a: frog
'': null
``` ```
then then
```bash ```bash
@@ -23,10 +21,8 @@ will output
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
--- ---
a: frog a: frog
'': null
``` ```
then then
```bash ```bash
@@ -35,17 +31,14 @@ yq eval 'select(. | documentIndex == 1)' sample.yml
will output will output
```yaml ```yaml
a: frog a: frog
'': null
``` ```
## Print Document Index with matches ## Print Document Index with matches
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
--- ---
a: frog a: frog
'': null
``` ```
then then
```bash ```bash
@@ -53,7 +46,9 @@ yq eval '.a | ({"match": ., "doc": (. | documentIndex)})' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null match: cat
'': null doc: 0
match: frog
doc: 1
``` ```

View File

@@ -9,7 +9,6 @@ yq eval-all 'select(fileIndex == 0) * select(filename == "file2.yaml")' file1.ya
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -24,7 +23,6 @@ sample.yaml
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash

View File

@@ -2,14 +2,10 @@ This is operation that returns true if the key exists in a map (or index in an a
## Has map key ## Has map key
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
- a: "yes" - a: yes
'': null
- a: ~ - a: ~
'': null
- a: - a:
'': null
- b: nope - b: nope
'': null
``` ```
then then
```bash ```bash

View File

@@ -6,7 +6,6 @@ returns length of string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
'': null
``` ```
then then
```bash ```bash
@@ -24,7 +23,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
c: dog c: dog
'': null
``` ```
then then
```bash ```bash
@@ -32,7 +30,7 @@ yq eval 'length' sample.yml
``` ```
will output will output
```yaml ```yaml
3 2
``` ```
## Array length ## Array length

View File

@@ -16,9 +16,13 @@ yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.y
## Merge objects together, returning merged result only ## Merge objects together, returning merged result only
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {field: me, fieldA: cat} a:
b: {field: {g: wizz}, fieldB: dog} field: me
'': null fieldA: cat
b:
field:
g: wizz
fieldB: dog
``` ```
then then
```bash ```bash
@@ -26,15 +30,22 @@ yq eval '.a * .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{'': null} field:
g: wizz
fieldA: cat
fieldB: dog
``` ```
## Merge objects together, returning parent object ## Merge objects together, returning parent object
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {field: me, fieldA: cat} a:
b: {field: {g: wizz}, fieldB: dog} field: me
'': null fieldA: cat
b:
field:
g: wizz
fieldB: dog
``` ```
then then
```bash ```bash
@@ -42,7 +53,15 @@ yq eval '. * {"a":.b}' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null a:
field:
g: wizz
fieldA: cat
fieldB: dog
b:
field:
g: wizz
fieldB: dog
``` ```
## Merge keeps style of LHS ## Merge keeps style of LHS
@@ -59,15 +78,22 @@ yq eval '. * {"a":.b}' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null a: {things: great, also: "me"}
b:
also: "me"
``` ```
## Merge arrays ## Merge arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [1, 2, 3] a:
b: [3, 4, 5] - 1
'': null - 2
- 3
b:
- 3
- 4
- 5
``` ```
then then
```bash ```bash
@@ -75,15 +101,31 @@ yq eval '. * {"a":.b}' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null a:
- 3
- 4
- 5
b:
- 3
- 4
- 5
``` ```
## Merge, appending arrays ## Merge, appending arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {array: [1, 2, {animal: dog}], value: coconut} a:
b: {array: [3, 4, {animal: cat}], value: banana} array:
'': null - 1
- 2
- animal: dog
value: coconut
b:
array:
- 3
- 4
- animal: cat
value: banana
``` ```
then then
```bash ```bash
@@ -91,7 +133,14 @@ yq eval '.a *+ .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{'': null} array:
- 1
- 2
- animal: dog
- 3
- 4
- animal: cat
value: banana
``` ```
## Merge to prefix an element ## Merge to prefix an element
@@ -99,7 +148,6 @@ Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
b: dog b: dog
'': null
``` ```
then then
```bash ```bash
@@ -107,16 +155,20 @@ yq eval '. * {"a": {"c": .a}}' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null a:
c: cat
b: dog
``` ```
## Merge with simple aliases ## Merge with simple aliases
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: &cat {c: frog} a: &cat
b: {f: *cat} c: frog
c: {g: thongs} b:
'': null f: *cat
c:
g: thongs
``` ```
then then
```bash ```bash
@@ -124,16 +176,19 @@ yq eval '.c * .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{'': null} g: thongs
f: *cat
``` ```
## Merge does not copy anchor names ## Merge does not copy anchor names
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {c: &cat frog} a:
b: {f: *cat} c: &cat frog
c: {g: thongs} b:
'': null f: *cat
c:
g: thongs
``` ```
then then
```bash ```bash
@@ -141,7 +196,8 @@ yq eval '.c * .a' sample.yml
``` ```
will output will output
```yaml ```yaml
{'': null} g: thongs
c: frog
``` ```
## Merge with merge anchors ## Merge with merge anchors
@@ -157,13 +213,14 @@ bar: &bar
c: bar_c c: bar_c
foobarList: foobarList:
b: foobarList_b b: foobarList_b
!!merge <<: [*foo, *bar] !!merge <<:
- *foo
- *bar
c: foobarList_c c: foobarList_c
foobar: foobar:
c: foobar_c c: foobar_c
!!merge <<: *foo !!merge <<: *foo
thing: foobar_thing thing: foobar_thing
'': null
``` ```
then then
```bash ```bash
@@ -171,6 +228,11 @@ yq eval '.foobar * .foobarList' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null c: foobarList_c
<<:
- *foo
- *bar
thing: foobar_thing
b: foobarList_b
``` ```

View File

@@ -5,8 +5,8 @@ You can get the key/index of matching nodes by using the `path` operator to retu
## Map path ## Map path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: cat} a:
'': null b: cat
``` ```
then then
```bash ```bash
@@ -21,8 +21,8 @@ will output
## Get map key ## Get map key
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: cat} a:
'': null b: cat
``` ```
then then
```bash ```bash
@@ -36,8 +36,9 @@ b
## Array path ## Array path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [cat, dog] a:
'': null - cat
- dog
``` ```
then then
```bash ```bash
@@ -52,8 +53,9 @@ will output
## Get array index ## Get array index
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [cat, dog] a:
'': null - cat
- dog
``` ```
then then
```bash ```bash
@@ -67,8 +69,10 @@ will output
## Print path and value ## Print path and value
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: [cat, dog, frog] a:
'': null - cat
- dog
- frog
``` ```
then then
```bash ```bash
@@ -76,7 +80,13 @@ yq eval '.a.[] | select(. == "*og") | [{"path":path, "value":.}]' sample.yml
``` ```
will output will output
```yaml ```yaml
- '': null - path:
- '': null - a
- 1
value: dog
- path:
- a
- 2
value: frog
``` ```

View File

@@ -3,8 +3,8 @@ Pipe the results of an expression into another. Like the bash operator.
## Simple Pipe ## Simple Pipe
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {b: cat} a:
'': null b: cat
``` ```
then then
```bash ```bash
@@ -21,7 +21,6 @@ Given a sample.yml file of:
a: cow a: cow
b: sheep b: sheep
c: same c: same
'': null
``` ```
then then
```bash ```bash
@@ -32,6 +31,5 @@ will output
a: cat a: cat
b: dog b: dog
c: same c: same
'': null
``` ```

View File

@@ -6,9 +6,9 @@ yq eval '.. style= "flow"' file.yaml
## Aliases are not traversed ## Aliases are not traversed
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: &cat {c: frog} a: &cat
c: frog
b: *cat b: *cat
'': null
``` ```
then then
```bash ```bash
@@ -16,10 +16,13 @@ yq eval '[..]' sample.yml
``` ```
will output will output
```yaml ```yaml
- a: &cat {c: frog} - a: &cat
c: frog
b: *cat b: *cat
'': null - &cat
- null c: frog
- frog
- *cat
``` ```
## Merge docs are not traversed ## Merge docs are not traversed
@@ -35,13 +38,14 @@ bar: &bar
c: bar_c c: bar_c
foobarList: foobarList:
b: foobarList_b b: foobarList_b
!!merge <<: [*foo, *bar] !!merge <<:
- *foo
- *bar
c: foobarList_c c: foobarList_c
foobar: foobar:
c: foobar_c c: foobar_c
!!merge <<: *foo !!merge <<: *foo
thing: foobar_thing thing: foobar_thing
'': null
``` ```
then then
```bash ```bash
@@ -52,7 +56,8 @@ will output
- c: foobar_c - c: foobar_c
!!merge <<: *foo !!merge <<: *foo
thing: foobar_thing thing: foobar_thing
'': null - foobar_c
- null - *foo
- foobar_thing
``` ```

View File

@@ -19,8 +19,10 @@ goat
## Select and update matching values in map ## Select and update matching values in map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: {things: cat, bob: goat, horse: dog} a:
'': null things: cat
bob: goat
horse: dog
``` ```
then then
```bash ```bash
@@ -28,7 +30,9 @@ yq eval '(.a.[] | select(. == "*at")) |= "rabbit"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {things: rabbit, bob: rabbit, horse: dog} a:
'': null things: rabbit
bob: rabbit
horse: dog
``` ```

View File

@@ -14,7 +14,6 @@ Given a sample.yml file of:
c: frog c: frog
a: blah a: blah
b: bing b: bing
'': null
``` ```
then then
```bash ```bash
@@ -22,7 +21,6 @@ yq eval 'sortKeys(.)' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null
a: blah a: blah
b: bing b: bing
c: frog c: frog
@@ -33,9 +31,19 @@ Note the array elements are left unsorted, but maps inside arrays are sorted
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
bParent: {c: dog, array: [3, 1, 2]} bParent:
aParent: {z: donkey, x: [{c: yum, b: delish}, {b: ew, a: apple}]} c: dog
'': null array:
- 3
- 1
- 2
aParent:
z: donkey
x:
- c: yum
b: delish
- b: ew
a: apple
``` ```
then then
```bash ```bash
@@ -43,8 +51,18 @@ yq eval 'sortKeys(..)' sample.yml
``` ```
will output will output
```yaml ```yaml
'': null aParent:
aParent: {z: donkey, x: [{c: yum, b: delish}, {b: ew, a: apple}]} x:
bParent: {c: dog, array: [3, 1, 2]} - b: delish
c: yum
- a: apple
b: ew
z: donkey
bParent:
array:
- 3
- 1
- 2
c: dog
``` ```

View File

@@ -6,7 +6,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -15,11 +14,10 @@ yq eval '.. style="tagged"' sample.yml
will output will output
```yaml ```yaml
!!map !!map
a: cat a: !!str cat
b: 5 b: !!int 5
c: 3.2 c: !!float 3.2
e: true e: !!bool true
'': !!null null
``` ```
## Set double quote style ## Set double quote style
@@ -29,7 +27,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -37,11 +34,10 @@ yq eval '.. style="double"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat a: "cat"
b: 5 b: "5"
c: 3.2 c: "3.2"
e: true e: "true"
'': "null"
``` ```
## Set single quote style ## Set single quote style
@@ -51,7 +47,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -59,11 +54,10 @@ yq eval '.. style="single"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat a: 'cat'
b: 5 b: '5'
c: 3.2 c: '3.2'
e: true e: 'true'
'': 'null'
``` ```
## Set literal quote style ## Set literal quote style
@@ -73,7 +67,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -81,12 +74,14 @@ yq eval '.. style="literal"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat a: |-
b: 5 cat
c: 3.2 b: |-
e: true 5
'': |- c: |-
null 3.2
e: |-
true
``` ```
## Set folded quote style ## Set folded quote style
@@ -96,7 +91,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -104,12 +98,14 @@ yq eval '.. style="folded"' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat a: >-
b: 5 cat
c: 3.2 b: >-
e: true 5
'': >- c: >-
null 3.2
e: >-
true
``` ```
## Set flow quote style ## Set flow quote style
@@ -119,7 +115,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -127,7 +122,7 @@ yq eval '.. style="flow"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: cat, b: 5, c: 3.2, e: true, '': null} {a: cat, b: 5, c: 3.2, e: true}
``` ```
## Pretty print ## Pretty print
@@ -139,7 +134,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -151,7 +145,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
## Read style ## Read style
@@ -166,6 +159,7 @@ yq eval '.. | style' sample.yml
will output will output
```yaml ```yaml
flow flow
double
single
``` ```

View File

@@ -7,7 +7,6 @@ b: 5
c: 3.2 c: 3.2
e: true e: true
f: [] f: []
'': null
``` ```
then then
```bash ```bash
@@ -16,7 +15,11 @@ yq eval '.. | tag' sample.yml
will output will output
```yaml ```yaml
!!map !!map
!!null !!str
!!int
!!float
!!bool
!!seq
``` ```
## Convert numbers to strings ## Convert numbers to strings
@@ -26,7 +29,6 @@ a: cat
b: 5 b: 5
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```
then then
```bash ```bash
@@ -35,9 +37,8 @@ yq eval '(.. | select(tag == "!!int")) tag= "!!str"' sample.yml
will output will output
```yaml ```yaml
a: cat a: cat
b: 5 b: "5"
c: 3.2 c: 3.2
e: true e: true
'': null
``` ```

View File

@@ -294,8 +294,8 @@ yq eval '.foobar.[]' sample.yml
``` ```
will output will output
```yaml ```yaml
foobar_c foo_c
*foo foo_a
foobar_thing foobar_thing
``` ```
@@ -360,9 +360,9 @@ yq eval '.foobarList.[]' sample.yml
``` ```
will output will output
```yaml ```yaml
foobarList_b bar_b
- *foo foo_a
- *bar bar_thing
foobarList_c foobarList_c
``` ```

View File

@@ -17,7 +17,6 @@ Given a sample.yml file of:
a: fieldA a: fieldA
b: fieldB b: fieldB
c: fieldC c: fieldC
'': null
``` ```
then then
```bash ```bash

View File

@@ -58,7 +58,7 @@ func traverse(d *dataTreeNavigator, matchingNode *CandidateNode, operation *Oper
if operation.Preferences != nil { if operation.Preferences != nil {
followAlias = !operation.Preferences.(*TraversePreferences).DontFollowAlias followAlias = !operation.Preferences.(*TraversePreferences).DontFollowAlias
} }
return traverseMap(matchingNode, operation.StringValue, followAlias) return traverseMap(matchingNode, operation.StringValue, followAlias, false)
case yaml.SequenceNode: case yaml.SequenceNode:
log.Debug("its a sequence of %v things!", len(value.Content)) log.Debug("its a sequence of %v things!", len(value.Content))
@@ -135,26 +135,15 @@ func traverseArrayIndices(matchingNode *CandidateNode, indicesToTraverse []*yaml
} }
func traverseMapWithIndices(candidate *CandidateNode, indices []*yaml.Node, followAlias bool) (*list.List, error) { func traverseMapWithIndices(candidate *CandidateNode, indices []*yaml.Node, followAlias bool) (*list.List, error) {
node := UnwrapDoc(candidate.Node)
var contents = node.Content
var matchingNodeMap = list.New()
if len(indices) == 0 { if len(indices) == 0 {
for index := 0; index < len(contents); index = index + 2 { return traverseMap(candidate, "", followAlias, true)
key := contents[index]
value := contents[index+1]
matchingNodeMap.PushBack(&CandidateNode{
Node: value,
Path: candidate.CreateChildPath(key.Value),
Document: candidate.Document,
})
}
return matchingNodeMap, nil
} }
var matchingNodeMap = list.New()
for _, indexNode := range indices { for _, indexNode := range indices {
log.Debug("traverseMapWithIndices: %v", indexNode.Value) log.Debug("traverseMapWithIndices: %v", indexNode.Value)
newNodes, err := traverseMap(candidate, indexNode.Value, followAlias) newNodes, err := traverseMap(candidate, indexNode.Value, followAlias, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -217,9 +206,9 @@ func keyMatches(key *yaml.Node, wantedKey string) bool {
return Match(key.Value, wantedKey) return Match(key.Value, wantedKey)
} }
func traverseMap(matchingNode *CandidateNode, key string, followAlias bool) (*list.List, error) { func traverseMap(matchingNode *CandidateNode, key string, followAlias bool, splat bool) (*list.List, error) {
var newMatches = orderedmap.NewOrderedMap() var newMatches = orderedmap.NewOrderedMap()
err := doTraverseMap(newMatches, matchingNode, key, followAlias) err := doTraverseMap(newMatches, matchingNode, key, followAlias, splat)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -248,7 +237,7 @@ func traverseMap(matchingNode *CandidateNode, key string, followAlias bool) (*li
return results, nil return results, nil
} }
func doTraverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode, wantedKey string, followAlias bool) error { func doTraverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode, wantedKey string, followAlias bool, splat bool) error {
// value.Content is a concatenated array of key, value, // value.Content is a concatenated array of key, value,
// so keys are in the even indexes, values in odd. // so keys are in the even indexes, values in odd.
// merge aliases are defined first, but we only want to traverse them // merge aliases are defined first, but we only want to traverse them
@@ -265,11 +254,11 @@ func doTraverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode,
//skip the 'merge' tag, find a direct match first //skip the 'merge' tag, find a direct match first
if key.Tag == "!!merge" && followAlias { if key.Tag == "!!merge" && followAlias {
log.Debug("Merge anchor") log.Debug("Merge anchor")
err := traverseMergeAnchor(newMatches, candidate, value, wantedKey) err := traverseMergeAnchor(newMatches, candidate, value, wantedKey, splat)
if err != nil { if err != nil {
return err return err
} }
} else if keyMatches(key, wantedKey) { } else if splat || keyMatches(key, wantedKey) {
log.Debug("MATCHED") log.Debug("MATCHED")
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
Node: value, Node: value,
@@ -283,7 +272,7 @@ func doTraverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode,
return nil return nil
} }
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, wantedKey string) error { func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, wantedKey string, splat bool) error {
switch value.Kind { switch value.Kind {
case yaml.AliasNode: case yaml.AliasNode:
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
@@ -291,10 +280,10 @@ func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *C
Path: originalCandidate.Path, Path: originalCandidate.Path,
Document: originalCandidate.Document, Document: originalCandidate.Document,
} }
return doTraverseMap(newMatches, candidateNode, wantedKey, true) return doTraverseMap(newMatches, candidateNode, wantedKey, true, splat)
case yaml.SequenceNode: case yaml.SequenceNode:
for _, childValue := range value.Content { for _, childValue := range value.Content {
err := traverseMergeAnchor(newMatches, originalCandidate, childValue, wantedKey) err := traverseMergeAnchor(newMatches, originalCandidate, childValue, wantedKey, splat)
if err != nil { if err != nil {
return err return err
} }