Skip to content

Commit 7d49d40

Browse files
committed
Added parents operator
1 parent 6251e95 commit 7d49d40

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

pkg/yqlib/doc/operators/parent.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ will output
5656
sam
5757
```
5858

59+
## Get parents
60+
Match all parents
61+
62+
Given a sample.yml file of:
63+
```yaml
64+
a:
65+
b:
66+
c: cat
67+
```
68+
then
69+
```bash
70+
yq '.a.b.c | parents' sample.yml
71+
```
72+
will output
73+
```yaml
74+
- c: cat
75+
- b:
76+
c: cat
77+
- a:
78+
b:
79+
c: cat
80+
```
81+
5982
## N-th parent
6083
You can optionally supply the number of levels to go up for the parent, the default being 1.
6184

pkg/yqlib/lexer_participle.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ var participleYqRules = []*participleYqRule{
130130
simpleOp("contains", containsOpType),
131131
simpleOp("split", splitStringOpType),
132132

133+
simpleOp("parents", getParentsOpType),
133134
{"ParentWithLevel", `parent\([0-9]+\)`, parentWithLevel(), 0},
134135
{"ParentWithDefaultLevel", `parent`, parentWithDefaultLevel(), 0},
135136

pkg/yqlib/operation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ var getKindOpType = &operationType{Type: "GET_KIND", NumArgs: 0, Precedence: 50,
128128
var getKeyOpType = &operationType{Type: "GET_KEY", NumArgs: 0, Precedence: 50, Handler: getKeyOperator}
129129
var isKeyOpType = &operationType{Type: "IS_KEY", NumArgs: 0, Precedence: 50, Handler: isKeyOperator}
130130
var getParentOpType = &operationType{Type: "GET_PARENT", NumArgs: 0, Precedence: 50, Handler: getParentOperator}
131+
var getParentsOpType = &operationType{Type: "GET_PARENTS", NumArgs: 0, Precedence: 50, Handler: getParentsOperator}
131132

132133
var getCommentOpType = &operationType{Type: "GET_COMMENT", NumArgs: 0, Precedence: 50, Handler: getCommentsOperator}
133134
var getAnchorOpType = &operationType{Type: "GET_ANCHOR", NumArgs: 0, Precedence: 50, Handler: getAnchorOperator}

pkg/yqlib/operator_parent.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ type parentOpPreferences struct {
66
Level int
77
}
88

9+
func getParentsOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
10+
log.Debugf("getParentsOperator")
11+
12+
var results = list.New()
13+
14+
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
15+
candidate := el.Value.(*CandidateNode)
16+
parentsList := &CandidateNode{Kind: SequenceNode, Tag: "!!seq"}
17+
parent := candidate.Parent
18+
for parent != nil {
19+
parentsList.AddChild(parent)
20+
parent = parent.Parent
21+
}
22+
results.PushBack(parentsList)
23+
}
24+
25+
return context.ChildContext(results), nil
26+
27+
}
28+
929
func getParentOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
1030
log.Debugf("getParentOperator")
1131

pkg/yqlib/operator_parent_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ var parentOperatorScenarios = []expressionScenario{
2929
"D0, P[b name], (!!str)::sam\n",
3030
},
3131
},
32+
{
33+
description: "Get parents",
34+
subdescription: "Match all parents",
35+
document: "{a: {b: {c: cat} } }",
36+
expression: `.a.b.c | parents`,
37+
expected: []string{
38+
"D0, P[], (!!seq)::- {c: cat}\n- {b: {c: cat}}\n- {a: {b: {c: cat}}}\n",
39+
},
40+
},
3241
{
3342
description: "N-th parent",
3443
subdescription: "You can optionally supply the number of levels to go up for the parent, the default being 1.",

0 commit comments

Comments
 (0)