Fixed styling + delete issue

This commit is contained in:
Mike Farah 2023-05-30 15:05:28 +10:00
parent f6c3fab309
commit fd67748078
48 changed files with 798 additions and 303 deletions

View File

@ -389,9 +389,9 @@ func (n *CandidateNode) UpdateFrom(other *CandidateNode, prefs assignPreferences
}
n.Content = make([]*CandidateNode, 0)
n.Kind = other.Kind
n.AddChildren(other.Content)
n.Kind = other.Kind
n.Value = other.Value
n.UpdateAttributesFrom(other, prefs)

View File

@ -12,7 +12,12 @@ Use `+=` as a relative append assign for things like increment. Note that `.a +=
## Concatenate arrays
Given a sample.yml file of:
```yaml
{a: [1, 2], b: [3, 4]}
a:
- 1
- 2
b:
- 3
- 4
```
then
```bash
@ -20,7 +25,10 @@ yq '.a + .b' sample.yml
```
will output
```yaml
[1, 2, 3, 4]
- 1
- 2
- 3
- 4
```
## Concatenate to existing array
@ -48,7 +56,9 @@ b:
## Concatenate null to array
Given a sample.yml file of:
```yaml
{a: [1, 2]}
a:
- 1
- 2
```
then
```bash
@ -56,7 +66,8 @@ yq '.a + null' sample.yml
```
will output
```yaml
[1, 2]
- 1
- 2
```
## Append to existing array
@ -143,7 +154,8 @@ a:
## String concatenation
Given a sample.yml file of:
```yaml
{a: cat, b: meow}
a: cat
b: meow
```
then
```bash
@ -151,7 +163,8 @@ yq '.a += .b' sample.yml
```
will output
```yaml
{a: catmeow, b: meow}
a: catmeow
b: meow
```
## Number addition - float
@ -159,7 +172,8 @@ If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
```yaml
{a: 3, b: 4.9}
a: 3
b: 4.9
```
then
```bash
@ -167,7 +181,8 @@ yq '.a = .a + .b' sample.yml
```
will output
```yaml
{a: 7.9, b: 4.9}
a: 7.9
b: 4.9
```
## Number addition - int
@ -175,7 +190,8 @@ If both the lhs and rhs are ints then the expression will be calculated with int
Given a sample.yml file of:
```yaml
{a: 3, b: 4}
a: 3
b: 4
```
then
```bash
@ -183,13 +199,15 @@ yq '.a = .a + .b' sample.yml
```
will output
```yaml
{a: 7, b: 4}
a: 7
b: 4
```
## Increment numbers
Given a sample.yml file of:
```yaml
{a: 3, b: 5}
a: 3
b: 5
```
then
```bash
@ -197,7 +215,8 @@ yq '.[] += 1' sample.yml
```
will output
```yaml
{a: 4, b: 6}
a: 4
b: 6
```
## Date addition

View File

@ -5,7 +5,7 @@ This operator is used to provide alternative (or default) values when a particul
## LHS is defined
Given a sample.yml file of:
```yaml
{a: bridge}
a: bridge
```
then
```bash
@ -33,7 +33,7 @@ hello
## LHS is null
Given a sample.yml file of:
```yaml
{a: ~}
a: ~
```
then
```bash
@ -47,7 +47,7 @@ hello
## LHS is false
Given a sample.yml file of:
```yaml
{a: false}
a: false
```
then
```bash
@ -61,7 +61,8 @@ hello
## RHS is an expression
Given a sample.yml file of:
```yaml
{a: false, b: cat}
a: false
b: cat
```
then
```bash

View File

@ -143,7 +143,8 @@ a: &cat
## Get alias
Given a sample.yml file of:
```yaml
{b: &billyBob meow, a: *billyBob}
b: &billyBob meow
a: *billyBob
```
then
```bash
@ -157,7 +158,8 @@ billyBob
## Set alias
Given a sample.yml file of:
```yaml
{b: &meow purr, a: cat}
b: &meow purr
a: cat
```
then
```bash
@ -165,13 +167,15 @@ yq '.a alias = "meow"' sample.yml
```
will output
```yaml
{b: &meow purr, a: *meow}
b: &meow purr
a: *meow
```
## Set alias to blank does nothing
Given a sample.yml file of:
```yaml
{b: &meow purr, a: cat}
b: &meow purr
a: cat
```
then
```bash
@ -179,13 +183,16 @@ yq '.a alias = ""' sample.yml
```
will output
```yaml
{b: &meow purr, a: cat}
b: &meow purr
a: cat
```
## Set alias relatively using assign-update
Given a sample.yml file of:
```yaml
{b: &meow purr, a: {f: meow}}
b: &meow purr
a:
f: meow
```
then
```bash
@ -193,13 +200,16 @@ yq '.a alias |= .f' sample.yml
```
will output
```yaml
{b: &meow purr, a: *meow}
b: &meow purr
a: *meow
```
## Explode alias and anchor
Given a sample.yml file of:
```yaml
{f: {a: &a cat, b: *a}}
f:
a: &a cat
b: *a
```
then
```bash
@ -207,7 +217,9 @@ yq 'explode(.f)' sample.yml
```
will output
```yaml
{f: {a: cat, b: cat}}
f:
a: cat
b: cat
```
## Explode with no aliases or anchors
@ -227,7 +239,9 @@ a: mike
## Explode with alias keys
Given a sample.yml file of:
```yaml
{f: {a: &a cat, *a: b}}
f:
a: &a cat
*a: b
```
then
```bash
@ -235,7 +249,9 @@ yq 'explode(.f)' sample.yml
```
will output
```yaml
{f: {a: cat, cat: b}}
f:
a: cat
cat: b
```
## Explode with merge anchors

View File

@ -27,7 +27,9 @@ x: frog
## Update node to be the child value
Given a sample.yml file of:
```yaml
{a: {b: {g: foof}}}
a:
b:
g: foof
```
then
```bash
@ -35,13 +37,16 @@ yq '.a |= .b' sample.yml
```
will output
```yaml
{a: {g: foof}}
a:
g: foof
```
## Double elements in an array
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -49,7 +54,9 @@ yq '.[] |= . * 2' sample.yml
```
will output
```yaml
[2, 4, 6]
- 2
- 4
- 6
```
## Update node from another file
@ -57,11 +64,11 @@ Note this will also work when the second file is a scalar (string/number)
Given a sample.yml file of:
```yaml
{a: apples}
a: apples
```
And another sample another.yml file of:
```yaml
{b: bob}
b: bob
```
then
```bash
@ -69,13 +76,16 @@ yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0
```
will output
```yaml
{a: {b: bob}}
a:
b: bob
```
## Update node to be the sibling value
Given a sample.yml file of:
```yaml
{a: {b: child}, b: sibling}
a:
b: child
b: sibling
```
then
```bash
@ -83,13 +93,16 @@ yq '.a = .b' sample.yml
```
will output
```yaml
{a: sibling, b: sibling}
a: sibling
b: sibling
```
## Updated multiple paths
Given a sample.yml file of:
```yaml
{a: fieldA, b: fieldB, c: fieldC}
a: fieldA
b: fieldB
c: fieldC
```
then
```bash
@ -97,13 +110,16 @@ yq '(.a, .c) = "potato"' sample.yml
```
will output
```yaml
{a: potato, b: fieldB, c: potato}
a: potato
b: fieldB
c: potato
```
## Update string value
Given a sample.yml file of:
```yaml
{a: {b: apple}}
a:
b: apple
```
then
```bash
@ -111,7 +127,8 @@ yq '.a.b = "frog"' sample.yml
```
will output
```yaml
{a: {b: frog}}
a:
b: frog
```
## Update string value via |=
@ -119,7 +136,8 @@ Note there is no difference between `=` and `|=` when the RHS is a scalar
Given a sample.yml file of:
```yaml
{a: {b: apple}}
a:
b: apple
```
then
```bash
@ -127,7 +145,8 @@ yq '.a.b |= "frog"' sample.yml
```
will output
```yaml
{a: {b: frog}}
a:
b: frog
```
## Update deeply selected results
@ -135,7 +154,9 @@ Note that the LHS is wrapped in brackets! This is to ensure we don't first filte
Given a sample.yml file of:
```yaml
{a: {b: apple, c: cactus}}
a:
b: apple
c: cactus
```
then
```bash
@ -143,13 +164,17 @@ yq '(.a[] | select(. == "apple")) = "frog"' sample.yml
```
will output
```yaml
{a: {b: frog, c: cactus}}
a:
b: frog
c: cactus
```
## Update array values
Given a sample.yml file of:
```yaml
[candy, apple, sandy]
- candy
- apple
- sandy
```
then
```bash
@ -157,7 +182,9 @@ yq '(.[] | select(. == "*andy")) = "bogs"' sample.yml
```
will output
```yaml
[bogs, apple, bogs]
- bogs
- apple
- bogs
```
## Update empty object

View File

@ -31,7 +31,8 @@ In the yaml 1.2 standard, support for yes/no as booleans was dropped - they are
Given a sample.yml file of:
```yaml
[yes, no]
- yes
- no
```
then
```bash
@ -56,7 +57,12 @@ false
## Matching nodes with select, equals and or
Given a sample.yml file of:
```yaml
[{a: bird, b: dog}, {a: frog, b: bird}, {a: cat, b: fly}]
- a: bird
b: dog
- a: frog
b: bird
- a: cat
b: fly
```
then
```bash
@ -64,14 +70,17 @@ yq '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
```
will output
```yaml
- {a: bird, b: dog}
- {a: cat, b: fly}
- 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:
```yaml
[false, true]
- false
- true
```
then
```bash
@ -119,7 +128,8 @@ b: false
## `all` returns true if all booleans in a given array are true
Given a sample.yml file of:
```yaml
[true, true]
- true
- true
```
then
```bash

View File

@ -26,7 +26,8 @@ will output
## Collect many
Given a sample.yml file of:
```yaml
{a: cat, b: dog}
a: cat
b: dog
```
then
```bash

