Repository navigation
Expand file tree
/
Copy pathjson_query.feature
More file actions
337 lines (267 loc) · 11.6 KB
/
Copy pathjson_query.feature
File metadata and controls
337 lines (267 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
@cli
Feature: celpy behaves a little like JSON Query.
See https://stedolan.github.io/jq/manual/
We can use celpy to process a few JQ-like expressions.
Generally, JQ's syntax cannot be made to work with CEL's syntax rule of ``primary : ["."] IDENT``
Some scenarios leverage the default behavior of ``--json-package`` to use the JSON document to populate a package.
We can use ``.name`` to refer to a name within this package.
The other scenarios use ``--json-document=_`` to create the document as a local variable, ``_``.
This leads to a change in the JQ syntax, where we have to use ``_["name"]`` instead of ``.["name"]``.
The JQ semantics for many operators are not the same as CEL, leading to some differences in the behavior.
Scenario: Extract a field from a JSON document.
Given JSON document '{"some": {"complex": {"path": 42}}}'
When echo document | celpy '.some.complex.path' is run
Then stdout is '42\n'
And stderr is ''
And exit status is 0
Scenario: Handle more complex types.
Given JSON document '{"creationTimestamp": "2018-07-06T05:04:03Z", "deleteProtection": false, "name": "projects/project-123/zones/us-east1-b/instances/dev/ec2", "instanceSize": "m1.standard"}'
When echo document | celpy '.creationTimestamp' is run
Then stdout is '"2018-07-06T05:04:03Z"\n'
And stderr is ''
And exit status is 0
Scenario: Extract a field from multiple JSON documents.
Given JSON document '{"some": {"complex": {"path": 6}}}'
And JSON document '{"some": {"complex": {"path": 7}}}'
When echo document | celpy '.some.complex.path' is run
Then stdout is '6\n7\n'
And stderr is ''
And exit status is 0
Scenario: Slurp in a large, multiline document.
Given JSON document '{"creationTimestamp": "2018-07-06T05:04:03Z", '
And JSON document '"deleteProtection": false, '
And JSON document '"name": "projects/project-123/zones/us-east1-b/instances/dev/ec2", '
And JSON document '"instanceSize": "m1.standard"}'
When echo document | celpy --slurp '.creationTimestamp' is run
Then stdout is '"2018-07-06T05:04:03Z"\n'
And stderr is ''
And exit status is 0
Scenario: JQ Basic Filters, Identity: .
This requires a syntax change.
Given JSON document '"Hello, world!"'
When echo document | celpy --json-document=_ '_' is run
Then stdout is '"Hello, world!"\n'
And stderr is ''
And exit status is 0
Scenario: JQ Basic Filters, Object Identifier-Index: .foo, .foo.bar
Given JSON document '{"foo": 42, "bar": "less interesting data"}'
And JSON document '{"notfoo": true, "alsonotfoo": false}'
And JSON document '{"foo": 42}'
When echo document | celpy '.foo' is run
Then stdout is '42\nnull\n42\n'
And stderr is ''
And exit status is 0
Scenario: JQ Basic Filters, Object Identifier-Index: .foo?
This requires a syntax change.
Given JSON document '{"foo": 42, "bar": "less interesting data"}'
And JSON document '{"notfoo": true, "alsonotfoo": false}'
And JSON document '{"foo": 42}'
And JSON document '[1,2]'
When echo document | celpy --json-document=_ 'has(_.foo) ? _.foo : null' is run
Then stdout is '42\nnull\n42\nnull\n'
And stderr is ''
And exit status is 0
Scenario: JQ Basic Filters, Object Identifier-Index: .foo? -- alternative 1
This requires a syntax change.
Given JSON document '{"foo": 42, "bar": "less interesting data"}'
And JSON document '{"notfoo": true, "alsonotfoo": false}'
And JSON document '{"foo": 42}'
And JSON document '[1,2]'
When echo document | celpy --json-document=_ 'has(_.foo) && _.foo' is run
Then stdout is '42\nfalse\n42\nfalse\n'
And stderr is ''
And exit status is 0
Scenario: JQ Basic Filters, Object Identifier-Index: .["foo"]
This requires a syntax change.
Given JSON document '{"foo": 42, "bar": "less interesting data"}'
And JSON document '{"notfoo": true, "alsonotfoo": false}'
And JSON document '{"foo": 42}'
When echo document | celpy --json-document=_ '_["foo"]' is run
Then stdout is '42\nnull\n42\n'
And stderr is ''
And exit status is 0
Scenario: JQ Basic Filters, Object Identifier-Index: .[2]
This requires a syntax change.
Other examples (i.e., negative indexing and ranges do not work.)
Given JSON document '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
When echo document | celpy --json-document=_ '_[0]' is run
Then stdout is '{"name": "JSON", "good": true}\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: Addition: +
Given JSON document '{"a": 7}'
When echo document | celpy '.a + 1' is run
Then stdout is '8\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: Subtraction: -
Given JSON document '{"a":3}'
When echo document | celpy '4 - .a' is run
Then stdout is '1\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: Multiplication, Division, Modulus
This requires a syntax change.
Given JSON document '5'
When echo document | celpy --json-document=_ '10 / _ * 3' is run
Then stdout is '6\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: length
This requires a syntax change.
Given JSON document '[[1,2], "string", {"a":2}, null]'
When echo document | celpy --json-document=_ '_.map(x, size(x))' is run
Then stdout is '[2, 6, 1, 0]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: has(key)
This requires a syntax change.
Given JSON document '[{"foo": 42}, {}]'
When echo document | celpy --json-document=_ '_.map(x, has(x.foo))' is run
Then stdout is '[true, false]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: select(boolean_expression), example 1
This requires a syntax change.
Given JSON document '[1,5,3,0,7]'
When echo document | celpy --json-document=_ '_.filter(x, x >= 2)' is run
Then stdout is '[5, 3, 7]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: select(boolean_expression), example 2
This requires a syntax change.
The semantics of CEL filter() don't correspond precisely to JQ select()
Given JSON document '[{"id": "first", "val": 1}, {"id": "second", "val": 2}]'
When echo document | celpy --json-document=_ '_.filter(x, x.id == "second")' is run
Then stdout is '[{"id": "second", "val": 2}]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: map(expr)
This requires a syntax change.
Given JSON document '[1,2,3]'
When echo document | celpy --json-document=_ '_.map(x, x+1)' is run
Then stdout is '[2, 3, 4]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: any(expr)
This requires a syntax change.
Given JSON document '[true, false]'
When echo document | celpy --json-document=_ '_.exists(x, x)' is run
Then stdout is 'true\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: all(expr)
This requires a syntax change.
Given JSON document '[true, false]'
When echo document | celpy --json-document=_ '_.all(x, x)' is run
Then stdout is 'false\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: tonumber(expr)
This requires a syntax change.
Given JSON document '[1, "1"]'
When echo document | celpy --json-document=_ '_.map(x, int(x))' is run
Then stdout is '[1, 1]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: tostring(expr)
This requires a syntax change.
The semantics of CEL string() don't correspond precisely to JQ tostring()
Given JSON document '[1, "1", [1]]'
When echo document | celpy --json-document=_ '_.map(x, string(x))' is run
Then stdout is '["1", "1", "ListType([IntType(1)])"]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: contains(expr), string case
This requires a syntax change.
Given JSON document '"foobar"'
When echo document | celpy --json-document=_ '_.contains("bar")' is run
Then stdout is 'true\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: startswith(expr)
This requires a syntax change.
Given JSON document '["fo", "foo", "barfoo", "foobar", "barfoob"]'
When echo document | celpy --json-document=_ '_.map(x, x.startsWith("foo"))' is run
Then stdout is '[false, true, false, true, false]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: endswith(expr)
This requires a syntax change.
Given JSON document '["foobar", "barfoo"]'
When echo document | celpy --json-document=_ '_.map(x, x.endsWith("foo"))' is run
Then stdout is '[false, true]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Builtin operators and functions: dates
This requires a syntax change.
Given JSON document '"2015-03-05T23:51:47Z"'
When echo document | celpy --json-document=_ 'int(timestamp(_))' is run
Then stdout is '1425599507\n'
And stderr is ''
And exit status is 0
Scenario: JQ Conditionals and Comparisons: ==
This requires a syntax change.
The semantics of == are different. CEL does not do the required type coercions.
Given JSON document '1'
And JSON document '1.0'
And JSON document '"1"'
And JSON document '"banana"'
When echo document | celpy --json-document=_ '_ == 1' is run
Then stdout is 'true\nnull\nnull\nnull\n'
And stderr is ''
And exit status is 0
Scenario: JQ Conditionals and Comparisons: if-then-else
This requires a syntax change.
Given JSON document '2'
When echo document | celpy --json-document=_ '_ == 0 ? "zero" : _ == 1 ? "one" : "many"' is run
Then stdout is '"many"\n'
And stderr is ''
And exit status is 0
Scenario: JQ Conditionals and Comparisons: <, <=, >, >=
This requires a syntax change.
The semantics of relational operators are different. CEL does not do the same type coercions.
Given JSON document '2'
When echo document | celpy --json-document=_ '_ < 5' is run
Then stdout is 'true\n'
And stderr is ''
And exit status is 0
Scenario: JQ Conditionals and Comparisons: and/or/not, example 1
This requires a syntax change.
The semantics of logical operators are different. CEL does not do the same type coercions.
Given JSON document '2'
When echo document | celpy -n '42 && "a string"' is run
Then stdout is ''
And stderr contains 'ERROR: <input>:1:1 found no matching overload for _&&_'
And exit status is 2
Scenario: JQ Conditionals and Comparisons: and/or/not, example 2
This requires a syntax change.
The semantics of relational operators are different. CEL does not do the same type coercions.
Given JSON document '2'
When echo document | celpy -n '[true || false, false || false]' is run
Then stdout is '[true, false]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Conditionals and Comparisons: and/or/not, example 3
This requires a syntax change.
The semantics of relational operators are different. CEL does not do the same type coercions.
Given JSON document '2'
When echo document | celpy -n '[true && false, true && true]' is run
Then stdout is '[false, true]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Conditionals and Comparisons: and/or/not, example 4
This requires a syntax change.
The semantics of relational operators are different. CEL does not do the same type coercions.
Given JSON document '2'
When echo document | celpy -n '[!true, !false]' is run
Then stdout is '[false, true]\n'
And stderr is ''
And exit status is 0
Scenario: JQ Regular Expressions
This requires a syntax change.
Given JSON document '"foo"'
When echo document | celpy --json-document=_ '_.matches("foo")' is run
Then stdout is 'true\n'
And stderr is ''
And exit status is 0