You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
4
5
5
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.
Copy file name to clipboardExpand all lines: pkg/yqlib/doc/operators/path.md
+59-1Lines changed: 59 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,12 @@
1
1
# Path
2
2
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.
4
4
5
5
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
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
+
9
+
7
10
{% hint style="warning" %}
8
11
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
returnContext{}, fmt.Errorf("DELPATHS: expected a sequence of sequences, but found %v", pathArraysNode.Tag)
117
+
}
118
+
119
+
updatedContext:=context
120
+
121
+
fori, child:=rangepathArraysNode.Content {
122
+
123
+
ifchild.Tag!="!!seq" {
124
+
returnContext{}, 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)
Copy file name to clipboardExpand all lines: pkg/yqlib/operator_path_test.go
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -93,6 +93,30 @@ var pathOperatorScenarios = []expressionScenario{
93
93
"D0, P[], ()::a:\n - things\n",
94
94
},
95
95
},
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\"]]",
0 commit comments