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.Content = make([]*CandidateNode, 0)
n.Kind = other.Kind
n.AddChildren(other.Content) n.AddChildren(other.Content)
n.Kind = other.Kind
n.Value = other.Value n.Value = other.Value
n.UpdateAttributesFrom(other, prefs) 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 ## Concatenate arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: [1, 2], b: [3, 4]} a:
- 1
- 2
b:
- 3
- 4
``` ```
then then
```bash ```bash
@ -20,7 +25,10 @@ yq '.a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2, 3, 4] - 1
- 2
- 3
- 4
``` ```
## Concatenate to existing array ## Concatenate to existing array
@ -48,7 +56,9 @@ b:
## 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:
- 1
- 2
``` ```
then then
```bash ```bash
@ -56,7 +66,8 @@ yq '.a + null' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2] - 1
- 2
``` ```
## Append to existing array ## Append to existing array
@ -143,7 +154,8 @@ a:
## String concatenation ## String concatenation
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: meow} a: cat
b: meow
``` ```
then then
```bash ```bash
@ -151,7 +163,8 @@ yq '.a += .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: catmeow, b: meow} a: catmeow
b: meow
``` ```
## Number addition - float ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: 3, b: 4.9} a: 3
b: 4.9
``` ```
then then
```bash ```bash
@ -167,7 +181,8 @@ yq '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 7.9, b: 4.9} a: 7.9
b: 4.9
``` ```
## Number addition - int ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: 3, b: 4} a: 3
b: 4
``` ```
then then
```bash ```bash
@ -183,13 +199,15 @@ yq '.a = .a + .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 7, b: 4} a: 7
b: 4
``` ```
## Increment numbers ## Increment numbers
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 3, b: 5} a: 3
b: 5
``` ```
then then
```bash ```bash
@ -197,7 +215,8 @@ yq '.[] += 1' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 4, b: 6} a: 4
b: 6
``` ```
## Date addition ## Date addition

View File

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

View File

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

View File

@ -27,7 +27,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:
b:
g: foof
``` ```
then then
```bash ```bash
@ -35,13 +37,16 @@ yq '.a |= .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {g: foof}} a:
g: foof
``` ```
## Double elements in an array ## Double elements in an array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -49,7 +54,9 @@ yq '.[] |= . * 2' sample.yml
``` ```
will output will output
```yaml ```yaml
[2, 4, 6] - 2
- 4
- 6
``` ```
## Update node from another file ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: apples} a: apples
``` ```
And another sample another.yml file of: And another sample another.yml file of:
```yaml ```yaml
{b: bob} b: bob
``` ```
then then
```bash ```bash
@ -69,13 +76,16 @@ yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0
``` ```
will output will output
```yaml ```yaml
{a: {b: bob}} a:
b: bob
``` ```
## 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}, b: sibling} a:
b: child
b: sibling
``` ```
then then
```bash ```bash
@ -83,13 +93,16 @@ yq '.a = .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: sibling, b: sibling} a: sibling
b: sibling
``` ```
## Updated multiple paths ## Updated multiple paths
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: fieldA, b: fieldB, c: fieldC} a: fieldA
b: fieldB
c: fieldC
``` ```
then then
```bash ```bash
@ -97,13 +110,16 @@ yq '(.a, .c) = "potato"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: potato, b: fieldB, c: potato} a: potato
b: fieldB
c: potato
``` ```
## Update string value ## Update string value
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {b: apple}} a:
b: apple
``` ```
then then
```bash ```bash
@ -111,7 +127,8 @@ yq '.a.b = "frog"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {b: frog}} a:
b: frog
``` ```
## Update string value via |= ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: {b: apple}} a:
b: apple
``` ```
then then
```bash ```bash
@ -127,7 +145,8 @@ yq '.a.b |= "frog"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {b: frog}} a:
b: frog
``` ```
## Update deeply selected results ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: {b: apple, c: cactus}} a:
b: apple
c: cactus
``` ```
then then
```bash ```bash
@ -143,13 +164,17 @@ yq '(.a[] | select(. == "apple")) = "frog"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {b: frog, c: cactus}} a:
b: frog
c: cactus
``` ```
## Update array values ## Update array values
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[candy, apple, sandy] - candy
- apple
- sandy
``` ```
then then
```bash ```bash
@ -157,7 +182,9 @@ yq '(.[] | select(. == "*andy")) = "bogs"' sample.yml
``` ```
will output will output
```yaml ```yaml
[bogs, apple, bogs] - bogs
- apple
- bogs
``` ```
## Update empty object ## 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: Given a sample.yml file of:
```yaml ```yaml
[yes, no] - yes
- no
``` ```
then then
```bash ```bash
@ -56,7 +57,12 @@ false
## Matching nodes with select, equals and or ## Matching nodes with select, equals and or
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -64,14 +70,17 @@ yq '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
``` ```
will output will output
```yaml ```yaml
- {a: bird, b: dog} - a: bird
- {a: cat, b: fly} b: dog
- a: cat
b: fly
``` ```
## `any` returns true if any boolean in a given array is true ## `any` returns true if any boolean in a given array is true
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[false, true] - false
- true
``` ```
then then
```bash ```bash
@ -119,7 +128,8 @@ b: false
## `all` returns true if all booleans in a given array are true ## `all` returns true if all booleans in a given array are true
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[true, true] - true
- true
``` ```
then then
```bash ```bash

View File

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

View File

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

View File