View File

@ -222,6 +222,7 @@ yq '. foot_comment=.a' sample.yml
will output
```yaml
a: cat
# cat
```
@ -258,6 +259,8 @@ yq '... comments=""' sample.yml
```
will output
```yaml
# hi
a: cat
b:
```

View File

@ -15,7 +15,9 @@ Array is equal or subset of
Given a sample.yml file of:
```yaml
[foobar, foobaz, blarp]
- foobar
- foobaz
- blarp
```
then
```bash
@ -31,7 +33,9 @@ Subtract the superset array from the subset, if there's anything left, it's not
Given a sample.yml file of:
```yaml
[foobar, foobaz, blarp]
- foobar
- foobaz
- blarp
```
then
```bash
@ -45,7 +49,12 @@ false
## Object included in array
Given a sample.yml file of:
```yaml
{"foo": 12, "bar": [1, 2, {"barp": 12, "blip": 13}]}
"foo": 12
"bar":
- 1
- 2
- "barp": 12
"blip": 13
```
then
```bash
@ -59,7 +68,12 @@ true
## Object not included in array
Given a sample.yml file of:
```yaml
{"foo": 12, "bar": [1, 2, {"barp": 12, "blip": 13}]}
"foo": 12
"bar":
- 1
- 2
- "barp": 12
"blip": 13
```
then
```bash
@ -73,7 +87,7 @@ false
## String contains substring
Given a sample.yml file of:
```yaml
"foobar"
foobar
```
then
```bash
@ -87,7 +101,7 @@ true
## String equals string
Given a sample.yml file of:
```yaml
"meow"
meow
```
then
```bash

View File

@ -15,7 +15,7 @@ will output
## Wrap (prefix) existing object
Given a sample.yml file of:
```yaml
{name: Mike}
name: Mike
```
then
```bash
@ -23,13 +23,17 @@ yq '{"wrap": .}' sample.yml
```
will output
```yaml
wrap: {name: Mike}
wrap:
name: Mike
```
## Using splat to create multiple objects
Given a sample.yml file of:
```yaml
{name: Mike, pets: [cat, dog]}
name: Mike
pets:
- cat
- dog
```
then
```bash
@ -44,9 +48,15 @@ Mike: dog
## Working with multiple documents
Given a sample.yml file of:
```yaml
{name: Mike, pets: [cat, dog]}
name: Mike
pets:
- cat
- dog
---
{name: Rosey, pets: [monkey, sheep]}
name: Rosey
pets:
- monkey
- sheep
```
then
```bash

View File

@ -5,7 +5,8 @@ Deletes matching entries in maps or arrays.
## Delete entry in map
Given a sample.yml file of:
```yaml
{a: cat, b: dog}
a: cat
b: dog
```
then
```bash
@ -13,13 +14,15 @@ yq 'del(.b)' sample.yml
```
will output
```yaml
{a: cat}
a: cat
```
## Delete nested entry in map
Given a sample.yml file of:
```yaml
{a: {a1: fred, a2: frood}}
a:
a1: fred
a2: frood
```
then
```bash
@ -27,13 +30,16 @@ yq 'del(.a.a1)' sample.yml
```
will output
```yaml
{a: {a2: frood}}
a:
a2: frood
```
## Delete entry in array
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -41,13 +47,15 @@ yq 'del(.[1])' sample.yml
```
will output
```yaml
[1, 3]
- 1
- 3
```
## Delete nested entry in array
Given a sample.yml file of:
```yaml
[{a: cat, b: dog}]
- a: cat
b: dog
```
then
```bash
@ -55,13 +63,14 @@ yq 'del(.[0].a)' sample.yml
```
will output
```yaml
[{b: dog}]
- b: dog
```
## Delete no matches
Given a sample.yml file of:
```yaml
{a: cat, b: dog}
a: cat
b: dog
```
then
```bash
@ -69,13 +78,16 @@ yq 'del(.c)' sample.yml
```
will output
```yaml
{a: cat, b: dog}
a: cat
b: dog
```
## Delete matching entries
Given a sample.yml file of:
```yaml
{a: cat, b: dog, c: bat}
a: cat
b: dog
c: bat
```
then
```bash
@ -83,13 +95,17 @@ yq 'del( .[] | select(. == "*at") )' sample.yml
```
will output
```yaml
{b: dog}
b: dog
```
## Recursively delete matching keys
Given a sample.yml file of:
```yaml
{a: {name: frog, b: {name: blog, age: 12}}}
a:
name: frog
b:
name: blog
age: 12
```
then
```bash
@ -97,6 +113,8 @@ yq 'del(.. | select(has("name")).name)' sample.yml
```
will output
```yaml
{a: {b: {age: 12}}}
a:
b:
age: 12
```

View File

@ -7,7 +7,8 @@ Divide behaves differently according to the type of the LHS:
## String split
Given a sample.yml file of:
```yaml
{a: cat_meow, b: _}
a: cat_meow
b: _
```
then
```bash
@ -15,7 +16,11 @@ yq '.c = .a / .b' sample.yml
```
will output
```yaml
{a: cat_meow, b: _, c: [cat, meow]}
a: cat_meow
b: _
c:
- cat
- meow
```
## Number division
@ -23,7 +28,8 @@ The result during division is calculated as a float
Given a sample.yml file of:
```yaml
{a: 12, b: 2.5}
a: 12
b: 2.5
```
then
```bash
@ -31,7 +37,8 @@ yq '.a = .a / .b' sample.yml
```
will output
```yaml
{a: 4.8, b: 2.5}
a: 4.8
b: 2.5
```
## Number division by zero
@ -39,7 +46,8 @@ Dividing by zero results in +Inf or -Inf
Given a sample.yml file of:
```yaml
{a: 1, b: -1}
a: 1
b: -1
```
then
```bash
@ -47,6 +55,7 @@ yq '.a = .a / 0 | .b = .b / 0' sample.yml
```
will output
```yaml
{a: !!float +Inf, b: !!float -Inf}
a: !!float +Inf
b: !!float -Inf
```

View File

