Added recurse examples

This commit is contained in:
Mike Farah 2021-01-08 12:11:29 +11:00
parent db62a16007
commit ceff2cc18d
2 changed files with 60 additions and 0 deletions

View File

@ -32,6 +32,48 @@ a: frog
frog frog
``` ```
## Recursively find nodes with keys
Given a sample.yml file of:
```yaml
a:
name: frog
b:
name: blog
age: 12
```
then
```bash
yq eval '.. | select(has("name"))' sample.yml
```
will output
```yaml
name: frog
b:
name: blog
age: 12
name: blog
age: 12
```
## Recursively find nodes with values
Given a sample.yml file of:
```yaml
a:
nameA: frog
b:
nameB: frog
age: 12
```
then
```bash
yq eval '.. | select(. == "frog")' sample.yml
```
will output
```yaml
frog
frog
```
## Recurse map (values and keys) ## Recurse map (values and keys)
Note that the map key appears in the results Note that the map key appears in the results

View File

@ -62,6 +62,24 @@ var recursiveDescentOperatorScenarios = []expressionScenario{
"D0, P[a], (!!str)::frog\n", "D0, P[a], (!!str)::frog\n",
}, },
}, },
{
description: "Recursively find nodes with keys",
document: `{a: {name: frog, b: {name: blog, age: 12}}}`,
expression: `.. | select(has("name"))`,
expected: []string{
"D0, P[a], (!!map)::{name: frog, b: {name: blog, age: 12}}\n",
"D0, P[a b], (!!map)::{name: blog, age: 12}\n",
},
},
{
description: "Recursively find nodes with values",
document: `{a: {nameA: frog, b: {nameB: frog, age: 12}}}`,
expression: `.. | select(. == "frog")`,
expected: []string{
"D0, P[a nameA], (!!str)::frog\n",
"D0, P[a b nameB], (!!str)::frog\n",
},
},
{ {
description: "Recurse map (values and keys)", description: "Recurse map (values and keys)",
subdescription: "Note that the map key appears in the results", subdescription: "Note that the map key appears in the results",