@ -15,7 +15,9 @@ Array is equal or subset of
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[foobar, foobaz, blarp] - foobar
- foobaz
- blarp
``` ```
then then
```bash ```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: Given a sample.yml file of:
```yaml ```yaml
[foobar, foobaz, blarp] - foobar
- foobaz
- blarp
``` ```
then then
```bash ```bash
@ -45,7 +49,12 @@ false
## Object included in array ## Object included in array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{"foo": 12, "bar": [1, 2, {"barp": 12, "blip": 13}]} "foo": 12
"bar":
- 1
- 2
- "barp": 12
"blip": 13
``` ```
then then
```bash ```bash
@ -59,7 +68,12 @@ true
## Object not included in array ## Object not included in array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{"foo": 12, "bar": [1, 2, {"barp": 12, "blip": 13}]} "foo": 12
"bar":
- 1
- 2
- "barp": 12
"blip": 13
``` ```
then then
```bash ```bash
@ -73,7 +87,7 @@ false
## String contains substring ## String contains substring
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
"foobar" foobar
``` ```
then then
```bash ```bash
@ -87,7 +101,7 @@ true
## String equals string ## String equals string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
"meow" meow
``` ```
then then
```bash ```bash

View File

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

View File

@ -5,7 +5,8 @@ Deletes matching entries in maps or arrays.
## Delete entry in map ## Delete entry in map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: dog} a: cat
b: dog
``` ```
then then
```bash ```bash
@ -13,13 +14,15 @@ yq 'del(.b)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: cat} a: cat
``` ```
## 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:
a1: fred
a2: frood
``` ```
then then
```bash ```bash
@ -27,13 +30,16 @@ yq 'del(.a.a1)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {a2: frood}} a:
a2: frood
``` ```
## Delete entry in array ## Delete entry in array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -41,13 +47,15 @@ yq 'del(.[1])' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 3] - 1
- 3
``` ```
## Delete nested entry in array ## Delete nested entry in array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: cat, b: dog}] - a: cat
b: dog
``` ```
then then
```bash ```bash
@ -55,13 +63,14 @@ yq 'del(.[0].a)' sample.yml
``` ```
will output will output
```yaml ```yaml
[{b: dog}] - b: dog
``` ```
## Delete no matches ## Delete no matches
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: dog} a: cat
b: dog
``` ```
then then
```bash ```bash
@ -69,13 +78,16 @@ yq 'del(.c)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: cat, b: dog} a: cat
b: dog
``` ```
## Delete matching entries ## Delete matching entries
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: dog, c: bat} a: cat
b: dog
c: bat
``` ```
then then
```bash ```bash
@ -83,13 +95,17 @@ yq 'del( .[] | select(. == "*at") )' sample.yml
``` ```
will output will output
```yaml ```yaml
{b: dog} b: dog
``` ```
## Recursively delete matching keys ## Recursively delete matching keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {name: frog, b: {name: blog, age: 12}}} a:
name: frog
b:
name: blog
age: 12
``` ```
then then
```bash ```bash
@ -97,6 +113,8 @@ yq 'del(.. | select(has("name")).name)' sample.yml
``` ```
will output will output
```yaml ```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 ## String split
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat_meow, b: _} a: cat_meow
b: _
``` ```
then then
```bash ```bash
@ -15,7 +16,11 @@ yq '.c = .a / .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: cat_meow, b: _, c: [cat, meow]} a: cat_meow
b: _
c:
- cat
- meow
``` ```
## Number division ## Number division
@ -23,7 +28,8 @@ The result during division is calculated as a float
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 12, b: 2.5} a: 12
b: 2.5
``` ```
then then
```bash ```bash
@ -31,7 +37,8 @@ yq '.a = .a / .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 4.8, b: 2.5} a: 4.8
b: 2.5
``` ```
## Number division by zero ## Number division by zero
@ -39,7 +46,8 @@ Dividing by zero results in +Inf or -Inf
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 1, b: -1} a: 1
b: -1
``` ```
then then
```bash ```bash
@ -47,6 +55,7 @@ yq '.a = .a / 0 | .b = .b / 0' sample.yml
``` ```
will output will output
```yaml ```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 ## Encode value as json string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: thing}} a:
cool: thing
``` ```
then then
```bash ```bash
@ -38,7 +39,12 @@ yq '.b = (.a | to_json)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {cool: thing}, b: "{\n \"cool\": \"thing\"\n}\n"} a:
cool: thing
b: |
{
"cool": "thing"
}
``` ```
## Encode value as json string, on one line ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: thing}} a:
cool: thing
``` ```
then then
```bash ```bash
@ -54,7 +61,9 @@ yq '.b = (.a | to_json(0))' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {cool: thing}, b: '{"cool":"thing"}'} a:
cool: thing
b: '{"cool":"thing"}'
``` ```
## Encode value as json string, on one line shorthand ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: thing}} a:
cool: thing
``` ```
then then
```bash ```bash
@ -70,7 +80,9 @@ yq '.b = (.a | @json)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {cool: thing}, b: '{"cool":"thing"}'} a:
cool: thing
b: '{"cool":"thing"}'
``` ```
## Decode a json encoded string ## Decode a json encoded string
@ -92,7 +104,8 @@ cool: thing
## Encode value as props string ## Encode value as props string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: thing}} a:
cool: thing
``` ```
then then
```bash ```bash
@ -100,7 +113,10 @@ yq '.b = (.a | @props)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {cool: thing}, b: "cool = thing\n"} a:
cool: thing
b: |
cool = thing
``` ```
## Decode props encoded string ## Decode props encoded string
@ -257,7 +273,10 @@ Scalars are strings, numbers and booleans.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, 'thing1,thing2', true, 3.40] - cat
- thing1,thing2
- true
- 3.40
``` ```
then then
```bash ```bash
@ -271,7 +290,14 @@ cat,"thing1,thing2",true,3.40
## Encode array of arrays as csv string ## Encode array of arrays as csv string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]] - - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
``` ```
then then
```bash ```bash
@ -288,7 +314,14 @@ Scalars are strings, numbers and booleans.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]] - - cat
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
``` ```
then then
```bash ```bash
@ -303,7 +336,10 @@ dog thing3 false 12
## Encode value as xml string ## Encode value as xml string
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: {foo: bar, +@id: hi}}} a:
cool:
foo: bar
+@id: hi
``` ```
then then
```bash ```bash
@ -320,7 +356,10 @@ will output
## Encode value as xml string on a single line ## Encode value as xml string on a single line
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: {foo: bar, +@id: hi}}} a:
cool:
foo: bar
+@id: hi
``` ```
then then
```bash ```bash
@ -335,7 +374,10 @@ will output
## Encode value as xml string with custom indentation ## Encode value as xml string with custom indentation
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {cool: {foo: bar, +@id: hi}}} a:
cool:
foo: bar
+@id: hi
``` ```
then then
```bash ```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 ## to_entries Map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 1, b: 2} a: 1
b: 2
``` ```
then then
```bash ```bash
@ -22,7 +23,8 @@ will output
## to_entries Array ## to_entries Array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[a, b] - a
- b
``` ```
then then
```bash ```bash
@ -39,7 +41,7 @@ will output
## to_entries null ## to_entries null
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[] null
``` ```
then then
```bash ```bash
@ -47,13 +49,13 @@ yq 'to_entries' sample.yml
``` ```
will output will output
```yaml ```yaml
[]
``` ```
## from_entries map ## from_entries map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 1, b: 2} a: 1
b: 2
``` ```
then then
```bash ```bash
@ -70,7 +72,8 @@ from_entries always creates a map, even for numeric keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[a, b] - a
- b
``` ```
then then
```bash ```bash
@ -85,7 +88,8 @@ will output
## Use with_entries to update keys ## Use with_entries to update keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 1, b: 2} a: 1
b: 2
``` ```
then then
```bash ```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: Given a sample.yml file of:
```yaml ```yaml
{a: 1, c: 3, b: 2} a: 1
c: 3
b: 2
``` ```
then then
```bash ```bash
@ -118,7 +124,10 @@ a: 1
## Use with_entries to filter the map ## Use with_entries to filter the map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {b: bird}, c: {d: dog}} a:
b: bird
c:
d: dog
``` ```
then then
```bash ```bash
@ -126,6 +135,7 @@ yq 'with_entries(select(.value | has("b")))' sample.yml
``` ```
will output will output
```yaml ```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: Given a sample.yml file of:
```yaml ```yaml
{a: {b: [{name: dog}, {name: cat}]}} a:
b:
- name: dog
- name: cat
``` ```
then then
```bash ```bash
@ -103,13 +106,17 @@ pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueE
``` ```
will output will output
```yaml ```yaml
{a: {b: [{name: moo}, {name: cat}]}} a:
b:
- name: moo
- name: cat
``` ```
## Dynamic key lookup with environment variable ## Dynamic key lookup with environment variable
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{cat: meow, dog: woof} cat: meow
dog: woof
``` ```
then then
```bash ```bash
@ -213,7 +220,7 @@ Error: variable ${myEmptyEnv} set but empty
## Replace string environment variable in document ## Replace string environment variable in document
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{v: '${myenv}'} v: ${myenv}
``` ```
then then
```bash ```bash
@ -221,7 +228,7 @@ myenv="cat meow" yq '.v |= envsubst' sample.yml
``` ```
will output will output
```yaml ```yaml
{v: 'cat meow'} v: cat meow
``` ```
## (Default) Return all envsubst errors ## (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 ## Dynamically evaluate a path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -17,7 +21,7 @@ yq 'eval(.pathExp)' sample.yml
``` ```
will output will output
```yaml ```yaml
{name: cat} name: cat
``` ```
## Dynamically update a path from an environment variable ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: {b: [{name: dog}, {name: cat}]}} a:
b:
- name: dog
- name: cat
``` ```
then then
```bash ```bash
@ -33,6 +40,9 @@ pathEnv=".a.b[0].name" valueEnv="moo" yq 'eval(strenv(pathEnv)) = strenv(valueE
``` ```
will output will output
```yaml ```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 ## Get filename
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat} a: cat
``` ```
then then
```bash ```bash
@ -27,7 +27,7 @@ sample.yml
## Get file index ## Get file index
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat} a: cat
``` ```
then then
```bash ```bash
@ -41,11 +41,11 @@ will output
## Get file indices of multiple documents ## Get file indices of multiple documents
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat} a: cat
``` ```
And another sample another.yml file of: And another sample another.yml file of:
```yaml ```yaml
{a: cat} a: cat
``` ```
then then
```bash ```bash
@ -60,7 +60,7 @@ will output
## Get file index alias ## Get file index alias
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat} a: cat
``` ```
then then
```bash ```bash

