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