wip - rabbit hole :/

This commit is contained in:
Mike Farah 2023-05-02 15:07:04 +10:00
parent 42843763ca
commit e3ccd05a00
10 changed files with 77 additions and 156 deletions

View File

@ -31,8 +31,7 @@ will output
lc: "" lc: ""
fc: "" fc: ""
- p: hello - p: hello
isKey: null isKey: true
true: null
hc: "" hc: ""
lc: hello-world-comment lc: hello-world-comment
fc: "" fc: ""
@ -42,8 +41,7 @@ will output
lc: "" lc: ""
fc: "" fc: ""
- p: hello.message - p: hello.message
isKey: null isKey: true
true: null
hc: "" hc: ""
lc: "" lc: ""
fc: "" fc: ""

View File

@ -5,8 +5,7 @@ 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 {a: cat, b: dog}
b: dog
``` ```
then then
```bash ```bash
@ -14,15 +13,13 @@ 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: {a: {a1: fred, a2: frood}}
a1: fred
a2: frood
``` ```
then then
```bash ```bash
@ -30,16 +27,13 @@ yq 'del(.a.a1)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a: {a2: frood}}
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 [1, 2, 3]
- 2
- 3
``` ```
then then
```bash ```bash
@ -47,15 +41,13 @@ yq 'del(.[1])' sample.yml
``` ```
will output will output
```yaml ```yaml
- 1 [1, 3]
- 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 [{a: cat, b: dog}]
b: dog
``` ```
then then
```bash ```bash
@ -63,14 +55,13 @@ 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 {a: cat, b: dog}
b: dog
``` ```
then then
```bash ```bash
@ -78,16 +69,13 @@ yq 'del(.c)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat {a: cat, b: dog}
b: dog
``` ```
## Delete matching entries ## Delete matching entries
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat {a: cat, b: dog, c: bat}
b: dog
c: bat
``` ```
then then
```bash ```bash
@ -95,17 +83,13 @@ 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: {a: {name: frog, b: {name: blog, age: 12}}}
name: frog
b:
name: blog
age: 12
``` ```
then then
```bash ```bash
@ -113,8 +97,6 @@ yq 'del(.. | select(has("name")).name)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a: {b: {age: 12}}}
b:
age: 12
``` ```

View File

@ -7,8 +7,7 @@ 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 {a: cat_meow, b: _}
b: _
``` ```
then then
```bash ```bash
@ -16,11 +15,7 @@ yq '.c = .a / .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: cat_meow {a: cat_meow, b: _, c: [cat, meow]}
b: _
c:
- cat
- meow
``` ```
## Number division ## Number division
@ -28,8 +23,7 @@ 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 {a: 12, b: 2.5}
b: 2.5
``` ```
then then
```bash ```bash
@ -37,8 +31,7 @@ yq '.a = .a / .b' sample.yml
``` ```
will output will output
```yaml ```yaml
a: 4.8 {a: 4.8, b: 2.5}
b: 2.5
``` ```
## Number division by zero ## Number division by zero
@ -46,8 +39,7 @@ Dividing by zero results in +Inf or -Inf
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: 1 {a: 1, b: -1}
b: -1
``` ```
then then
```bash ```bash
@ -55,7 +47,6 @@ yq '.a = .a / 0 | .b = .b / 0' sample.yml
``` ```
will output will output
```yaml ```yaml
a: !!float +Inf {a: !!float +Inf, b: !!float -Inf}
b: !!float -Inf
``` ```

View File

@ -85,6 +85,7 @@ will output
```yaml ```yaml
match: cat match: cat
doc: 0 doc: 0
---
match: frog match: frog
doc: 1 doc: 1
``` ```

View File

