mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-03 10:55:36 +00:00
Added set path example
This commit is contained in:
parent
688fe55bb9
commit
22f376bbfd
@ -122,6 +122,31 @@ a:
|
|||||||
b: things
|
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
|
## Set array path
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
|||||||
@ -4,6 +4,16 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var documentToPrune = `
|
||||||
|
parentA: bob
|
||||||
|
parentB:
|
||||||
|
child1: i am child1
|
||||||
|
child2: i am child2
|
||||||
|
parentC:
|
||||||
|
child1: me child1
|
||||||
|
child2: me child2
|
||||||
|
`
|
||||||
|
|
||||||
var pathOperatorScenarios = []expressionScenario{
|
var pathOperatorScenarios = []expressionScenario{
|
||||||
{
|
{
|
||||||
description: "Map path",
|
description: "Map path",
|
||||||
@ -78,6 +88,15 @@ var pathOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], ()::a:\n b: things\n",
|
"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",
|
description: "Set array path",
|
||||||
document: `a: [cat, frog]`,
|
document: `a: [cat, frog]`,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user