@aws-lambda-powertools/jmespath 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/Expression.js +5 -18
- package/lib/cjs/Functions.js +4 -6
- package/lib/cjs/Lexer.js +275 -204
- package/lib/cjs/ParsedResult.js +2 -0
- package/lib/cjs/Parser.js +676 -433
- package/lib/cjs/TreeInterpreter.js +364 -233
- package/lib/cjs/errors.d.ts +0 -6
- package/lib/cjs/errors.d.ts.map +1 -1
- package/lib/cjs/errors.js +39 -0
- package/lib/esm/Expression.js +5 -18
- package/lib/esm/Functions.js +4 -6
- package/lib/esm/Lexer.js +274 -203
- package/lib/esm/ParsedResult.js +2 -0
- package/lib/esm/Parser.js +675 -432
- package/lib/esm/TreeInterpreter.js +363 -232
- package/lib/esm/errors.d.ts +0 -6
- package/lib/esm/errors.d.ts.map +1 -1
- package/lib/esm/errors.js +39 -0
- package/package.json +2 -2
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _TreeInterpreter_instances, _TreeInterpreter_functions, _TreeInterpreter_visitSubexpressionOrIndexExpressionOrPipe, _TreeInterpreter_visitField, _TreeInterpreter_visitComparator, _TreeInterpreter_visitCurrent, _TreeInterpreter_visitExpref, _TreeInterpreter_visitFunctionExpression, _TreeInterpreter_visitFilterProjection, _TreeInterpreter_visitFlatten, _TreeInterpreter_visitIdentity, _TreeInterpreter_visitIndex, _TreeInterpreter_visitSlice, _TreeInterpreter_visitKeyValPair, _TreeInterpreter_visitLiteral, _TreeInterpreter_visitMultiSelectObject, _TreeInterpreter_visitMultiSelectList, _TreeInterpreter_visitOrExpression, _TreeInterpreter_visitAndExpression, _TreeInterpreter_visitNotExpression, _TreeInterpreter_visitProjection, _TreeInterpreter_visitValueProjection;
|
|
13
1
|
import { isIntegerNumber, isRecord, isStrictEqual, } from '@aws-lambda-powertools/commons/typeutils';
|
|
14
2
|
import { ArityError, JMESPathError, JMESPathTypeError, UnknownFunctionError, VariadicArityError, } from './errors.js';
|
|
15
3
|
import { Expression } from './Expression.js';
|
|
@@ -25,19 +13,18 @@ import { isTruthy, sliceArray } from './utils.js';
|
|
|
25
13
|
* @internal
|
|
26
14
|
*/
|
|
27
15
|
class TreeInterpreter {
|
|
16
|
+
#functions;
|
|
28
17
|
/**
|
|
29
18
|
* @param options The options to use for the interpreter.
|
|
30
19
|
*/
|
|
31
20
|
constructor(options) {
|
|
32
|
-
_TreeInterpreter_instances.add(this);
|
|
33
|
-
_TreeInterpreter_functions.set(this, void 0);
|
|
34
21
|
if (options?.customFunctions) {
|
|
35
|
-
|
|
22
|
+
this.#functions = options.customFunctions;
|
|
36
23
|
}
|
|
37
24
|
else {
|
|
38
|
-
|
|
25
|
+
this.#functions = new Functions();
|
|
39
26
|
}
|
|
40
|
-
|
|
27
|
+
this.#functions.introspectMethods();
|
|
41
28
|
}
|
|
42
29
|
/**
|
|
43
30
|
* Visit a node in the AST.
|
|
@@ -50,28 +37,28 @@ class TreeInterpreter {
|
|
|
50
37
|
visit(node, value) {
|
|
51
38
|
const nodeType = node.type;
|
|
52
39
|
const visitMethods = {
|
|
53
|
-
subexpression:
|
|
54
|
-
field:
|
|
55
|
-
comparator:
|
|
56
|
-
current:
|
|
57
|
-
expref:
|
|
58
|
-
function_expression:
|
|
59
|
-
filter_projection:
|
|
60
|
-
flatten:
|
|
61
|
-
identity:
|
|
62
|
-
index:
|
|
63
|
-
index_expression:
|
|
64
|
-
slice:
|
|
65
|
-
key_val_pair:
|
|
66
|
-
literal:
|
|
67
|
-
multi_select_object:
|
|
68
|
-
multi_select_list:
|
|
69
|
-
or_expression:
|
|
70
|
-
and_expression:
|
|
71
|
-
not_expression:
|
|
72
|
-
pipe:
|
|
73
|
-
projection:
|
|
74
|
-
value_projection:
|
|
40
|
+
subexpression: this.#visitSubexpressionOrIndexExpressionOrPipe,
|
|
41
|
+
field: this.#visitField,
|
|
42
|
+
comparator: this.#visitComparator,
|
|
43
|
+
current: this.#visitCurrent,
|
|
44
|
+
expref: this.#visitExpref,
|
|
45
|
+
function_expression: this.#visitFunctionExpression,
|
|
46
|
+
filter_projection: this.#visitFilterProjection,
|
|
47
|
+
flatten: this.#visitFlatten,
|
|
48
|
+
identity: this.#visitIdentity,
|
|
49
|
+
index: this.#visitIndex,
|
|
50
|
+
index_expression: this.#visitSubexpressionOrIndexExpressionOrPipe,
|
|
51
|
+
slice: this.#visitSlice,
|
|
52
|
+
key_val_pair: this.#visitKeyValPair,
|
|
53
|
+
literal: this.#visitLiteral,
|
|
54
|
+
multi_select_object: this.#visitMultiSelectObject,
|
|
55
|
+
multi_select_list: this.#visitMultiSelectList,
|
|
56
|
+
or_expression: this.#visitOrExpression,
|
|
57
|
+
and_expression: this.#visitAndExpression,
|
|
58
|
+
not_expression: this.#visitNotExpression,
|
|
59
|
+
pipe: this.#visitSubexpressionOrIndexExpressionOrPipe,
|
|
60
|
+
projection: this.#visitProjection,
|
|
61
|
+
value_projection: this.#visitValueProjection,
|
|
75
62
|
};
|
|
76
63
|
const visitMethod = visitMethods[nodeType];
|
|
77
64
|
if (visitMethod) {
|
|
@@ -81,230 +68,374 @@ class TreeInterpreter {
|
|
|
81
68
|
throw new JMESPathError(`Not Implemented: Invalid node type: ${node.type}`);
|
|
82
69
|
}
|
|
83
70
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Visit a subexpression, index expression, or pipe node.
|
|
73
|
+
*
|
|
74
|
+
* This method is shared between subexpression, index expression, and pipe
|
|
75
|
+
* since they all behave the same way in the context of an expression.
|
|
76
|
+
*
|
|
77
|
+
* They all visit their children and return the result of the last child.
|
|
78
|
+
*
|
|
79
|
+
* @param node The node to visit.
|
|
80
|
+
* @param value The current value to visit.
|
|
81
|
+
*/
|
|
82
|
+
#visitSubexpressionOrIndexExpressionOrPipe(node, value) {
|
|
83
|
+
let result = value;
|
|
84
|
+
for (const child of node.children) {
|
|
85
|
+
result = this.visit(child, result);
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
98
88
|
}
|
|
99
|
-
|
|
100
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Visit a field node.
|
|
91
|
+
*
|
|
92
|
+
* @param node The field node to visit.
|
|
93
|
+
* @param value The current value to visit.
|
|
94
|
+
*/
|
|
95
|
+
#visitField(node, value) {
|
|
96
|
+
if (!node.value)
|
|
97
|
+
return null;
|
|
98
|
+
if (isRecord(value) &&
|
|
99
|
+
typeof node.value === 'string' &&
|
|
100
|
+
node.value in value) {
|
|
101
|
+
return value[node.value];
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
101
106
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// types will return null.
|
|
118
|
-
if (comparator === 'lt') {
|
|
119
|
-
return left < right;
|
|
120
|
-
}
|
|
121
|
-
else if (comparator === 'lte') {
|
|
122
|
-
return left <= right;
|
|
107
|
+
/**
|
|
108
|
+
* Visit a comparator node.
|
|
109
|
+
*
|
|
110
|
+
* @param node The comparator node to visit.
|
|
111
|
+
* @param value The current value to visit.
|
|
112
|
+
*/
|
|
113
|
+
#visitComparator(node, value) {
|
|
114
|
+
const comparator = node.value;
|
|
115
|
+
const left = this.visit(node.children[0], value);
|
|
116
|
+
const right = this.visit(node.children[1], value);
|
|
117
|
+
if (typeof comparator === 'string' &&
|
|
118
|
+
['eq', 'ne', 'gt', 'gte', 'lt', 'lte'].includes(comparator)) {
|
|
119
|
+
// Common cases: comparator is == or !=
|
|
120
|
+
if (comparator === 'eq') {
|
|
121
|
+
return isStrictEqual(left, right);
|
|
123
122
|
}
|
|
124
|
-
else if (comparator === '
|
|
125
|
-
return left
|
|
123
|
+
else if (comparator === 'ne') {
|
|
124
|
+
return !isStrictEqual(left, right);
|
|
126
125
|
}
|
|
127
|
-
else {
|
|
128
|
-
|
|
126
|
+
else if (typeof left === 'number' && typeof right === 'number') {
|
|
127
|
+
// Ordering operators only work on numbers. Evaluating them on other
|
|
128
|
+
// types will return null.
|
|
129
|
+
if (comparator === 'lt') {
|
|
130
|
+
return left < right;
|
|
131
|
+
}
|
|
132
|
+
else if (comparator === 'lte') {
|
|
133
|
+
return left <= right;
|
|
134
|
+
}
|
|
135
|
+
else if (comparator === 'gt') {
|
|
136
|
+
return left > right;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
return left >= right;
|
|
140
|
+
}
|
|
129
141
|
}
|
|
130
142
|
}
|
|
143
|
+
else {
|
|
144
|
+
throw new JMESPathError(`Invalid comparator: ${comparator}`);
|
|
145
|
+
}
|
|
131
146
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const args = [];
|
|
141
|
-
for (const child of node.children) {
|
|
142
|
-
args.push(this.visit(child, value));
|
|
143
|
-
}
|
|
144
|
-
// check that method name is a string
|
|
145
|
-
if (typeof node.value !== 'string') {
|
|
146
|
-
throw new JMESPathError(`Function name must be a string, got ${node.value}`);
|
|
147
|
-
}
|
|
148
|
-
// convert snake_case to camelCase
|
|
149
|
-
const normalizedFunctionName = node.value.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
|
150
|
-
// capitalize first letter & add `func` prefix
|
|
151
|
-
const funcName = `func${normalizedFunctionName.charAt(0).toUpperCase() +
|
|
152
|
-
normalizedFunctionName.slice(1)}`;
|
|
153
|
-
if (!__classPrivateFieldGet(this, _TreeInterpreter_functions, "f").methods.has(funcName)) {
|
|
154
|
-
throw new UnknownFunctionError(node.value);
|
|
147
|
+
/**
|
|
148
|
+
* Visit a current node.
|
|
149
|
+
*
|
|
150
|
+
* @param node The current node to visit.
|
|
151
|
+
* @param value The current value to visit.
|
|
152
|
+
*/
|
|
153
|
+
#visitCurrent(_node, value) {
|
|
154
|
+
return value;
|
|
155
155
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return
|
|
156
|
+
/**
|
|
157
|
+
* Visit an expref node.
|
|
158
|
+
*
|
|
159
|
+
* @param node The expref node to visit.
|
|
160
|
+
* @param value The current value to visit.
|
|
161
|
+
*/
|
|
162
|
+
#visitExpref(node, _value) {
|
|
163
|
+
return new Expression(node.children[0], this);
|
|
164
164
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Visit a function expression node.
|
|
167
|
+
*
|
|
168
|
+
* @param node The function expression node to visit.
|
|
169
|
+
* @param value The current value to visit.
|
|
170
|
+
*/
|
|
171
|
+
#visitFunctionExpression(node, value) {
|
|
172
|
+
const args = [];
|
|
173
|
+
for (const child of node.children) {
|
|
174
|
+
args.push(this.visit(child, value));
|
|
171
175
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
176
|
+
// check that method name is a string
|
|
177
|
+
if (typeof node.value !== 'string') {
|
|
178
|
+
throw new JMESPathError(`Function name must be a string, got ${node.value}`);
|
|
179
|
+
}
|
|
180
|
+
// convert snake_case to camelCase
|
|
181
|
+
const normalizedFunctionName = node.value.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
|
182
|
+
// capitalize first letter & add `func` prefix
|
|
183
|
+
const funcName = `func${normalizedFunctionName.charAt(0).toUpperCase() +
|
|
184
|
+
normalizedFunctionName.slice(1)}`;
|
|
185
|
+
if (!this.#functions.methods.has(funcName)) {
|
|
186
|
+
throw new UnknownFunctionError(node.value);
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
// We know that methodName is a key of this.#functions, but TypeScript
|
|
190
|
+
// doesn't know that, so we have to use @ts-ignore to tell it that it's
|
|
191
|
+
// okay. We could use a type assertion like `as keyof Functions`, but
|
|
192
|
+
// we also want to keep the args generic, so for now we'll just ignore it.
|
|
193
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
194
|
+
// @ts-ignore-next-line
|
|
195
|
+
return this.#functions[funcName](args);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (error instanceof JMESPathTypeError ||
|
|
199
|
+
error instanceof VariadicArityError ||
|
|
200
|
+
error instanceof ArityError) {
|
|
201
|
+
error.setEvaluatedFunctionName(node.value);
|
|
202
|
+
throw error;
|
|
185
203
|
}
|
|
186
204
|
}
|
|
187
205
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (Array.isArray(
|
|
197
|
-
|
|
206
|
+
/**
|
|
207
|
+
* Visit a filter projection node.
|
|
208
|
+
*
|
|
209
|
+
* @param node The filter projection node to visit.
|
|
210
|
+
* @param value The current value to visit.
|
|
211
|
+
*/
|
|
212
|
+
#visitFilterProjection(node, value) {
|
|
213
|
+
const base = this.visit(node.children[0], value);
|
|
214
|
+
if (!Array.isArray(base)) {
|
|
215
|
+
return null;
|
|
198
216
|
}
|
|
199
|
-
|
|
200
|
-
|
|
217
|
+
const comparatorNode = node.children[2];
|
|
218
|
+
const collected = [];
|
|
219
|
+
for (const item of base) {
|
|
220
|
+
if (isTruthy(this.visit(comparatorNode, item))) {
|
|
221
|
+
const current = this.visit(node.children[1], item);
|
|
222
|
+
if (current !== null) {
|
|
223
|
+
collected.push(current);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
201
226
|
}
|
|
227
|
+
return collected;
|
|
202
228
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Visit a flatten node.
|
|
231
|
+
*
|
|
232
|
+
* @param node The flatten node to visit.
|
|
233
|
+
* @param value The current value to visit.
|
|
234
|
+
*/
|
|
235
|
+
#visitFlatten(node, value) {
|
|
236
|
+
const base = this.visit(node.children[0], value);
|
|
237
|
+
if (!Array.isArray(base)) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
const mergedList = [];
|
|
241
|
+
for (const item of base) {
|
|
242
|
+
if (Array.isArray(item)) {
|
|
243
|
+
mergedList.push(...item);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
mergedList.push(item);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return mergedList;
|
|
214
250
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Visit an identity node.
|
|
253
|
+
*
|
|
254
|
+
* @param node The identity node to visit.
|
|
255
|
+
* @param value The current value to visit.
|
|
256
|
+
*/
|
|
257
|
+
#visitIdentity(_node, value) {
|
|
258
|
+
return value;
|
|
219
259
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Visit an index node.
|
|
262
|
+
*
|
|
263
|
+
* @param node The index node to visit.
|
|
264
|
+
* @param value The current value to visit.
|
|
265
|
+
*/
|
|
266
|
+
#visitIndex(node, value) {
|
|
267
|
+
if (!Array.isArray(value)) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
// The Python implementation doesn't support string indexing
|
|
271
|
+
// even though we could, so we won't either for now.
|
|
272
|
+
if (typeof node.value !== 'number') {
|
|
273
|
+
throw new JMESPathError(`Invalid index: ${node.value}`);
|
|
274
|
+
}
|
|
275
|
+
const index = node.value < 0 ? value.length + node.value : node.value;
|
|
276
|
+
const found = value[index];
|
|
277
|
+
if (found === undefined) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
return found;
|
|
225
281
|
}
|
|
226
|
-
|
|
227
|
-
|
|
282
|
+
/**
|
|
283
|
+
* Visit a slice node.
|
|
284
|
+
*
|
|
285
|
+
* @param node The slice node to visit.
|
|
286
|
+
* @param value The current value to visit.
|
|
287
|
+
*/
|
|
288
|
+
#visitSlice(node, value) {
|
|
289
|
+
const step = isIntegerNumber(node.children[2]) ? node.children[2] : 1;
|
|
290
|
+
if (step === 0) {
|
|
291
|
+
throw new Error('Invalid slice, step cannot be 0');
|
|
292
|
+
}
|
|
293
|
+
if (!Array.isArray(value)) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
if (value.length === 0) {
|
|
297
|
+
return [];
|
|
298
|
+
}
|
|
299
|
+
return sliceArray({
|
|
300
|
+
array: value,
|
|
301
|
+
start: node.children[0],
|
|
302
|
+
end: node.children[1],
|
|
303
|
+
step,
|
|
304
|
+
});
|
|
228
305
|
}
|
|
229
|
-
|
|
230
|
-
|
|
306
|
+
/**
|
|
307
|
+
* Visit a key-value pair node.
|
|
308
|
+
*
|
|
309
|
+
* @param node The key-value pair node to visit.
|
|
310
|
+
* @param value The current value to visit.
|
|
311
|
+
*/
|
|
312
|
+
#visitKeyValPair(node, value) {
|
|
313
|
+
return this.visit(node.children[0], value);
|
|
231
314
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}, _TreeInterpreter_visitLiteral = function _TreeInterpreter_visitLiteral(node, _value) {
|
|
241
|
-
return node.value;
|
|
242
|
-
}, _TreeInterpreter_visitMultiSelectObject = function _TreeInterpreter_visitMultiSelectObject(node, value) {
|
|
243
|
-
if (Object.is(value, null)) {
|
|
244
|
-
return null;
|
|
315
|
+
/**
|
|
316
|
+
* Visit a literal node.
|
|
317
|
+
*
|
|
318
|
+
* @param node The literal node to visit.
|
|
319
|
+
* @param value The current value to visit.
|
|
320
|
+
*/
|
|
321
|
+
#visitLiteral(node, _value) {
|
|
322
|
+
return node.value;
|
|
245
323
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Visit a multi-select object node.
|
|
326
|
+
*
|
|
327
|
+
* @param node The multi-select object node to visit.
|
|
328
|
+
* @param value The current value to visit.
|
|
329
|
+
*/
|
|
330
|
+
#visitMultiSelectObject(node, value) {
|
|
331
|
+
if (Object.is(value, null)) {
|
|
332
|
+
return null;
|
|
250
333
|
}
|
|
334
|
+
const collected = {};
|
|
335
|
+
for (const child of node.children) {
|
|
336
|
+
if (typeof child.value === 'string') {
|
|
337
|
+
collected[child.value] = this.visit(child, value);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return collected;
|
|
251
341
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Visit a multi-select list node.
|
|
344
|
+
*
|
|
345
|
+
* @param node The multi-select list node to visit.
|
|
346
|
+
* @param value The current value to visit.
|
|
347
|
+
*/
|
|
348
|
+
#visitMultiSelectList(node, value) {
|
|
349
|
+
if (Object.is(value, null)) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const collected = [];
|
|
353
|
+
for (const child of node.children) {
|
|
354
|
+
collected.push(this.visit(child, value));
|
|
355
|
+
}
|
|
356
|
+
return collected;
|
|
266
357
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
358
|
+
/**
|
|
359
|
+
* Visit an or expression node.
|
|
360
|
+
*
|
|
361
|
+
* @param node The or expression node to visit.
|
|
362
|
+
* @param value The current value to visit.
|
|
363
|
+
*/
|
|
364
|
+
#visitOrExpression(node, value) {
|
|
365
|
+
const matched = this.visit(node.children[0], value);
|
|
366
|
+
if (!isTruthy(matched)) {
|
|
367
|
+
return this.visit(node.children[1], value);
|
|
368
|
+
}
|
|
271
369
|
return matched;
|
|
272
370
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return null;
|
|
371
|
+
/**
|
|
372
|
+
* Visit an and expression node.
|
|
373
|
+
*
|
|
374
|
+
* @param node The and expression node to visit.
|
|
375
|
+
* @param value The current value to visit.
|
|
376
|
+
*/
|
|
377
|
+
#visitAndExpression(node, value) {
|
|
378
|
+
const matched = this.visit(node.children[0], value);
|
|
379
|
+
if (!isTruthy(matched)) {
|
|
380
|
+
return matched;
|
|
381
|
+
}
|
|
382
|
+
return this.visit(node.children[1], value);
|
|
286
383
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Visit a not expression node.
|
|
386
|
+
*
|
|
387
|
+
* @param node The not expression node to visit.
|
|
388
|
+
* @param value The current value to visit.
|
|
389
|
+
*/
|
|
390
|
+
#visitNotExpression(node, value) {
|
|
391
|
+
const originalResult = this.visit(node.children[0], value);
|
|
392
|
+
if (typeof originalResult === 'number' && originalResult === 0) {
|
|
393
|
+
// Special case for 0, !0 should be false, not true.
|
|
394
|
+
// 0 is not a special cased integer in jmespath.
|
|
395
|
+
return false;
|
|
292
396
|
}
|
|
397
|
+
return !isTruthy(originalResult);
|
|
293
398
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
399
|
+
/**
|
|
400
|
+
* Visit a projection node.
|
|
401
|
+
*
|
|
402
|
+
* @param node The projection node to visit.
|
|
403
|
+
* @param value The current value to visit.
|
|
404
|
+
*/
|
|
405
|
+
#visitProjection(node, value) {
|
|
406
|
+
const base = this.visit(node.children[0], value);
|
|
407
|
+
if (!Array.isArray(base)) {
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
const collected = [];
|
|
411
|
+
for (const item of base) {
|
|
412
|
+
const current = this.visit(node.children[1], item);
|
|
413
|
+
if (current !== null) {
|
|
414
|
+
collected.push(current);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return collected;
|
|
299
418
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Visit a value projection node.
|
|
421
|
+
*
|
|
422
|
+
* @param node The value projection node to visit.
|
|
423
|
+
* @param value The current value to visit.
|
|
424
|
+
*/
|
|
425
|
+
#visitValueProjection(node, value) {
|
|
426
|
+
const base = this.visit(node.children[0], value);
|
|
427
|
+
if (!isRecord(base)) {
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
const values = Object.values(base);
|
|
431
|
+
const collected = [];
|
|
432
|
+
for (const item of values) {
|
|
433
|
+
const current = this.visit(node.children[1], item);
|
|
434
|
+
if (current !== null) {
|
|
435
|
+
collected.push(current);
|
|
436
|
+
}
|
|
306
437
|
}
|
|
438
|
+
return collected;
|
|
307
439
|
}
|
|
308
|
-
|
|
309
|
-
};
|
|
440
|
+
}
|
|
310
441
|
export { TreeInterpreter };
|