@ -30,8 +30,7 @@ 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: {a: {cool: thing}}
cool: thing
``` ```
then then
```bash ```bash
@ -39,12 +38,7 @@ yq '.b = (.a | to_json)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a: {cool: thing}, b: "{\n \"cool\": \"thing\"\n}\n"}
cool: thing
b: |
{
"cool": "thing"
}
``` ```
## Encode value as json string, on one line ## Encode value as json string, on one line
@ -52,8 +46,7 @@ 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: {a: {cool: thing}}
cool: thing
``` ```
then then
```bash ```bash
@ -61,9 +54,7 @@ yq '.b = (.a | to_json(0))' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a: {cool: thing}, b: '{"cool":"thing"}'}
cool: thing
b: '{"cool":"thing"}'
``` ```
## Encode value as json string, on one line shorthand ## Encode value as json string, on one line shorthand
@ -71,8 +62,7 @@ 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: {a: {cool: thing}}
cool: thing
``` ```
then then
```bash ```bash
@ -80,9 +70,7 @@ yq '.b = (.a | @json)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a: {cool: thing}, b: '{"cool":"thing"}'}
cool: thing
b: '{"cool":"thing"}'
``` ```
## Decode a json encoded string ## Decode a json encoded string
@ -104,8 +92,7 @@ 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: {a: {cool: thing}}
cool: thing
``` ```
then then
```bash ```bash
@ -113,10 +100,7 @@ yq '.b = (.a | @props)' sample.yml
``` ```
will output will output
```yaml ```yaml
a: {a: {cool: thing}, b: "cool = thing\n"}
cool: thing
b: |
cool = thing
``` ```
## Decode props encoded string ## Decode props encoded string
@ -273,10 +257,7 @@ Scalars are strings, numbers and booleans.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
- cat [cat, 'thing1,thing2', true, 3.40]
- thing1,thing2
- true
- 3.40
``` ```
then then
```bash ```bash
@ -290,14 +271,7 @@ 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 [[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]]
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
``` ```
then then
```bash ```bash
@ -314,14 +288,7 @@ Scalars are strings, numbers and booleans.
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
- - cat [[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]]
- thing1,thing2
- true
- 3.40
- - dog
- thing3
- false
- 12
``` ```
then then
```bash ```bash
@ -336,10 +303,7 @@ 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: {a: {cool: {foo: bar, +@id: hi}}}
cool:
foo: bar
+@id: hi
``` ```
then then
```bash ```bash
@ -356,10 +320,7 @@ 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: {a: {cool: {foo: bar, +@id: hi}}}
cool:
foo: bar
+@id: hi
``` ```
then then
```bash ```bash
@ -374,10 +335,7 @@ 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: {a: {cool: {foo: bar, +@id: hi}}}
cool:
foo: bar
+@id: hi
``` ```
then then
```bash ```bash

View File

@ -5,8 +5,7 @@ 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 {a: 1, b: 2}
b: 2
``` ```
then then
```bash ```bash
@ -23,8 +22,7 @@ will output
## to_entries Array ## to_entries Array
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
- a [a, b]
- b
``` ```
then then
```bash ```bash
@ -41,7 +39,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
@ -49,13 +47,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 {a: 1, b: 2}
b: 2
``` ```
then then
```bash ```bash
@ -63,8 +61,10 @@ yq 'to_entries | from_entries' sample.yml
``` ```
will output will output
```yaml ```yaml
a: 1 - a
b: 2 - 1
- b
- 2
``` ```
## from_entries with numeric key indices ## from_entries with numeric key indices
@ -72,8 +72,7 @@ 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 [a, b]
- b
``` ```
then then
```bash ```bash
@ -81,15 +80,16 @@ yq 'to_entries | from_entries' sample.yml
``` ```
will output will output
```yaml ```yaml
0: a - 0
1: b - a
- 1
- b
``` ```
## 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 {a: 1, b: 2}
b: 2
``` ```
then then
```bash ```bash
@ -97,8 +97,10 @@ yq 'with_entries(.key |= "KEY_" + .)' sample.yml
``` ```
will output will output
```yaml ```yaml
KEY_a: 1 - KEY_a
KEY_b: 2 - 1
- KEY_b
- 2
``` ```
## Custom sort map keys ## Custom sort map keys
@ -106,9 +108,7 @@ 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 {a: 1, c: 3, b: 2}
c: 3
b: 2
``` ```
then then
```bash ```bash
@ -116,18 +116,19 @@ yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml
``` ```
will output will output
```yaml ```yaml
c: 3 !!tag
b: 2 - c
a: 1 - 3
- b
- 2
- 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: {a: {b: bird}, c: {d: dog}}
b: bird
c:
d: dog
``` ```
then then
```bash ```bash
@ -135,7 +136,7 @@ yq 'with_entries(select(.value | has("b")))' sample.yml
``` ```
will output will output
```yaml ```yaml
a: - a
b: bird - {b: bird}
``` ```

