Skip to content

Commit b5b81ab

Browse files
committed
Added DELPATHS operator
1 parent da3f3b9 commit b5b81ab

File tree

6 files changed

+159
-23
lines changed

6 files changed

+159
-23
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Path
22

3-
The path operator can be used to get the traversal paths of matching nodes in an expression. The path is returned as an array, which if traversed in order will lead to the matching node.
3+
The `path` operator can be used to get the traversal paths of matching nodes in an expression. The path is returned as an array, which if traversed in order will lead to the matching node.
44

55
You can get the key/index of matching nodes by using the `path` operator to return the path array then piping that through `.[-1]` to get the last element of that array, the key.
6+
7+
Use `setpath` to set a value to the path array returned by `path`, and similarly `delpaths` for an array of path arrays.
8+

pkg/yqlib/doc/operators/path.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Path
22

3-
The path operator can be used to get the traversal paths of matching nodes in an expression. The path is returned as an array, which if traversed in order will lead to the matching node.
3+
The `path` operator can be used to get the traversal paths of matching nodes in an expression. The path is returned as an array, which if traversed in order will lead to the matching node.
44

55
You can get the key/index of matching nodes by using the `path` operator to return the path array then piping that through `.[-1]` to get the last element of that array, the key.
66

7+
Use `setpath` to set a value to the path array returned by `path`, and similarly `delpaths` for an array of path arrays.
8+
9+
710
{% hint style="warning" %}
811
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
912

