Added set path example

This commit is contained in:
Mike Farah 2022-11-10 10:17:08 +11:00
parent 688fe55bb9
commit 22f376bbfd
2 changed files with 44 additions and 0 deletions

View File

@ -122,6 +122,31 @@ a:
b: things
```
## Set path to extract prune deep paths
Like pick but recursive.
Given a sample.yml file of:
```yaml
parentA: bob
parentB:
child1: i am child1
child2: i am child2
parentC:
child1: me child1
child2: me child2
```
then
```bash
yq '(.parentB.child2, .parentC.child1) as $i ireduce({}; setpath($i | path; $i))' sample.yml
```
will output
```yaml
parentB:
child2: i am child2
parentC:
child1: me child1
```
## Set array path
Given a sample.yml file of:
```yaml

View File

@ -4,6 +4,16 @@ import (
"testing"
)
var documentToPrune = `
parentA: bob
parentB:
child1: i am child1
child2: i am child2
parentC:
child1: me child1
child2: me child2
`
var pathOperatorScenarios = []expressionScenario{
{
description: "Map path",
@ -78,6 +88,15 @@ var pathOperatorScenarios = []expressionScenario{
"D0, P[], ()::a:\n b: things\n",
},
},
{
description: "Set path to extract prune deep paths",
subdescription: "Like pick but recursive.",
document: documentToPrune,
expression: `(.parentB.child2, .parentC.child1) as $i ireduce({}; setpath($i | path; $i))`,
expected: []string{
"D0, P[], (!!map)::parentB:\n child2: i am child2\nparentC:\n child1: me child1\n",
},
},
{
description: "Set array path",
document: `a: [cat, frog]`,