View File

@ -21,8 +21,7 @@ cat
## Multi value variable ## Multi value variable
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
- cat [cat, dog]
- dog
``` ```
then then
```bash ```bash
@ -39,14 +38,7 @@ 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": {"posts": [{"title": First post, "author": anon}, {"title": A well-written article, "author": person1}], "realnames": {"anon": Anonymous Coward, "person1": Person McPherson}}
- "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

@ -35,15 +35,15 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *
} }
parentNode := candidate.Parent parentNode := candidate.Parent
parentPath := parentNode.GetPath() candidatePath := candidate.GetPath()
childPath := parentPath[len(parentPath)-1] childPath := candidatePath[len(candidatePath)-1]
if parentNode.Kind == MappingNode { if parentNode.Kind == MappingNode {
deleteFromMap(candidate.Parent, childPath) deleteFromMap(candidate.Parent, childPath)
} else if parentNode.Kind == SequenceNode { } else if parentNode.Kind == SequenceNode {
deleteFromArray(candidate.Parent, childPath) deleteFromArray(candidate.Parent, childPath)
} else { } else {
return Context{}, fmt.Errorf("Cannot delete nodes from parent of tag %v", parentNode.Tag) return Context{}, fmt.Errorf("cannot delete nodes from parent of tag %v", parentNode.Tag)
} }
} }
return context, nil return context, nil

View File

@ -100,7 +100,7 @@ var deleteOperatorScenarios = []expressionScenario{
document: `{a: {a1: fred, a2: frood}}`, document: `{a: {a1: fred, a2: frood}}`,
expression: `del(.. | select(.=="frood"))`, expression: `del(.. | select(.=="frood"))`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{a: {a1: fred}}\n", "D0, P[], (doc)::{a: {a1: fred}}\n",
}, },
}, },
{ {
@ -131,16 +131,14 @@ var deleteOperatorScenarios = []expressionScenario{
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}}`,
expression: `del(..)`, expression: `del(..)`,
expected: []string{ expected: []string{},
"D0, P[], (!!map)::{}\n",
},
}, },
{ {
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}}`,
expression: `del(.. | select(tag == "!!map") | (.b.thing1,.thing2))`, expression: `del(.. | select(tag == "!!map") | (.b.thing1,.thing2))`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::a: {thing1: yep, thing3: hi, b: {great: huh}}\n", "D0, P[], (doc)::a: {thing1: yep, thing3: hi, b: {great: huh}}\n",
}, },
}, },
{ {
@ -172,7 +170,7 @@ var deleteOperatorScenarios = []expressionScenario{
document: `{a: {name: frog, b: {name: blog, age: 12}}}`, document: `{a: {name: frog, b: {name: blog, age: 12}}}`,
expression: `del(.. | select(has("name")).name)`, expression: `del(.. | select(has("name")).name)`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::{a: {b: {age: 12}}}\n", "D0, P[], (doc)::{a: {b: {age: 12}}}\n",
}, },
}, },
} }

View File

@ -45,7 +45,7 @@ var documentIndexScenarios = []expressionScenario{
expression: `.a | ({"match": ., "doc": document_index})`, expression: `.a | ({"match": ., "doc": document_index})`,
expected: []string{ expected: []string{
"D0, P[], (!!map)::match: cat\ndoc: 0\n", "D0, P[], (!!map)::match: cat\ndoc: 0\n",
"D0, P[], (!!map)::match: frog\ndoc: 1\n", "D1, P[], (!!map)::match: frog\ndoc: 1\n",
}, },
}, },
} }