View File

@ -6,7 +6,9 @@ Filters an array (or map values) by the expression given. Equivalent to doing `m
## Filter array ## Filter array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -14,13 +16,19 @@ yq 'filter(. < 3)' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2] - 1
- 2
``` ```
## Filter map values ## Filter map values
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{c: {things: cool, frog: yes}, d: {things: hot, frog: false}} c:
things: cool
frog: yes
d:
things: hot
frog: false
``` ```
then then
```bash ```bash
@ -28,6 +36,7 @@ yq 'filter(.things == "cool")' sample.yml
``` ```
will output will output
```yaml ```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: Given a sample.yml file of:
```yaml ```yaml
[1, [2], [[3]]] - 1
- - 2
- - - 3
``` ```
then then
```bash ```bash
@ -14,13 +16,17 @@ yq 'flatten' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
## Flatten with depth of one ## Flatten with depth of one
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, [2], [[3]]] - 1
- - 2
- - - 3
``` ```
then then
```bash ```bash
@ -28,13 +34,15 @@ yq 'flatten(1)' sample.yml
``` ```
will output will output
```yaml ```yaml
[1, 2, [3]] - 1
- 2
- - 3
``` ```
## Flatten empty array ## Flatten empty array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[[]] - []
``` ```
then then
```bash ```bash
@ -48,7 +56,8 @@ will output
## Flatten array of objects ## Flatten array of objects
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{foo: bar}, [{foo: baz}]] - foo: bar
- - foo: baz
``` ```
then then
```bash ```bash
@ -56,6 +65,7 @@ yq 'flatten' sample.yml
``` ```
will output will output
```yaml ```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 ## Group by field
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -13,15 +18,25 @@ yq 'group_by(.foo)' sample.yml
``` ```
will output will output
```yaml ```yaml
- - {foo: 1, bar: 10} - - foo: 1
- {foo: 1, bar: 1} bar: 10
- - {foo: 3, bar: 100} - foo: 1
bar: 1
- - foo: 3
bar: 100
``` ```
## Group by field, with nuls ## Group by field, with nuls
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -29,10 +44,13 @@ yq 'group_by(.foo)' sample.yml
``` ```
will output will output
```yaml ```yaml
- - {cat: dog} - - cat: dog
- {no: foo for you} - no: foo for you
- - {foo: 1, bar: 10} - - foo: 1
- {foo: 1, bar: 1} bar: 10
- - {foo: 3, bar: 100} - 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 ## Map keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{dog: woof, cat: meow} dog: woof
cat: meow
``` ```
then then
```bash ```bash
@ -20,7 +21,8 @@ will output
## Array keys ## Array keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[apple, banana] - apple
- banana
``` ```
then then
```bash ```bash
@ -35,7 +37,9 @@ will output
## Retrieve array key ## Retrieve array key
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash

View File

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

View File

@ -50,7 +50,7 @@ bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u
## Simple example ## Simple example
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{myFile: ../../examples/thing.yml} myFile: ../../examples/thing.yml
``` ```
then then
```bash ```bash
@ -67,7 +67,8 @@ Note that you can modify the filename in the load operator if needed.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{something: {file: thing.yml}} something:
file: thing.yml
``` ```
then then
```bash ```bash
@ -75,7 +76,9 @@ yq '.something |= load("../../examples/" + .file)' sample.yml
``` ```
will output will output
```yaml ```yaml
{something: {a: apple is included, b: cool.}} something:
a: apple is included
b: cool.
``` ```
## Replace _all_ nodes with referenced file ## 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: Given a sample.yml file of:
```yaml ```yaml
{something: {file: thing.yml}, over: {here: [{file: thing.yml}]}} something:
file: thing.yml
over:
here:
- file: thing.yml
``` ```
then then
```bash ```bash
@ -91,7 +98,13 @@ yq '(.. | select(has("file"))) |= load("../../examples/" + .file)' sample.yml
``` ```
will output will output
```yaml ```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 ## 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: Given a sample.yml file of:
```yaml ```yaml
{something: {file: thing.yml}} something:
file: thing.yml
``` ```
then then
```bash ```bash
@ -107,7 +121,9 @@ yq '.something |= load_str("../../examples/" + .file)' sample.yml
``` ```
will output will output
```yaml ```yaml
{something: "a: apple is included\nb: cool."} something: |-
a: apple is included
b: cool.
``` ```
## Load from XML ## 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 ## Map array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -13,13 +15,17 @@ yq 'map(. + 1)' sample.yml
``` ```
will output will output
```yaml ```yaml
[2, 3, 4] - 2
- 3
- 4
``` ```
## Map object values ## Map object values
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 1, b: 2, c: 3} a: 1
b: 2
c: 3
``` ```
then then
```bash ```bash
@ -27,6 +33,8 @@ yq 'map_values(. + 1)' sample.yml
``` ```
will output will output
```yaml ```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: Given a sample.yml file of:
```yaml ```yaml
{a: 13, b: 2} a: 13
b: 2
``` ```
then then
```bash ```bash
@ -15,7 +16,8 @@ yq '.a = .a % .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 1, b: 2} a: 1
b: 2
``` ```
## Number modulo - float ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: 12, b: 2.5} a: 12
b: 2.5
``` ```
then then
```bash ```bash
@ -31,7 +34,8 @@ yq '.a = .a % .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: !!float 2, b: 2.5} a: !!float 2
b: 2.5
``` ```
## Number modulo - int by zero ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: 1, b: 0} a: 1
b: 0
``` ```
then then
```bash ```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: Given a sample.yml file of:
```yaml ```yaml
{a: 1.1, b: 0} a: 1.1
b: 0
``` ```
then then
```bash ```bash
@ -63,6 +69,7 @@ yq '.a = .a % .b' sample.yml
``` ```
will output will output
```yaml ```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 ## Map path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {b: cat}} a:
b: cat
``` ```
then then
```bash ```bash
@ -25,7 +26,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:
b: cat
``` ```
then then
```bash ```bash
@ -39,7 +41,9 @@ b
## Array path ## Array path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: [cat, dog]} a:
- cat
- dog
``` ```
then then
```bash ```bash
@ -54,7 +58,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:
- cat
- dog
``` ```
then then
```bash ```bash
@ -68,7 +74,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:
- cat
- dog
- frog
``` ```
then then
```bash ```bash
@ -89,7 +98,8 @@ will output
## Set path ## Set path
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {b: cat}} a:
b: cat
``` ```
then then
```bash ```bash
@ -97,7 +107,8 @@ yq 'setpath(["a", "b"]; "things")' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {b: things}} a:
b: things
``` ```
## Set on empty document ## Set on empty document
@ -172,7 +183,10 @@ Notice delpaths takes an _array_ of paths.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {b: cat, c: dog, d: frog}} a:
b: cat
c: dog
d: frog
``` ```
then then
```bash ```bash
@ -180,7 +194,8 @@ yq 'delpaths([["a", "c"], ["a", "d"]])' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: {b: cat}} a:
b: cat
``` ```
## Delete array path ## 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: Given a sample.yml file of:
```yaml ```yaml
[cat, leopard, lion] - cat
- leopard
- lion
``` ```
then then
```bash ```bash
@ -39,6 +41,7 @@ yq 'pick([2, 0, 734, -5])' sample.yml
``` ```
will output will output
```yaml ```yaml
[lion, cat] - lion
- cat
``` ```

View File

@ -5,7 +5,9 @@ Reverses the order of the items in an array
## Reverse ## Reverse
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -13,7 +15,9 @@ yq 'reverse' sample.yml
``` ```
will output will output
```yaml ```yaml
[3, 2, 1] - 3
- 2
- 1
``` ```
## Sort descending by string field ## Sort descending by string field
@ -21,7 +25,9 @@ Use sort with reverse to sort in descending order.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: banana}, {a: cat}, {a: apple}] - a: banana
- a: cat
- a: apple
``` ```
then then
```bash ```bash
@ -29,6 +35,8 @@ yq 'sort_by(.a) | reverse' sample.yml
``` ```
will output will output
```yaml ```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 ## Slicing arrays
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, dog, frog, cow] - cat
- dog
- frog
- cow
``` ```
then then
```bash ```bash
@ -24,7 +27,10 @@ Starts from the start of the array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, dog, frog, cow] - cat
- dog
- frog
- cow
``` ```
then then
```bash ```bash
@ -41,7 +47,10 @@ Finishes at the end of the array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, dog, frog, cow] - cat
- dog
- frog
- cow
``` ```
then then
```bash ```bash
@ -56,7 +65,10 @@ will output
## Slicing arrays - use negative numbers to count backwards from the end ## Slicing arrays - use negative numbers to count backwards from the end
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, dog, frog, cow] - cat
- dog
- frog
- cow
``` ```
then then
```bash ```bash
@ -73,7 +85,10 @@ using an expression to find the index
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, dog, frog, cow] - cat
- dog
- frog
- cow
``` ```
then then
```bash ```bash

View File

@ -18,7 +18,9 @@ See [here](https://mikefarah.gitbook.io/yq/operators/entries#custom-sort-map-key
## Sort keys of map ## Sort keys of map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{c: frog, a: blah, b: bing} c: frog
a: blah
b: bing
``` ```
then then
```bash ```bash
@ -26,7 +28,9 @@ yq 'sort_keys(.)' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: blah, b: bing, c: frog} a: blah
b: bing
c: frog
``` ```
## Sort keys recursively ## 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: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -42,6 +58,18 @@ yq 'sort_keys(..)' sample.yml
``` ```
will output will output
```yaml ```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 ## Sort by string field
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: banana}, {a: cat}, {a: apple}] - a: banana
- a: cat
- a: apple
``` ```
then then
```bash ```bash
@ -18,13 +20,19 @@ yq 'sort_by(.a)' sample.yml
``` ```
will output will output
```yaml ```yaml
[{a: apple}, {a: banana}, {a: cat}] - a: apple
- a: banana
- a: cat
``` ```
## Sort by multiple fields ## Sort by multiple fields
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: dog}, {a: cat, b: banana}, {a: cat, b: apple}] - a: dog
- a: cat
b: banana
- a: cat
b: apple
``` ```
then then
```bash ```bash
@ -32,7 +40,11 @@ yq 'sort_by(.a, .b)' sample.yml
``` ```
will output will output
```yaml ```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 ## Sort descending by string field
@ -40,7 +52,9 @@ Use sort with reverse to sort in descending order.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: banana}, {a: cat}, {a: apple}] - a: banana
- a: cat
- a: apple
``` ```
then then
```bash ```bash
@ -48,7 +62,9 @@ yq 'sort_by(.a) | reverse' sample.yml
``` ```
will output will output
```yaml ```yaml
[{a: cat}, {a: banana}, {a: apple}] - a: cat
- a: banana
- a: apple
``` ```
## Sort array in place ## 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: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -106,13 +129,22 @@ yq 'sort_by(.a)' sample.yml
``` ```
will output will output
```yaml ```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 ## Sort by numeric field
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: 10}, {a: 100}, {a: 1}] - a: 10
- a: 100
- a: 1
``` ```
then then
```bash ```bash
@ -120,13 +152,17 @@ yq 'sort_by(.a)' sample.yml
``` ```
will output will output
```yaml ```yaml
[{a: 1}, {a: 10}, {a: 100}] - a: 1
- a: 10
- a: 100
``` ```
## Sort by custom date field ## Sort by custom date field
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -134,13 +170,21 @@ yq 'with_dtf("02-Jan-2006"; sort_by(.a))' sample.yml
``` ```
will output will output
```yaml ```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 ## Sort, nulls come first
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[8, 3, null, 6, true, false, cat] - 8
- 3
- null
- 6
- true
- false
- cat
``` ```
then then
```bash ```bash
@ -148,6 +192,12 @@ yq 'sort' sample.yml
``` ```
will output will output
```yaml ```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 ## Join strings
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, meow, 1, null, true] - cat
- meow
- 1
- null
- true
``` ```
then then
```bash ```bash
@ -105,7 +109,10 @@ cat; meow; 1; ; true
## Trim strings ## Trim strings
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[' cat', 'dog ', ' cow cow ', horse] - ' cat'
- 'dog '
- ' cow cow '
- horse
``` ```
then then
```bash ```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: Given a sample.yml file of:
```yaml ```yaml
[cat, dog] - cat
- dog
``` ```
then then
```bash ```bash
@ -346,7 +354,7 @@ b: !goat heart
## Split strings ## Split strings
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
"cat; meow; 1; ; true" cat; meow; 1; ; true
``` ```
then then
```bash ```bash
@ -364,7 +372,7 @@ will output
## Split strings one match ## Split strings one match
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
"word" word
``` ```
then then
```bash ```bash

View File

@ -28,7 +28,9 @@ Note that order of the keys does not matter
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{a: b, c: d}, {a: b}] - a: b
c: d
- a: b
``` ```
then then
```bash ```bash
@ -36,7 +38,7 @@ yq '. - [{"c": "d", "a": "b"}]' sample.yml
``` ```
will output will output
```yaml ```yaml
[{a: b}] - a: b
``` ```
## Number subtraction - float ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: 3, b: 4.5} a: 3
b: 4.5
``` ```
then then
```bash ```bash
@ -52,7 +55,8 @@ yq '.a = .a - .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: -1.5, b: 4.5} a: -1.5
b: 4.5
``` ```
## Number subtraction - int ## 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: Given a sample.yml file of:
```yaml ```yaml
{a: 3, b: 4} a: 3
b: 4
``` ```
then then
```bash ```bash
@ -68,13 +73,15 @@ yq '.a = .a - .b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: -1, b: 4} a: -1
b: 4
``` ```
## Decrement numbers ## Decrement numbers
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: 3, b: 5} a: 3
b: 5
``` ```
then then
```bash ```bash
@ -82,7 +89,8 @@ yq '.[] -= 1' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 2, b: 4} a: 2
b: 4
``` ```
## Date subtraction ## 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 ## Get tag
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: 5, c: 3.2, e: true, f: []} a: cat
b: 5
c: 3.2
e: true
f: []
``` ```
then then
```bash ```bash
@ -24,7 +28,11 @@ will output
## type is an alias for tag ## type is an alias for tag
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: 5, c: 3.2, e: true, f: []} a: cat
b: 5
c: 3.2
e: true
f: []
``` ```
then then
```bash ```bash
@ -43,7 +51,7 @@ will output
## Set custom tag ## Set custom tag
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: str} a: str
``` ```
then then
```bash ```bash
@ -51,13 +59,16 @@ yq '.a tag = "!!mikefarah"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: !!mikefarah str} a: !!mikefarah str
``` ```
## Find numbers and convert them to strings ## Find numbers and convert them to strings
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: cat, b: 5, c: 3.2, e: true} a: cat
b: 5
c: 3.2
e: true
``` ```
then then
```bash ```bash
@ -65,6 +76,9 @@ yq '(.. | select(tag == "!!int")) tag= "!!str"' sample.yml
``` ```
will output will output
```yaml ```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 ## Simple map navigation
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {b: apple}} a:
b: apple
``` ```
then then
```bash ```bash
@ -13,7 +14,7 @@ yq '.a' sample.yml
``` ```
will output will output
```yaml ```yaml
{b: apple} b: apple
``` ```
## Splat ## Splat
@ -21,7 +22,8 @@ Often used to pipe children into other operators
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[{b: apple}, {c: banana}] - b: apple
- c: banana
``` ```
then then
```bash ```bash
@ -29,8 +31,8 @@ yq '.[]' sample.yml
``` ```
will output will output
```yaml ```yaml
{b: apple} b: apple
{c: banana} c: banana
``` ```
## Optional Splat ## 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: Given a sample.yml file of:
```yaml ```yaml
"cat" cat
``` ```
then then
```bash ```bash
@ -53,7 +55,7 @@ Use quotes with square brackets around path elements with special characters
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{"{}": frog} "{}": frog
``` ```
then then
```bash ```bash
@ -85,7 +87,7 @@ Use quotes with square brackets around path elements with special characters
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{"red rabbit": frog} "red rabbit": frog
``` ```
then then
```bash ```bash
@ -101,7 +103,9 @@ Expressions within [] can be used to dynamically lookup / calculate keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{b: apple, apple: crispy yum, banana: soft yum} b: apple
apple: crispy yum
banana: soft yum
``` ```
then then
```bash ```bash
@ -117,7 +121,7 @@ Nodes are added dynamically while traversing
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{c: banana} c: banana
``` ```
then then
```bash ```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: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -146,7 +152,9 @@ will output
## Wildcard matching ## Wildcard matching
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: {cat: apple, mad: things}} a:
cat: apple
mad: things
``` ```
then then
```bash ```bash
@ -161,7 +169,9 @@ things
## Aliases ## Aliases
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: &cat {c: frog}, b: *cat} a: &cat
c: frog
b: *cat
``` ```
then then
```bash ```bash
@ -175,7 +185,9 @@ will output
## Traversing aliases with splat ## Traversing aliases with splat
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: &cat {c: frog}, b: *cat} a: &cat
c: frog
b: *cat
``` ```
then then
```bash ```bash
@ -189,7 +201,9 @@ frog
## Traversing aliases explicitly ## Traversing aliases explicitly
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: &cat {c: frog}, b: *cat} a: &cat
c: frog
b: *cat
``` ```
then then
```bash ```bash
@ -203,7 +217,9 @@ frog
## Traversing arrays by index ## Traversing arrays by index
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[1, 2, 3] - 1
- 2
- 3
``` ```
then then
```bash ```bash
@ -231,7 +247,7 @@ cat
## Maps with numeric keys ## Maps with numeric keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{2: cat} 2: cat
``` ```
then then
```bash ```bash
@ -245,7 +261,7 @@ cat
## Maps with non existing numeric keys ## Maps with non existing numeric keys
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: b} a: b
``` ```
then then
```bash ```bash
@ -452,7 +468,10 @@ foobarList_c
## Select multiple indices ## Select multiple indices
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
{a: [a, b, c]} a:
- a
- b
- c
``` ```
then then
```bash ```bash

View File

@ -8,7 +8,10 @@ Note that unique maintains the original order of the array.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[2, 1, 3, 2] - 2
- 1
- 3
- 2
``` ```
then then
```bash ```bash
@ -16,7 +19,9 @@ yq 'unique' sample.yml
``` ```
will output will output
```yaml ```yaml
[2, 1, 3] - 2
- 1
- 3
``` ```
## Unique nulls ## 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: Given a sample.yml file of:
```yaml ```yaml
[~, null, ~, null] - ~
- null
- ~
- null
``` ```
then then
```bash ```bash
@ -32,7 +40,8 @@ yq 'unique' sample.yml
``` ```
will output will output
```yaml ```yaml
[~, null] - ~
- null
``` ```
## Unique all nulls ## Unique all nulls
@ -40,7 +49,10 @@ Run against the node tag to unique all the nulls
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[~, null, ~, null] - ~
- null
- ~
- null
``` ```
then then
```bash ```bash
@ -48,13 +60,18 @@ yq 'unique_by(tag)' sample.yml
``` ```
will output will output
```yaml ```yaml
[~] - ~
``` ```
## Unique array object fields ## Unique array object fields
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash
@ -62,6 +79,9 @@ yq 'unique_by(.name)' sample.yml
``` ```
will output will output
```yaml ```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 ## Multi value variable
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
[cat, dog] - cat
- dog
``` ```
then then
```bash ```bash
@ -38,7 +39,14 @@ Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicB
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```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 then
```bash ```bash