@ -30,7 +30,8 @@ Base64 assumes [rfc4648](https://rfc-editor.org/rfc/rfc4648.html) encoding. Enco
## Encode value as json string
Given a sample.yml file of:
```yaml
{a: {cool: thing}}
a:
cool: thing
```
then
```bash
@ -38,7 +39,12 @@ yq '.b = (.a | to_json)' sample.yml
```
will output
```yaml
{a: {cool: thing}, b: "{\n \"cool\": \"thing\"\n}\n"}
a:
cool: thing
b: |
{
"cool": "thing"
}
```
## Encode value as json string, on one line
@ -46,7 +52,8 @@ Pass in a 0 indent to print json on a single line.
Given a sample.yml file of:
```yaml
{a: {cool: thing}}
a:
cool: thing
```
then
```bash
@ -54,7 +61,9 @@ yq '.b = (.a | to_json(0))' sample.yml
```
will output
```yaml
{a: {cool: thing}, b: '{"cool":"thing"}'}
a:
cool: thing
b: '{"cool":"thing"}'
```
## Encode value as json string, on one line shorthand
@ -62,7 +71,8 @@ Pass in a 0 indent to print json on a single line.
Given a sample.yml file of:
```yaml
{a: {cool: thing}}
a:
cool: thing
```
then
```bash
@ -70,7 +80,9 @@ yq '.b = (.a | @json)' sample.yml
```
will output
```yaml
{a: {cool: thing}, b: '{"cool":"thing"}'}
a:
cool: thing
b: '{"cool":"thing"}'
```
## Decode a json encoded string
@ -92,7 +104,8 @@ cool: thing
## Encode value as props string
Given a sample.yml file of:
```yaml
{a: {cool: thing}}
a:
cool: thing
```
then
```bash
@ -100,7 +113,10 @@ yq '.b = (.a | @props)' sample.yml
```
will output
```yaml
{a: {cool: thing}, b: "cool = thing\n"}
a:
cool: thing
b: |
cool = thing
```
## Decode props encoded string
@ -257,7 +273,10 @@ Scalars are strings, numbers and booleans.
Given a sample.yml file of:
```yaml
[cat, 'thing1,thing2', true, 3.40]
- cat
- thing1,thing2
- true
- 3.40
```
then
```bash
@ -271,7 +290,14 @@ cat,"thing1,thing2",true,3.40
## Encode array of arrays as csv string
Given a sample.yml file of:
```yaml
[[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]]
- - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
```
then
```bash
@ -288,7 +314,14 @@ Scalars are strings, numbers and booleans.
Given a sample.yml file of:
```yaml
[[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]]
- - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
```
then
```bash
@ -303,7 +336,10 @@ dog thing3 false 12
## Encode value as xml string
Given a sample.yml file of:
```yaml
{a: {cool: {foo: bar, +@id: hi}}}
a:
cool:
foo: bar
+@id: hi
```
then
```bash
@ -320,7 +356,10 @@ will output
## Encode value as xml string on a single line
Given a sample.yml file of:
```yaml
{a: {cool: {foo: bar, +@id: hi}}}
a:
cool:
foo: bar
+@id: hi
```
then
```bash
@ -335,7 +374,10 @@ will output
## Encode value as xml string with custom indentation
Given a sample.yml file of:
```yaml
{a: {cool: {foo: bar, +@id: hi}}}
a:
cool:
foo: bar
+@id: hi
```
then
```bash

View File

@ -5,7 +5,8 @@ Similar to the same named functions in `jq` these functions convert to/from an o
## to_entries Map
Given a sample.yml file of:
```yaml
{a: 1, b: 2}
a: 1
b: 2
```
then
```bash
@ -22,7 +23,8 @@ will output
## to_entries Array
Given a sample.yml file of:
```yaml
[a, b]
- a
- b
```
then
```bash
@ -39,7 +41,7 @@ will output
## to_entries null
Given a sample.yml file of:
```yaml
[]
null
```
then
```bash
@ -47,13 +49,13 @@ yq 'to_entries' sample.yml
```
will output
```yaml
[]
```
## from_entries map
Given a sample.yml file of:
```yaml
{a: 1, b: 2}
a: 1
b: 2
```
then
```bash
@ -70,7 +72,8 @@ from_entries always creates a map, even for numeric keys
Given a sample.yml file of:
```yaml
[a, b]
- a
- b
```
then
```bash
@ -85,7 +88,8 @@ will output
## Use with_entries to update keys
Given a sample.yml file of:
```yaml
{a: 1, b: 2}
a: 1
b: 2
```
then
```bash
@ -102,7 +106,9 @@ Use to_entries to convert to an array of key/value pairs, sort the array using s
Given a sample.yml file of:
```yaml
{a: 1, c: 3, b: 2}
a: 1
c: 3
b: 2
```
then
```bash
@ -118,7 +124,10 @@ a: 1
## Use with_entries to filter the map
Given a sample.yml file of:
```yaml
{a: {b: bird}, c: {d: dog}}
a:
b: bird
c:
d: dog
```
then
```bash
@ -126,6 +135,7 @@ yq 'with_entries(select(.value | has("b")))' sample.yml
```
will output
```yaml
a: {b: bird}
a:
b: bird
```

View File

@ -95,7 +95,10 @@ The env variable can be any valid yq expression.
Given a sample.yml file of:
```yaml
{a: {b: [{name: dog}, {name: cat}]}}
a:
b:
- name: dog
- name: cat
```
then
```bash
@ -103,13 +106,17 @@ pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueE
```
will output
```yaml
{a: {b: [{name: moo}, {name: cat}]}}
a:
b:
- name: moo
- name: cat
```
## Dynamic key lookup with environment variable
Given a sample.yml file of:
```yaml
{cat: meow, dog: woof}
cat: meow
dog: woof
```
then
```bash
@ -213,7 +220,7 @@ Error: variable ${myEmptyEnv} set but empty
## Replace string environment variable in document
Given a sample.yml file of:
```yaml
{v: '${myenv}'}
v: ${myenv}
```
then
```bash
@ -221,7 +228,7 @@ myenv="cat meow" yq '.v |= envsubst' sample.yml
```
will output
```yaml
{v: 'cat meow'}
v: cat meow
```
## (Default) Return all envsubst errors

View File

@ -9,7 +9,11 @@ Tip: This can be a useful way to parameterise complex scripts.
## Dynamically evaluate a path
Given a sample.yml file of:
```yaml
{pathExp: '.a.b[] | select(.name == "cat")', a: {b: [{name: dog}, {name: cat}]}}
pathExp: .a.b[] | select(.name == "cat")
a:
b:
- name: dog
- name: cat
```
then
```bash
@ -17,7 +21,7 @@ yq 'eval(.pathExp)' sample.yml
```
will output
```yaml
{name: cat}
name: cat
```
## Dynamically update a path from an environment variable
@ -25,7 +29,10 @@ The env variable can be any valid yq expression.
Given a sample.yml file of:
```yaml
{a: {b: [{name: dog}, {name: cat}]}}
a:
b:
- name: dog
- name: cat
```
then
```bash
@ -33,6 +40,9 @@ pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueE
```
will output
```yaml
{a: {b: [{name: moo}, {name: cat}]}}
a:
b:
- name: moo
- name: cat
```

View File

@ -13,7 +13,7 @@ yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file
## Get filename
Given a sample.yml file of:
```yaml
{a: cat}
a: cat
```
then
```bash
@ -27,7 +27,7 @@ sample.yml
## Get file index
Given a sample.yml file of:
```yaml
{a: cat}
a: cat
```
then
```bash
@ -41,11 +41,11 @@ will output
## Get file indices of multiple documents
Given a sample.yml file of:
```yaml
{a: cat}
a: cat
```
And another sample another.yml file of:
```yaml
{a: cat}
a: cat
```
then
```bash
@ -60,7 +60,7 @@ will output
## Get file index alias
Given a sample.yml file of:
```yaml
{a: cat}
a: cat
```
then
```bash

View File

@ -6,7 +6,9 @@ Filters an array (or map values) by the expression given. Equivalent to doing `m
## Filter array
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -14,13 +16,19 @@ yq 'filter(. < 3)' sample.yml
```
will output
```yaml
[1, 2]
- 1
- 2
```
## Filter map values
Given a sample.yml file of:
```yaml
{c: {things: cool, frog: yes}, d: {things: hot, frog: false}}
c:
things: cool
frog: yes
d:
things: hot
frog: false
```
then
```bash
@ -28,6 +36,7 @@ yq 'filter(.things == "cool")' sample.yml
```
will output
```yaml
[{things: cool, frog: yes}]
- things: cool
frog: yes
```

View File

@ -6,7 +6,9 @@ Recursively flattens all arrays
Given a sample.yml file of:
```yaml
[1, [2], [[3]]]
- 1
- - 2
- - - 3
```
then
```bash
@ -14,13 +16,17 @@ yq 'flatten' sample.yml
```
will output
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
## Flatten with depth of one
Given a sample.yml file of:
```yaml
[1, [2], [[3]]]
- 1
- - 2
- - - 3
```
then
```bash
@ -28,13 +34,15 @@ yq 'flatten(1)' sample.yml
```
will output
```yaml
[1, 2, [3]]
- 1
- 2
- - 3
```
## Flatten empty array
Given a sample.yml file of:
```yaml
[[]]
- []
```
then
```bash
@ -48,7 +56,8 @@ will output
## Flatten array of objects
Given a sample.yml file of:
```yaml
[{foo: bar}, [{foo: baz}]]
- foo: bar
- - foo: baz
```
then
```bash
@ -56,6 +65,7 @@ yq 'flatten' sample.yml
```
will output
```yaml
[{foo: bar}, {foo: baz}]
- foo: bar
- foo: baz
```

View File

@ -5,7 +5,12 @@ This is used to group items in an array by an expression.
## Group by field
Given a sample.yml file of:
```yaml
[{foo: 1, bar: 10}, {foo: 3, bar: 100}, {foo: 1, bar: 1}]
- foo: 1
bar: 10
- foo: 3
bar: 100
- foo: 1
bar: 1
```
then
```bash
@ -13,15 +18,25 @@ yq 'group_by(.foo)' sample.yml
```
will output
```yaml
- - {foo: 1, bar: 10}
- {foo: 1, bar: 1}
- - {foo: 3, bar: 100}
- - foo: 1
bar: 10
- foo: 1
bar: 1
- - foo: 3
bar: 100
```
## Group by field, with nuls
Given a sample.yml file of:
```yaml
[{cat: dog}, {foo: 1, bar: 10}, {foo: 3, bar: 100}, {no: foo for you}, {foo: 1, bar: 1}]
- cat: dog
- foo: 1
bar: 10
- foo: 3
bar: 100
- no: foo for you
- foo: 1
bar: 1
```
then
```bash
@ -29,10 +44,13 @@ yq 'group_by(.foo)' sample.yml
```
will output
```yaml
- - {cat: dog}
- {no: foo for you}
- - {foo: 1, bar: 10}
- {foo: 1, bar: 1}
- - {foo: 3, bar: 100}
- - cat: dog
- no: foo for you
- - foo: 1
bar: 10
- foo: 1
bar: 1
- - foo: 3
bar: 100
```

View File

@ -5,7 +5,8 @@ Use the `keys` operator to return map keys or array indices.
## Map keys
Given a sample.yml file of:
```yaml
{dog: woof, cat: meow}
dog: woof
cat: meow
```
then
```bash
@ -20,7 +21,8 @@ will output
## Array keys
Given a sample.yml file of:
```yaml
[apple, banana]
- apple
- banana
```
then
```bash
@ -35,7 +37,9 @@ will output
## Retrieve array key
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash

View File

@ -7,7 +7,7 @@ returns length of string
Given a sample.yml file of:
```yaml
{a: cat}
a: cat
```
then
```bash
@ -21,7 +21,7 @@ will output
## null length
Given a sample.yml file of:
```yaml
{a: null}
a: null
```
then
```bash
@ -37,7 +37,8 @@ returns number of entries
Given a sample.yml file of:
```yaml
{a: cat, c: dog}
a: cat
c: dog
```
then
```bash
@ -53,7 +54,10 @@ returns number of elements
Given a sample.yml file of:
```yaml
[2, 4, 6, 8]
- 2
- 4
- 6
- 8
```
then
```bash

View File

@ -50,7 +50,7 @@ bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u
## Simple example
Given a sample.yml file of:
```yaml
{myFile: ../../examples/thing.yml}
myFile: ../../examples/thing.yml
```
then
```bash
@ -67,7 +67,8 @@ Note that you can modify the filename in the load operator if needed.
Given a sample.yml file of:
```yaml
{something: {file: thing.yml}}
something:
file: thing.yml
```
then
```bash
@ -75,7 +76,9 @@ yq '.something |= load("../../examples/" + .file)' sample.yml
```
will output
```yaml
{something: {a: apple is included, b: cool.}}
something:
a: apple is included
b: cool.
```
## Replace _all_ nodes with referenced file
@ -83,7 +86,11 @@ Recursively match all the nodes (`..`) and then filter the ones that have a 'fil
Given a sample.yml file of:
```yaml
{something: {file: thing.yml}, over: {here: [{file: thing.yml}]}}
something:
file: thing.yml
over:
here:
- file: thing.yml
```
then
```bash
@ -91,7 +98,13 @@ yq '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.yml
```
will output
```yaml
{something: {a: apple is included, b: cool.}, over: {here: [{a: apple is included, b: cool.}]}}
something:
a: apple is included
b: cool.
over:
here:
- a: apple is included
b: cool.
```
## Replace node with referenced file as string
@ -99,7 +112,8 @@ This will work for any text based file
Given a sample.yml file of:
```yaml
{something: {file: thing.yml}}
something:
file: thing.yml
```
then
```bash
@ -107,7 +121,9 @@ yq '.something |= load_str("../../examples/" + .file)' sample.yml
```
will output
```yaml
{something: "a: apple is included\nb: cool."}
something: |-
a: apple is included
b: cool.
```
## Load from XML

View File

@ -5,7 +5,9 @@ Maps values of an array. Use `map_values` to map values of an object.
## Map array
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -13,13 +15,17 @@ yq 'map(. + 1)' sample.yml
```
will output
```yaml
[2, 3, 4]
- 2
- 3
- 4
```
## Map object values
Given a sample.yml file of:
```yaml
{a: 1, b: 2, c: 3}
a: 1
b: 2
c: 3
```
then
```bash
@ -27,6 +33,8 @@ yq 'map_values(. + 1)' sample.yml
```
will output
```yaml
{a: 2, b: 3, c: 4}
a: 2
b: 3
c: 4
```

View File

@ -7,7 +7,8 @@ If the lhs and rhs are ints then the expression will be calculated with ints.
Given a sample.yml file of:
```yaml
{a: 13, b: 2}
a: 13
b: 2
```
then
```bash
@ -15,7 +16,8 @@ yq '.a = .a % .b' sample.yml
```
will output
```yaml
{a: 1, b: 2}
a: 1
b: 2
```
## Number modulo - float
@ -23,7 +25,8 @@ If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
```yaml
{a: 12, b: 2.5}
a: 12
b: 2.5
```
then
```bash
@ -31,7 +34,8 @@ yq '.a = .a % .b' sample.yml
```
will output
```yaml
{a: !!float 2, b: 2.5}
a: !!float 2
b: 2.5
```
## Number modulo - int by zero
@ -39,7 +43,8 @@ If the lhs is an int and rhs is a 0 the result is an error.
Given a sample.yml file of:
```yaml
{a: 1, b: 0}
a: 1
b: 0
```
then
```bash
@ -55,7 +60,8 @@ If the lhs is a float and rhs is a 0 the result is NaN.
Given a sample.yml file of:
```yaml
{a: 1.1, b: 0}
a: 1.1
b: 0
```
then
```bash
@ -63,6 +69,7 @@ yq '.a = .a % .b' sample.yml
```
will output
```yaml
{a: !!float NaN, b: 0}
a: !!float NaN
b: 0
```

View File

@ -10,7 +10,8 @@ Use `setpath` to set a value to the path array returned by `path`, and similarly
## Map path
Given a sample.yml file of:
```yaml
{a: {b: cat}}
a:
b: cat
```
then
```bash
@ -25,7 +26,8 @@ will output
## Get map key
Given a sample.yml file of:
```yaml
{a: {b: cat}}
a:
b: cat
```
then
```bash
@ -39,7 +41,9 @@ b
## Array path
Given a sample.yml file of:
```yaml
{a: [cat, dog]}
a:
- cat
- dog
```
then
```bash
@ -54,7 +58,9 @@ will output
## Get array index
Given a sample.yml file of:
```yaml
{a: [cat, dog]}
a:
- cat
- dog
```
then
```bash
@ -68,7 +74,10 @@ will output
## Print path and value
Given a sample.yml file of:
```yaml
{a: [cat, dog, frog]}
a:
- cat
- dog
- frog
```
then
```bash
@ -89,7 +98,8 @@ will output
## Set path
Given a sample.yml file of:
```yaml
{a: {b: cat}}
a:
b: cat
```
then
```bash
@ -97,7 +107,8 @@ yq 'setpath(["a", "b"]; "things")' sample.yml
```
will output
```yaml
{a: {b: things}}
a:
b: things
```
## Set on empty document
@ -172,7 +183,10 @@ Notice delpaths takes an _array_ of paths.
Given a sample.yml file of:
```yaml
{a: {b: cat, c: dog, d: frog}}
a:
b: cat
c: dog
d: frog
```
then
```bash
@ -180,7 +194,8 @@ yq 'delpaths([["a", "c"], ["a", "d"]])' sample.yml
```
will output
```yaml
{a: {b: cat}}
a:
b: cat
```
## Delete array path

View File

@ -31,7 +31,9 @@ Note that the order of the indices matches the pick order and non existent indic
Given a sample.yml file of:
```yaml
[cat, leopard, lion]
- cat
- leopard
- lion
```
then
```bash
@ -39,6 +41,7 @@ yq 'pick([2, 0, 734, -5])' sample.yml
```
will output
```yaml
[lion, cat]
- lion
- cat
```

View File

@ -5,7 +5,9 @@ Reverses the order of the items in an array
## Reverse
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -13,7 +15,9 @@ yq 'reverse' sample.yml
```
will output
```yaml
[3, 2, 1]
- 3
- 2
- 1
```
## Sort descending by string field
@ -21,7 +25,9 @@ Use sort with reverse to sort in descending order.
Given a sample.yml file of:
```yaml
[{a: banana}, {a: cat}, {a: apple}]
- a: banana
- a: cat
- a: apple
```
then
```bash
@ -29,6 +35,8 @@ yq 'sort_by(.a) | reverse' sample.yml
```
will output
```yaml
[{a: cat}, {a: banana}, {a: apple}]
- a: cat
- a: banana
- a: apple
```

View File

@ -7,7 +7,10 @@ You may leave out the first or second number, which will will refer to the start
## Slicing arrays
Given a sample.yml file of:
```yaml
[cat, dog, frog, cow]
- cat
- dog
- frog
- cow
```
then
```bash
@ -24,7 +27,10 @@ Starts from the start of the array
Given a sample.yml file of:
```yaml
[cat, dog, frog, cow]
- cat
- dog
- frog
- cow
```
then
```bash
@ -41,7 +47,10 @@ Finishes at the end of the array
Given a sample.yml file of:
```yaml
[cat, dog, frog, cow]
- cat
- dog
- frog
- cow
```
then
```bash
@ -56,7 +65,10 @@ will output
## Slicing arrays - use negative numbers to count backwards from the end
Given a sample.yml file of:
```yaml
[cat, dog, frog, cow]
- cat
- dog
- frog
- cow
```
then
```bash
@ -73,7 +85,10 @@ using an expression to find the index
Given a sample.yml file of:
```yaml
[cat, dog, frog, cow]
- cat
- dog
- frog
- cow
```
then
```bash

View File

@ -18,7 +18,9 @@ See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-key
## Sort keys of map
Given a sample.yml file of:
```yaml
{c: frog, a: blah, b: bing}
c: frog
a: blah
b: bing
```
then
```bash
@ -26,7 +28,9 @@ yq 'sort_keys(.)' sample.yml
```
will output
```yaml
{a: blah, b: bing, c: frog}
a: blah
b: bing
c: frog
```
## Sort keys recursively
@ -34,7 +38,19 @@ Note the array elements are left unsorted, but maps inside arrays are sorted
Given a sample.yml file of:
```yaml
{bParent: {c: dog, array: [3, 1, 2]}, aParent: {z: donkey, x: [{c: yum, b: delish}, {b: ew, a: apple}]}}
bParent:
c: dog
array:
- 3
- 1
- 2
aParent:
z: donkey
x:
- c: yum
b: delish
- b: ew
a: apple
```
then
```bash
@ -42,6 +58,18 @@ yq 'sort_keys(..)' sample.yml
```
will output
```yaml
{aParent: {x: [{b: delish, c: yum}, {a: apple, b: ew}], z: donkey}, bParent: {array: [3, 1, 2], c: dog}}
aParent:
x:
- b: delish
c: yum
- a: apple
b: ew
z: donkey
bParent:
array:
- 3
- 1
- 2
c: dog
```

View File

@ -10,7 +10,9 @@ Note that at this stage, `yq` only sorts scalar fields.
## Sort by string field
Given a sample.yml file of:
```yaml
[{a: banana}, {a: cat}, {a: apple}]
- a: banana
- a: cat
- a: apple
```
then
```bash
@ -18,13 +20,19 @@ yq 'sort_by(.a)' sample.yml
```
will output
```yaml
[{a: apple}, {a: banana}, {a: cat}]
- a: apple
- a: banana
- a: cat
```
## Sort by multiple fields
Given a sample.yml file of:
```yaml
[{a: dog}, {a: cat, b: banana}, {a: cat, b: apple}]
- a: dog
- a: cat
b: banana
- a: cat
b: apple
```
then
```bash
@ -32,7 +40,11 @@ yq 'sort_by(.a, .b)' sample.yml
```
will output
```yaml
[{a: cat, b: apple}, {a: cat, b: banana}, {a: dog}]
- a: cat
b: apple
- a: cat
b: banana
- a: dog
```
## Sort descending by string field
@ -40,7 +52,9 @@ Use sort with reverse to sort in descending order.
Given a sample.yml file of:
```yaml
[{a: banana}, {a: cat}, {a: apple}]
- a: banana
- a: cat
- a: apple
```
then
```bash
@ -48,7 +62,9 @@ yq 'sort_by(.a) | reverse' sample.yml
```
will output
```yaml
[{a: cat}, {a: banana}, {a: apple}]
- a: cat
- a: banana
- a: apple
```
## Sort array in place
@ -98,7 +114,14 @@ Note the order of the elements in unchanged when equal in sorting.
Given a sample.yml file of:
```yaml
[{a: banana, b: 1}, {a: banana, b: 2}, {a: banana, b: 3}, {a: banana, b: 4}]
- a: banana
b: 1
- a: banana
b: 2
- a: banana
b: 3
- a: banana
b: 4
```
then
```bash
@ -106,13 +129,22 @@ yq 'sort_by(.a)' sample.yml
```
will output
```yaml
[{a: banana, b: 1}, {a: banana, b: 2}, {a: banana, b: 3}, {a: banana, b: 4}]
- a: banana
b: 1
- a: banana
b: 2
- a: banana
b: 3
- a: banana
b: 4
```
## Sort by numeric field
Given a sample.yml file of:
```yaml
[{a: 10}, {a: 100}, {a: 1}]
- a: 10
- a: 100
- a: 1
```
then
```bash
@ -120,13 +152,17 @@ yq 'sort_by(.a)' sample.yml
```
will output
```yaml
[{a: 1}, {a: 10}, {a: 100}]
- a: 1
- a: 10
- a: 100
```
## Sort by custom date field
Given a sample.yml file of:
```yaml
[{a: 12-Jun-2011}, {a: 23-Dec-2010}, {a: 10-Aug-2011}]
- a: 12-Jun-2011
- a: 23-Dec-2010
- a: 10-Aug-2011
```
then
```bash
@ -134,13 +170,21 @@ yq 'with_dtf("02-Jan-2006"; sort_by(.a))' sample.yml
```
will output
```yaml
[{a: 23-Dec-2010}, {a: 12-Jun-2011}, {a: 10-Aug-2011}]
- a: 23-Dec-2010
- a: 12-Jun-2011
- a: 10-Aug-2011
```
## Sort, nulls come first
Given a sample.yml file of:
```yaml
[8, 3, null, 6, true, false, cat]
- 8
- 3
- null
- 6
- true
- false
- cat
```
then
```bash
@ -148,6 +192,12 @@ yq 'sort' sample.yml
```
will output
```yaml
[null, false, true, 3, 6, 8, cat]
- null
- false
- true
- 3
- 6
- 8
- cat
```

View File

@ -91,7 +91,11 @@ will output
## Join strings
Given a sample.yml file of:
```yaml
[cat, meow, 1, null, true]
- cat
- meow
- 1
- null
- true
```
then
```bash
@ -105,7 +109,10 @@ cat; meow; 1; ; true
## Trim strings
Given a sample.yml file of:
```yaml
[' cat', 'dog ', ' cow cow ', horse]
- ' cat'
- 'dog '
- ' cow cow '
- horse
```
then
```bash
@ -277,7 +284,8 @@ Like jq's equivalent, this works like match but only returns true/false instead
Given a sample.yml file of:
```yaml
[cat, dog]
- cat
- dog
```
then
```bash
@ -346,7 +354,7 @@ b: !goat heart
## Split strings
Given a sample.yml file of:
```yaml
"cat; meow; 1; ; true"
cat; meow; 1; ; true
```
then
```bash
@ -364,7 +372,7 @@ will output
## Split strings one match
Given a sample.yml file of:
```yaml
"word"
word
```
then
```bash

View File

@ -28,7 +28,9 @@ Note that order of the keys does not matter
Given a sample.yml file of:
```yaml
[{a: b, c: d}, {a: b}]
- a: b
c: d
- a: b
```
then
```bash
@ -36,7 +38,7 @@ yq '. - [{"c": "d", "a": "b"}]' sample.yml
```
will output
```yaml
[{a: b}]
- a: b
```
## Number subtraction - float
@ -44,7 +46,8 @@ If the lhs or rhs are floats then the expression will be calculated with floats.
Given a sample.yml file of:
```yaml
{a: 3, b: 4.5}
a: 3
b: 4.5
```
then
```bash
@ -52,7 +55,8 @@ yq '.a = .a - .b' sample.yml
```
will output
```yaml
{a: -1.5, b: 4.5}
a: -1.5
b: 4.5
```
## Number subtraction - int
@ -60,7 +64,8 @@ If both the lhs and rhs are ints then the expression will be calculated with int
Given a sample.yml file of:
```yaml
{a: 3, b: 4}
a: 3
b: 4
```
then
```bash
@ -68,13 +73,15 @@ yq '.a = .a - .b' sample.yml
```
will output
```yaml
{a: -1, b: 4}
a: -1
b: 4
```
## Decrement numbers
Given a sample.yml file of:
```yaml
{a: 3, b: 5}
a: 3
b: 5
```
then
```bash
@ -82,7 +89,8 @@ yq '.[] -= 1' sample.yml
```
will output
```yaml
{a: 2, b: 4}
a: 2
b: 4
```
## Date subtraction

View File

@ -5,7 +5,11 @@ The tag operator can be used to get or set the tag of nodes (e.g. `!!str`, `!!in
## Get tag
Given a sample.yml file of:
```yaml
{a: cat, b: 5, c: 3.2, e: true, f: []}
a: cat
b: 5
c: 3.2
e: true
f: []
```
then
```bash
@ -24,7 +28,11 @@ will output
## type is an alias for tag
Given a sample.yml file of:
```yaml
{a: cat, b: 5, c: 3.2, e: true, f: []}
a: cat
b: 5
c: 3.2
e: true
f: []
```
then
```bash
@ -43,7 +51,7 @@ will output
## Set custom tag
Given a sample.yml file of:
```yaml
{a: str}
a: str
```
then
```bash
@ -51,13 +59,16 @@ yq '.a tag = "!!mikefarah"' sample.yml
```
will output
```yaml
{a: !!mikefarah str}
a: !!mikefarah str
```
## Find numbers and convert them to strings
Given a sample.yml file of:
```yaml
{a: cat, b: 5, c: 3.2, e: true}
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
@ -65,6 +76,9 @@ yq '(.. | select(tag == "!!int")) tag= "!!str"' sample.yml
```
will output
```yaml
{a: cat, b: "5", c: 3.2, e: true}
a: cat
b: "5"
c: 3.2
e: true
```

View File

@ -5,7 +5,8 @@ This is the simplest (and perhaps most used) operator. It is used to navigate de
## Simple map navigation
Given a sample.yml file of:
```yaml
{a: {b: apple}}
a:
b: apple
```
then
```bash
@ -13,7 +14,7 @@ yq '.a' sample.yml
```
will output
```yaml
{b: apple}
b: apple
```
## Splat
@ -21,7 +22,8 @@ Often used to pipe children into other operators
Given a sample.yml file of:
```yaml
[{b: apple}, {c: banana}]
- b: apple
- c: banana
```
then
```bash
@ -29,8 +31,8 @@ yq '.[]' sample.yml
```
will output
```yaml
{b: apple}
{c: banana}
b: apple
c: banana
```
## Optional Splat
@ -38,7 +40,7 @@ Just like splat, but won't error if you run it against scalars
Given a sample.yml file of:
```yaml
"cat"
cat
```
then
```bash
@ -53,7 +55,7 @@ Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
```yaml
{"{}": frog}
"{}": frog
```
then
```bash
@ -85,7 +87,7 @@ Use quotes with square brackets around path elements with special characters
Given a sample.yml file of:
```yaml
{"red rabbit": frog}
"red rabbit": frog
```
then
```bash
@ -101,7 +103,9 @@ Expressions within [] can be used to dynamically lookup / calculate keys
Given a sample.yml file of:
```yaml
{b: apple, apple: crispy yum, banana: soft yum}
b: apple
apple: crispy yum
banana: soft yum
```
then
```bash
@ -117,7 +121,7 @@ Nodes are added dynamically while traversing
Given a sample.yml file of:
```yaml
{c: banana}
c: banana
```
then
```bash
@ -133,7 +137,9 @@ Like jq, does not output an error when the yaml is not an array or object as exp
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -146,7 +152,9 @@ will output
## Wildcard matching
Given a sample.yml file of:
```yaml
{a: {cat: apple, mad: things}}
a:
cat: apple
mad: things
```
then
```bash
@ -161,7 +169,9 @@ things
## Aliases
Given a sample.yml file of:
```yaml
{a: &cat {c: frog}, b: *cat}
a: &cat
c: frog
b: *cat
```
then
```bash
@ -175,7 +185,9 @@ will output
## Traversing aliases with splat
Given a sample.yml file of:
```yaml
{a: &cat {c: frog}, b: *cat}
a: &cat
c: frog
b: *cat
```
then
```bash
@ -189,7 +201,9 @@ frog
## Traversing aliases explicitly
Given a sample.yml file of:
```yaml
{a: &cat {c: frog}, b: *cat}
a: &cat
c: frog
b: *cat
```
then
```bash
@ -203,7 +217,9 @@ frog
## Traversing arrays by index
Given a sample.yml file of:
```yaml
[1, 2, 3]
- 1
- 2
- 3
```
then
```bash
@ -231,7 +247,7 @@ cat
## Maps with numeric keys
Given a sample.yml file of:
```yaml
{2: cat}
2: cat
```
then
```bash
@ -245,7 +261,7 @@ cat
## Maps with non existing numeric keys
Given a sample.yml file of:
```yaml
{a: b}
a: b
```
then
```bash
@ -452,7 +468,10 @@ foobarList_c
## Select multiple indices
Given a sample.yml file of:
```yaml
{a: [a, b, c]}
a:
- a
- b
- c
```
then
```bash

View File

@ -8,7 +8,10 @@ Note that unique maintains the original order of the array.
Given a sample.yml file of:
```yaml
[2, 1, 3, 2]
- 2
- 1
- 3
- 2
```
then
```bash
@ -16,7 +19,9 @@ yq 'unique' sample.yml
```
will output
```yaml
[2, 1, 3]
- 2
- 1
- 3
```
## Unique nulls
@ -24,7 +29,10 @@ Unique works on the node value, so it considers different representations of nul
Given a sample.yml file of:
```yaml
[~, null, ~, null]
- ~
- null
- ~
- null
```
then
```bash
@ -32,7 +40,8 @@ yq 'unique' sample.yml
```
will output
```yaml
[~, null]
- ~
- null
```
## Unique all nulls
@ -40,7 +49,10 @@ Run against the node tag to unique all the nulls
Given a sample.yml file of:
```yaml
[~, null, ~, null]
- ~
- null
- ~
- null
```
then
```bash
@ -48,13 +60,18 @@ yq 'unique_by(tag)' sample.yml
```
will output
```yaml
[~]
- ~
```
## Unique array object fields
Given a sample.yml file of:
```yaml
[{name: harry, pet: cat}, {name: billy, pet: dog}, {name: harry, pet: dog}]
- name: harry
pet: cat
- name: billy
pet: dog
- name: harry
pet: dog
```
then
```bash
@ -62,6 +79,9 @@ yq 'unique_by(.name)' sample.yml
```
will output
```yaml
[{name: harry, pet: cat}, {name: billy, pet: dog}]
- name: harry
pet: cat
- name: billy
pet: dog
```

View File

@ -21,7 +21,8 @@ cat
## Multi value variable
Given a sample.yml file of:
```yaml
[cat, dog]
- cat
- dog
```
then
```bash
@ -38,7 +39,14 @@ Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicB
Given a sample.yml file of:
```yaml
{"posts": [{"title": First post, "author": anon}, {"title": A well-written article, "author": person1}], "realnames": {"anon": Anonymous Coward, "person1": Person McPherson}}
"posts":
- "title": First post
"author": anon
- "title": A well-written article
"author": person1
"realnames":
"anon": Anonymous Coward
"person1": Person McPherson
```
then
```bash

View File

@ -83,6 +83,7 @@ yq -o=props '... comments = ""' sample.yml
```
will output
```properties
# block comments come through
person.name = Mike Wazowski
person.pets.0 = cat
person.food.0 = pizza

View File

@ -64,13 +64,13 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod
candidate.HeadComment = comment
candidate.LeadingContent = "" // clobber the leading content, if there was any.
}
if preferences.FootComment && candidate.Kind == DocumentNode && comment != "" {
log.Debugf("AssignComments - setting line comment to %v", comment)
candidate.TrailingContent = "# " + comment
} else if preferences.FootComment && candidate.Kind == DocumentNode {
log.Debugf("AssignComments - setting line comment to %v", comment)
candidate.TrailingContent = comment
} else if preferences.FootComment && candidate.Kind != DocumentNode {
// if preferences.FootComment && candidate.Kind == DocumentNode && comment != "" {
// log.Debugf("AssignComments - setting line comment to %v", comment)
// candidate.TrailingContent = "# " + comment
// } else if preferences.FootComment && candidate.Kind == DocumentNode {
// log.Debugf("AssignComments - setting line comment to %v", comment)
// candidate.TrailingContent = comment
if preferences.FootComment { //&& candidate.Kind != DocumentNode {
candidate.FootComment = comment
candidate.TrailingContent = ""
}

View File

@ -17,24 +17,23 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *
if candidate.Kind == DocumentNode {
//need to delete this node from context.
newResults := list.New()
for item := context.MatchingNodes.Front(); item != nil; item = item.Next() {
nodeInContext := item.Value.(*CandidateNode)
if nodeInContext != candidate {
newResults.PushBack(nodeInContext)
} else {
log.Info("Need to delete this %v", NodeToString(nodeInContext))
}
}
return context.ChildContext(newResults), nil
return removeFromContext(context, candidate)
} else if candidate.Parent == nil {
//problem: context may already be '.a' and then I pass in '.a.a2'.
// should pass in .a2.
log.Info("Could not find parent of %v", NodeToString(candidate))
return context, nil
}
log.Debugf("processing deletion of candidate %v", NodeToString(candidate))
parentNode := candidate.Parent
if parentNode != nil && parentNode.Kind == DocumentNode {
log.Debugf("it has a document parent")
return removeFromContext(context, candidate.Parent)
}
candidatePath := candidate.GetPath()
childPath := candidatePath[len(candidatePath)-1]
@ -49,6 +48,19 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *
return context, nil
}
func removeFromContext(context Context, candidate *CandidateNode) (Context, error) {
newResults := list.New()
for item := context.MatchingNodes.Front(); item != nil; item = item.Next() {
nodeInContext := item.Value.(*CandidateNode)
if nodeInContext != candidate {
newResults.PushBack(nodeInContext)
} else {
log.Info("Need to delete this %v", NodeToString(nodeInContext))
}
}
return context.ChildContext(newResults), nil
}
func deleteFromMap(candidate *CandidateNode, childPath interface{}) {
log.Debug("deleteFromMap")
node := candidate.unwrapDocument()

View File

@ -127,6 +127,12 @@ var deleteOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::a: [x, x]\n",
},
},
{
skipDoc: true,
document: `a: null`,
expression: `del(..)`,
expected: []string{},
},
{
skipDoc: true,
document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`,

View File

@ -33,25 +33,28 @@ func multiplyOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
return crossFunction(d, context, expressionNode, multiply(expressionNode.Operation.Preferences.(multiplyPreferences)), false)
}
func getComments(lhs *CandidateNode, rhs *CandidateNode) (leadingContent string, headComment string, footComment string) {
func getComments(lhs *CandidateNode, rhs *CandidateNode) (leadingContent string, headComment string, footComment string, trailingContent string) {
leadingContent = rhs.LeadingContent
headComment = rhs.HeadComment
footComment = rhs.FootComment
trailingContent = rhs.TrailingContent
if lhs.HeadComment != "" || lhs.LeadingContent != "" {
headComment = lhs.HeadComment
leadingContent = lhs.LeadingContent
}
if lhs.FootComment != "" {
if lhs.FootComment != "" || lhs.TrailingContent != "" {
footComment = lhs.FootComment
trailingContent = lhs.TrailingContent
}
return leadingContent, headComment, footComment
return leadingContent, headComment, footComment, trailingContent
}
func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
// need to do this before unWrapping the potential document node
leadingContent, headComment, footComment := getComments(lhs, rhs)
leadingContent, headComment, footComment, trailingContent := getComments(lhs, rhs)
lhs = lhs.unwrapDocument()
rhs = rhs.unwrapDocument()
log.Debugf("Multiplying LHS: %v", NodeToString(lhs))
@ -71,6 +74,7 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex
newBlank.LeadingContent = leadingContent
newBlank.HeadComment = headComment
newBlank.FootComment = footComment
newBlank.TrailingContent = trailingContent
return mergeObjects(d, context.WritableClone(), newBlank, rhs, preferences)
}

View File

@ -45,7 +45,7 @@ var pathOperatorScenarios = []expressionScenario{
document: `{a: {b: cat}}`,
expression: `.a.b | path | .[-1]`,
expected: []string{
"D0, P[a b -1], (!!str)::b\n",
"D0, P[a b 1], (!!str)::b\n",
},
},
{
@ -61,7 +61,7 @@ var pathOperatorScenarios = []expressionScenario{
document: `{a: [cat, dog]}`,
expression: `.a.[] | select(. == "dog") | path | .[-1]`,
expected: []string{
"D0, P[a 1 -1], (!!int)::1\n",
"D0, P[a 1 1], (!!int)::1\n",
},
},
{

View File

@ -25,6 +25,8 @@ func recursiveDecent(results *list.List, context Context, preferences recursiveD
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
candidate = candidate.unwrapDocument()
log.Debugf("Recursive Decent, added %v", NodeToString(candidate))
results.PushBack(candidate)

View File

@ -10,7 +10,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{}`,
expression: `..`,
expected: []string{
"D0, P[], (!!map)::{}\n",
"D0, P[], (doc)::{}\n",
},
},
{
@ -18,7 +18,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{}`,
expression: `...`,
expected: []string{
"D0, P[], (!!map)::{}\n",
"D0, P[], (doc)::{}\n",
},
},
{
@ -26,7 +26,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[]`,
expression: `..`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
"D0, P[], (doc)::[]\n",
},
},
{
@ -34,7 +34,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[]`,
expression: `...`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
"D0, P[], (doc)::[]\n",
},
},
{
@ -42,7 +42,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `cat`,
expression: `..`,
expected: []string{
"D0, P[], (!!str)::cat\n",
"D0, P[], (doc)::cat\n",
},
},
{
@ -50,7 +50,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `cat`,
expression: `...`,
expected: []string{
"D0, P[], (!!str)::cat\n",
"D0, P[], (doc)::cat\n",
},
},
{
@ -58,7 +58,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: frog}`,
expression: `..`,
expected: []string{
"D0, P[], (!!map)::{a: frog}\n",
"D0, P[], (doc)::{a: frog}\n",
"D0, P[a], (!!str)::frog\n",
},
},
@ -86,7 +86,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: frog}`,
expression: `...`,
expected: []string{
"D0, P[], (!!map)::{a: frog}\n",
"D0, P[], (doc)::{a: frog}\n",
"D0, P[a], (!!str)::a\n",
"D0, P[a], (!!str)::frog\n",
},
@ -96,7 +96,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: {b: apple}}`,
expression: `..`,
expected: []string{
"D0, P[], (!!map)::{a: {b: apple}}\n",
"D0, P[], (doc)::{a: {b: apple}}\n",
"D0, P[a], (!!map)::{b: apple}\n",
"D0, P[a b], (!!str)::apple\n",
},
@ -106,7 +106,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: {b: apple}}`,
expression: `...`,
expected: []string{
"D0, P[], (!!map)::{a: {b: apple}}\n",
"D0, P[], (doc)::{a: {b: apple}}\n",
"D0, P[a], (!!str)::a\n",
"D0, P[a], (!!map)::{b: apple}\n",
"D0, P[a b], (!!str)::b\n",
@ -118,7 +118,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[1,2,3]`,
expression: `..`,
expected: []string{
"D0, P[], (!!seq)::[1, 2, 3]\n",
"D0, P[], (doc)::[1, 2, 3]\n",
"D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n",
"D0, P[2], (!!int)::3\n",
@ -129,7 +129,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[1,2,3]`,
expression: `...`,
expected: []string{
"D0, P[], (!!seq)::[1, 2, 3]\n",
"D0, P[], (doc)::[1, 2, 3]\n",
"D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n",
"D0, P[2], (!!int)::3\n",
@ -140,7 +140,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[{a: cat},2,true]`,
expression: `..`,
expected: []string{
"D0, P[], (!!seq)::[{a: cat}, 2, true]\n",
"D0, P[], (doc)::[{a: cat}, 2, true]\n",
"D0, P[0], (!!map)::{a: cat}\n",
"D0, P[0 a], (!!str)::cat\n",
"D0, P[1], (!!int)::2\n",
@ -152,7 +152,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[{a: cat},2,true]`,
expression: `...`,
expected: []string{
"D0, P[], (!!seq)::[{a: cat}, 2, true]\n",
"D0, P[], (doc)::[{a: cat}, 2, true]\n",
"D0, P[0], (!!map)::{a: cat}\n",
"D0, P[0 a], (!!str)::a\n",
"D0, P[0 a], (!!str)::cat\n",
@ -173,7 +173,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: &cat {c: frog}, b: *cat}`,
expression: `...`,
expected: []string{
"D0, P[], (!!map)::{a: &cat {c: frog}, b: *cat}\n",
"D0, P[], (doc)::{a: &cat {c: frog}, b: *cat}\n",
"D0, P[a], (!!str)::a\n",
"D0, P[a], (!!map)::&cat {c: frog}\n",
"D0, P[a c], (!!str)::c\n",

View File

@ -7,6 +7,7 @@ func splitDocumentOperator(d *dataTreeNavigator, context Context, expressionNode
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
candidate.SetDocument(index)
candidate.SetParent(nil)
index = index + 1
}

View File

@ -50,7 +50,7 @@ func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode *
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
log.Debugf("Setting style of : %v", candidate.GetKey())
log.Debugf("Setting style of : %v", NodeToString(candidate))
if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil {

View File

@ -26,7 +26,7 @@ var styleOperatorScenarios = []expressionScenario{
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="tagged"`,
expected: []string{
"D0, P[], (!!map)::!!map\na: !!str cat\nb: !!int 5\nc: !!float 3.2\ne: !!bool true\n",
"D0, P[], (doc)::!!map\na: !!str cat\nb: !!int 5\nc: !!float 3.2\ne: !!bool true\n",
},
},
{
@ -34,7 +34,7 @@ var styleOperatorScenarios = []expressionScenario{
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="double"`,
expected: []string{
"D0, P[], (!!map)::a: \"cat\"\nb: \"5\"\nc: \"3.2\"\ne: \"true\"\n",
"D0, P[], (doc)::a: \"cat\"\nb: \"5\"\nc: \"3.2\"\ne: \"true\"\n",
},
},
{
@ -42,7 +42,7 @@ var styleOperatorScenarios = []expressionScenario{
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `... style="double"`,
expected: []string{
"D0, P[], (!!map)::\"a\": \"cat\"\n\"b\": \"5\"\n\"c\": \"3.2\"\n\"e\": \"true\"\n",
"D0, P[], (doc)::\"a\": \"cat\"\n\"b\": \"5\"\n\"c\": \"3.2\"\n\"e\": \"true\"\n",
},
},
{
@ -50,7 +50,7 @@ var styleOperatorScenarios = []expressionScenario{
document: "bing: &foo frog\na:\n c: cat\n <<: [*foo]",
expression: `(... | select(tag=="!!str")) style="single"`,
expected: []string{
"D0, P[], (!!map)::'bing': &foo 'frog'\n'a':\n 'c': 'cat'\n !!merge <<: [*foo]\n",
"D0, P[], (doc)::'bing': &foo 'frog'\n'a':\n 'c': 'cat'\n !!merge <<: [*foo]\n",
},
},
{
@ -58,7 +58,7 @@ var styleOperatorScenarios = []expressionScenario{
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="single"`,
expected: []string{
"D0, P[], (!!map)::a: 'cat'\nb: '5'\nc: '3.2'\ne: 'true'\n",
"D0, P[], (doc)::a: 'cat'\nb: '5'\nc: '3.2'\ne: 'true'\n",
},
},
{
@ -66,7 +66,7 @@ var styleOperatorScenarios = []expressionScenario{
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="literal"`,
expected: []string{
`D0, P[], (!!map)::a: |-
`D0, P[], (doc)::a: |-
cat
b: |-
5
@ -82,7 +82,7 @@ e: |-
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="folded"`,
expected: []string{
`D0, P[], (!!map)::a: >-
`D0, P[], (doc)::a: >-
cat
b: >-
5
@ -98,7 +98,7 @@ e: >-
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="flow"`,
expected: []string{
"D0, P[], (!!map)::{a: cat, b: 5, c: 3.2, e: true}\n",
"D0, P[], (doc)::{a: cat, b: 5, c: 3.2, e: true}\n",
},
},
{
@ -107,7 +107,7 @@ e: >-
document: `{a: cat, "b": 5, 'c': 3.2, "e": true}`,
expression: `... style=""`,
expected: []string{
"D0, P[], (!!map)::a: cat\nb: 5\nc: 3.2\ne: true\n",
"D0, P[], (doc)::a: cat\nb: 5\nc: 3.2\ne: true\n",
},
},
{