@@ -154,3 +157,58 @@ a:
154157
- things
155158
```
156159
160+
## Delete path
161+
Notice delpaths takes an _array_ of paths.
162+
163+
Given a sample.yml file of:
164+
```yaml
165+
a:
166+
b: cat
167+
c: dog
168+
d: frog
169+
```
170+
then
171+
```bash
172+
yq 'delpaths([["a", "c"], ["a", "d"]])' sample.yml
173+
```
174+
will output
175+
```yaml
176+
a:
177+
b: cat
178+
```
179+
180+
## Delete array path
181+
Given a sample.yml file of:
182+
```yaml
183+
a:
184+
- cat
185+
- frog
186+
```
187+
then
188+
```bash
189+
yq 'delpaths([["a", 0]])' sample.yml
190+
```
191+
will output
192+
```yaml
193+
a:
194+
- frog
195+
```
196+
197+
## Delete - wrong parameter
198+
delpaths does not work with a single path array
199+
200+
Given a sample.yml file of:
201+
```yaml
202+
a:
203+
- cat
204+
- frog
205+
```
206+
then
207+
```bash
208+
yq 'delpaths(["a", 0])' sample.yml
209+
```
210+
will output
211+
```bash
212+
Error: DELPATHS: expected entry [0] to be a sequence, but its a !!str. Note that delpaths takes an array of path arrays, e.g. [["a", "b"]]
213+
```
214+

pkg/yqlib/lexer_participle.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var participleYqRules = []*participleYqRule{
129129
simpleOp("file_?index|fileIndex|fi", getFileIndexOpType),
130130
simpleOp("path", getPathOpType),
131131
simpleOp("set_?path", setPathOpType),
132+
simpleOp("del_?paths", delPathsOpType),
132133

133134
simpleOp("to_?entries|toEntries", toEntriesOpType),
134135
simpleOp("from_?entries|fromEntries", fromEntriesOpType),

pkg/yqlib/lib.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ var getFileIndexOpType = &operationType{Type: "GET_FILE_INDEX", NumArgs: 0, Prec
133133

134134
var getPathOpType = &operationType{Type: "GET_PATH", NumArgs: 0, Precedence: 50, Handler: getPathOperator}
135135
var setPathOpType = &operationType{Type: "SET_PATH", NumArgs: 1, Precedence: 50, Handler: setPathOperator}
136+
var delPathsOpType = &operationType{Type: "DEL_PATHS", NumArgs: 1, Precedence: 50, Handler: delPathsOperator}
136137

137138
var explodeOpType = &operationType{Type: "EXPLODE", NumArgs: 1, Precedence: 50, Handler: explodeOperator}
138139
var sortByOpType = &operationType{Type: "SORT_BY", NumArgs: 1, Precedence: 50, Handler: sortByOperator}

pkg/yqlib/operator_path.go

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,24 @@ func createPathNodeFor(pathElement interface{}) *yaml.Node {
1616
}
1717
}
1818

19-
func getPathArrayFromExp(d *dataTreeNavigator, context Context, pathExp *ExpressionNode) ([]interface{}, error) {
20-
lhsPathContext, err := d.GetMatchingNodes(context.ReadOnlyClone(), pathExp)
21-
22-
if err != nil {
23-
return nil, err
24-
}
25-
26-
if lhsPathContext.MatchingNodes.Len() != 1 {
27-
return nil, fmt.Errorf("expected single path but found %v results instead", lhsPathContext.MatchingNodes.Len())
28-
}
29-
lhsValue := lhsPathContext.MatchingNodes.Front().Value.(*CandidateNode)
30-
if lhsValue.Node.Kind != yaml.SequenceNode {
31-
return nil, fmt.Errorf("expected path array, but got %v instead", lhsValue.Node.Tag)
19+
func getPathArrayFromNode(funcName string, node *yaml.Node) ([]interface{}, error) {
20+
if node.Kind != yaml.SequenceNode {
21+
return nil, fmt.Errorf("%v: expected path array, but got %v instead", funcName, node.Tag)
3222
}
3323

34-
path := make([]interface{}, len(lhsValue.Node.Content))
24+
path := make([]interface{}, len(node.Content))
3525

36-
for i, childNode := range lhsValue.Node.Content {
26+
for i, childNode := range node.Content {
3727
if childNode.Tag == "!!str" {
3828
path[i] = childNode.Value
3929
} else if childNode.Tag == "!!int" {
4030
number, err := parseInt(childNode.Value)
4131
if err != nil {
42-
return nil, fmt.Errorf("could not parse %v as an int: %w", childNode.Value, err)
32+
return nil, fmt.Errorf("%v: could not parse %v as an int: %w", funcName, childNode.Value, err)
4333
}
4434
path[i] = number
4535
} else {
46-
return nil, fmt.Errorf("expected either a !!str or !!int in the path, found %v instead", childNode.Tag)
36+
return nil, fmt.Errorf("%v: expected either a !!str or !!int in the path, found %v instead", funcName, childNode.Tag)
4737
}
4838

4939
}
@@ -58,7 +48,18 @@ func setPathOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
5848
return Context{}, fmt.Errorf("SETPATH must be given a block (;), got %v instead", expressionNode.RHS.Operation.OperationType.Type)
5949
}
6050

61-
lhsPath, err := getPathArrayFromExp(d, context, expressionNode.RHS.LHS)
51+
lhsPathContext, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS.LHS)
52+
53+
if err != nil {
54+
return Context{}, err
55+
}
56+
57+
if lhsPathContext.MatchingNodes.Len() != 1 {
58+
return Context{}, fmt.Errorf("SETPATH: expected single path but found %v results instead", lhsPathContext.MatchingNodes.Len())
59+
}
60+
lhsValue := lhsPathContext.MatchingNodes.Front().Value.(*CandidateNode)
61+
62+
lhsPath, err := getPathArrayFromNode("SETPATH", lhsValue.Node)
6263

6364
if err != nil {
6465
return Context{}, err
@@ -68,8 +69,6 @@ func setPathOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
6869

6970
assignmentOp := &Operation{OperationType: assignOpType}
7071

71-
//TODO if context is empty, create a new one
72-
7372
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
7473
candidate := el.Value.(*CandidateNode)
7574

@@ -79,7 +78,7 @@ func setPathOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
7978
}
8079

8180
if targetContextValue.MatchingNodes.Len() != 1 {
82-
return Context{}, fmt.Errorf("Expected single value on RHS but found %v", targetContextValue.MatchingNodes.Len())
81+
return Context{}, fmt.Errorf("SETPATH: expected single value on RHS but found %v", targetContextValue.MatchingNodes.Len())
8382
}
8483

8584
rhsOp := &Operation{OperationType: valueOpType, CandidateNode: targetContextValue.MatchingNodes.Front().Value.(*CandidateNode)}
@@ -100,6 +99,56 @@ func setPathOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
10099
return context, nil
101100
}
102101

102+
func delPathsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
103+
log.Debugf("delPaths")
104+
// single RHS expression that returns an array of paths (array of arrays)
105+
106+
pathArraysContext, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
107+
if err != nil {
108+
return Context{}, err
109+
}
110+
if pathArraysContext.MatchingNodes.Len() != 1 {
111+
return Context{}, fmt.Errorf("DELPATHS: expected single value but found %v", pathArraysContext.MatchingNodes.Len())
112+
}
113+
pathArraysNode := pathArraysContext.MatchingNodes.Front().Value.(*CandidateNode).Node
114+
115+
if pathArraysNode.Tag != "!!seq" {
116+
return Context{}, fmt.Errorf("DELPATHS: expected a sequence of sequences, but found %v", pathArraysNode.Tag)
117+
}
118+
119+
updatedContext := context
120+
121+
for i, child := range pathArraysNode.Content {
122+
123+
if child.Tag != "!!seq" {
124+
return Context{}, fmt.Errorf("DELPATHS: expected entry [%v] to be a sequence, but its a %v. Note that delpaths takes an array of path arrays, e.g. [[\"a\", \"b\"]]", i, child.Tag)
125+
}
126+
childPath, err := getPathArrayFromNode("DELPATHS", child)
127+
128+
if err != nil {
129+
return Context{}, err
130+
}
131+
132+
childTraversalExp := createTraversalTree(childPath, traversePreferences{}, false)
133+
deleteChildOp := &Operation{OperationType: deleteChildOpType}
134+
135+
deleteChildOpNode := &ExpressionNode{
136+
Operation: deleteChildOp,
137+
RHS: childTraversalExp,
138+
}
139+
140+
updatedContext, err = d.GetMatchingNodes(updatedContext, deleteChildOpNode)
141+
142+
if err != nil {
143+
return Context{}, err
144+
}
145+
146+
}
147+
148+
return updatedContext, nil
149+
150+
}
151+
103152
func getPathOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
104153
log.Debugf("GetPath")
105154

pkg/yqlib/operator_path_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ var pathOperatorScenarios = []expressionScenario{
9393
"D0, P[], ()::a:\n - things\n",
9494
},
9595
},
96+
{
97+
description: "Delete path",
98+
subdescription: "Notice delpaths takes an _array_ of paths.",
99+
document: `{a: {b: cat, c: dog, d: frog}}`,
100+
expression: `delpaths([["a", "c"], ["a", "d"]])`,
101+
expected: []string{
102+
"D0, P[], (doc)::{a: {b: cat}}\n",
103+
},
104+
},
105+
{
106+
description: "Delete array path",
107+
document: `a: [cat, frog]`,
108+
expression: `delpaths([["a", 0]])`,
109+
expected: []string{
110+
"D0, P[], (doc)::a: [frog]\n",
111+
},
112+
},
113+
{
114+
description: "Delete - wrong parameter",
115+
subdescription: "delpaths does not work with a single path array",
116+
document: `a: [cat, frog]`,
117+
expression: `delpaths(["a", 0])`,
118+
expectedError: "DELPATHS: expected entry [0] to be a sequence, but its a !!str. Note that delpaths takes an array of path arrays, e.g. [[\"a\", \"b\"]]",
119+
},
96120
}
97121

98122
func TestPathOperatorsScenarios(t *testing.T) {

0 commit comments

Comments
 (0)