mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-26 08:25:38 +00:00
Added set path example
This commit is contained in:
parent
688fe55bb9
commit
22f376bbfd
@ -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
|
||||
|
@ -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]`,
|
||||
|
Loading…
Reference in New Issue
Block a user