View File

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

View File

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

View File

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

View File

@ -127,6 +127,12 @@ var deleteOperatorScenarios = []expressionScenario{
"D0, P[], (doc)::a: [x, x]\n", "D0, P[], (doc)::a: [x, x]\n",
}, },
}, },
{
skipDoc: true,
document: `a: null`,
expression: `del(..)`,
expected: []string{},
},
{ {
skipDoc: true, skipDoc: true,
document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`, 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) 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 leadingContent = rhs.LeadingContent
headComment = rhs.HeadComment headComment = rhs.HeadComment
footComment = rhs.FootComment footComment = rhs.FootComment
trailingContent = rhs.TrailingContent
if lhs.HeadComment != "" || lhs.LeadingContent != "" { if lhs.HeadComment != "" || lhs.LeadingContent != "" {
headComment = lhs.HeadComment headComment = lhs.HeadComment
leadingContent = lhs.LeadingContent leadingContent = lhs.LeadingContent
} }
if lhs.FootComment != "" { if lhs.FootComment != "" || lhs.TrailingContent != "" {
footComment = lhs.FootComment 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) { 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) { return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
// need to do this before unWrapping the potential document node // 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() lhs = lhs.unwrapDocument()
rhs = rhs.unwrapDocument() rhs = rhs.unwrapDocument()
log.Debugf("Multiplying LHS: %v", NodeToString(lhs)) log.Debugf("Multiplying LHS: %v", NodeToString(lhs))
@ -71,6 +74,7 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, contex
newBlank.LeadingContent = leadingContent newBlank.LeadingContent = leadingContent
newBlank.HeadComment = headComment newBlank.HeadComment = headComment
newBlank.FootComment = footComment newBlank.FootComment = footComment
newBlank.TrailingContent = trailingContent
return mergeObjects(d, context.WritableClone(), newBlank, rhs, preferences) return mergeObjects(d, context.WritableClone(), newBlank, rhs, preferences)
} }

View File

@ -45,7 +45,7 @@ var pathOperatorScenarios = []expressionScenario{
document: `{a: {b: cat}}`, document: `{a: {b: cat}}`,
expression: `.a.b | path | .[-1]`, expression: `.a.b | path | .[-1]`,
expected: []string{ 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]}`, document: `{a: [cat, dog]}`,
expression: `.a.[] | select(. == "dog") | path | .[-1]`, expression: `.a.[] | select(. == "dog") | path | .[-1]`,
expected: []string{ 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() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
candidate = candidate.unwrapDocument()
log.Debugf("Recursive Decent, added %v", NodeToString(candidate)) log.Debugf("Recursive Decent, added %v", NodeToString(candidate))
results.PushBack(candidate) results.PushBack(candidate)

View File

@ -10,7 +10,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{}`, document: `{}`,
expression: `..`, expression: `..`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{}\n", "D0, P[], (doc)::{}\n",
}, },
}, },
{ {
@ -18,7 +18,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{}`, document: `{}`,
expression: `...`, expression: `...`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{}\n", "D0, P[], (doc)::{}\n",
}, },
}, },
{ {
@ -26,7 +26,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[]`, document: `[]`,
expression: `..`, expression: `..`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::[]\n", "D0, P[], (doc)::[]\n",
}, },
}, },
{ {
@ -34,7 +34,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[]`, document: `[]`,
expression: `...`, expression: `...`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::[]\n", "D0, P[], (doc)::[]\n",
}, },
}, },
{ {
@ -42,7 +42,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `cat`, document: `cat`,
expression: `..`, expression: `..`,
expected: []string{ expected: []string{
"D0, P[], (!!str)::cat\n", "D0, P[], (doc)::cat\n",
}, },
}, },
{ {
@ -50,7 +50,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `cat`, document: `cat`,
expression: `...`, expression: `...`,
expected: []string{ expected: []string{
"D0, P[], (!!str)::cat\n", "D0, P[], (doc)::cat\n",
}, },
}, },
{ {
@ -58,7 +58,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: frog}`, document: `{a: frog}`,
expression: `..`, expression: `..`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{a: frog}\n", "D0, P[], (doc)::{a: frog}\n",
"D0, P[a], (!!str)::frog\n", "D0, P[a], (!!str)::frog\n",
}, },
}, },
@ -86,7 +86,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: frog}`, document: `{a: frog}`,
expression: `...`, expression: `...`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{a: frog}\n", "D0, P[], (doc)::{a: frog}\n",
"D0, P[a], (!!str)::a\n", "D0, P[a], (!!str)::a\n",
"D0, P[a], (!!str)::frog\n", "D0, P[a], (!!str)::frog\n",
}, },
@ -96,7 +96,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: {b: apple}}`, document: `{a: {b: apple}}`,
expression: `..`, expression: `..`,
expected: []string{ 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], (!!map)::{b: apple}\n",
"D0, P[a b], (!!str)::apple\n", "D0, P[a b], (!!str)::apple\n",
}, },
@ -106,7 +106,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: {b: apple}}`, document: `{a: {b: apple}}`,
expression: `...`, expression: `...`,
expected: []string{ 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], (!!str)::a\n",
"D0, P[a], (!!map)::{b: apple}\n", "D0, P[a], (!!map)::{b: apple}\n",
"D0, P[a b], (!!str)::b\n", "D0, P[a b], (!!str)::b\n",
@ -118,7 +118,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[1,2,3]`, document: `[1,2,3]`,
expression: `..`, expression: `..`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::[1, 2, 3]\n", "D0, P[], (doc)::[1, 2, 3]\n",
"D0, P[0], (!!int)::1\n", "D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n", "D0, P[1], (!!int)::2\n",
"D0, P[2], (!!int)::3\n", "D0, P[2], (!!int)::3\n",
@ -129,7 +129,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[1,2,3]`, document: `[1,2,3]`,
expression: `...`, expression: `...`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::[1, 2, 3]\n", "D0, P[], (doc)::[1, 2, 3]\n",
"D0, P[0], (!!int)::1\n", "D0, P[0], (!!int)::1\n",
"D0, P[1], (!!int)::2\n", "D0, P[1], (!!int)::2\n",
"D0, P[2], (!!int)::3\n", "D0, P[2], (!!int)::3\n",
@ -140,7 +140,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[{a: cat},2,true]`, document: `[{a: cat},2,true]`,
expression: `..`, expression: `..`,
expected: []string{ 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], (!!map)::{a: cat}\n",
"D0, P[0 a], (!!str)::cat\n", "D0, P[0 a], (!!str)::cat\n",
"D0, P[1], (!!int)::2\n", "D0, P[1], (!!int)::2\n",
@ -152,7 +152,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `[{a: cat},2,true]`, document: `[{a: cat},2,true]`,
expression: `...`, expression: `...`,
expected: []string{ 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], (!!map)::{a: cat}\n",
"D0, P[0 a], (!!str)::a\n", "D0, P[0 a], (!!str)::a\n",
"D0, P[0 a], (!!str)::cat\n", "D0, P[0 a], (!!str)::cat\n",
@ -173,7 +173,7 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
document: `{a: &cat {c: frog}, b: *cat}`, document: `{a: &cat {c: frog}, b: *cat}`,
expression: `...`, expression: `...`,
expected: []string{ 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], (!!str)::a\n",
"D0, P[a], (!!map)::&cat {c: frog}\n", "D0, P[a], (!!map)::&cat {c: frog}\n",
"D0, P[a c], (!!str)::c\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() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
candidate.SetDocument(index) candidate.SetDocument(index)
candidate.SetParent(nil)
index = index + 1 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() { for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
log.Debugf("Setting style of : %v", candidate.GetKey()) log.Debugf("Setting style of : %v", NodeToString(candidate))
if expressionNode.Operation.UpdateAssign { if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {

View File

@ -26,7 +26,7 @@ var styleOperatorScenarios = []expressionScenario{
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="tagged"`, expression: `.. style="tagged"`,
expected: []string{ 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}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="double"`, expression: `.. style="double"`,
expected: []string{ 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}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `... style="double"`, expression: `... style="double"`,
expected: []string{ 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]", document: "bing: &foo frog\na:\n c: cat\n <<: [*foo]",
expression: `(... | select(tag=="!!str")) style="single"`, expression: `(... | select(tag=="!!str")) style="single"`,
expected: []string{ 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}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="single"`, expression: `.. style="single"`,
expected: []string{ 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}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="literal"`, expression: `.. style="literal"`,
expected: []string{ expected: []string{
`D0, P[], (!!map)::a: |- `D0, P[], (doc)::a: |-
cat cat
b: |- b: |-
5 5
@ -82,7 +82,7 @@ e: |-
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="folded"`, expression: `.. style="folded"`,
expected: []string{ expected: []string{
`D0, P[], (!!map)::a: >- `D0, P[], (doc)::a: >-
cat cat
b: >- b: >-
5 5
@ -98,7 +98,7 @@ e: >-
document: `{a: cat, b: 5, c: 3.2, e: true}`, document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="flow"`, expression: `.. style="flow"`,
expected: []string{ 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}`, document: `{a: cat, "b": 5, 'c': 3.2, "e": true}`,
expression: `... style=""`, expression: `... style=""`,
expected: []string{ 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",
}, },
}, },
{ {