@aws-lambda-powertools/jmespath 0.0.0-alpha.0 → 2.1.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/LICENSE +14 -0
- package/README.md +214 -5
- package/lib/cjs/Expression.d.ts +19 -0
- package/lib/cjs/Expression.d.ts.map +1 -0
- package/lib/cjs/Expression.js +38 -0
- package/lib/cjs/Functions.d.ts +262 -0
- package/lib/cjs/Functions.d.ts.map +1 -0
- package/lib/cjs/Functions.js +619 -0
- package/lib/cjs/Lexer.d.ts +19 -0
- package/lib/cjs/Lexer.d.ts.map +1 -0
- package/lib/cjs/Lexer.js +271 -0
- package/lib/cjs/ParsedResult.d.ts +15 -0
- package/lib/cjs/ParsedResult.d.ts.map +1 -0
- package/lib/cjs/ParsedResult.js +33 -0
- package/lib/cjs/Parser.d.ts +35 -0
- package/lib/cjs/Parser.d.ts.map +1 -0
- package/lib/cjs/Parser.js +506 -0
- package/lib/cjs/PowertoolsFunctions.d.ts +36 -0
- package/lib/cjs/PowertoolsFunctions.d.ts.map +1 -0
- package/lib/cjs/PowertoolsFunctions.js +69 -0
- package/lib/cjs/TreeInterpreter.d.ts +28 -0
- package/lib/cjs/TreeInterpreter.d.ts.map +1 -0
- package/lib/cjs/TreeInterpreter.js +313 -0
- package/lib/cjs/ast.d.ts +185 -0
- package/lib/cjs/ast.d.ts.map +1 -0
- package/lib/cjs/ast.js +275 -0
- package/lib/cjs/constants.d.ts +60 -0
- package/lib/cjs/constants.d.ts.map +1 -0
- package/lib/cjs/constants.js +96 -0
- package/lib/cjs/envelopes.d.ts +69 -0
- package/lib/cjs/envelopes.d.ts.map +1 -0
- package/lib/cjs/envelopes.js +90 -0
- package/lib/cjs/errors.d.ts +183 -0
- package/lib/cjs/errors.d.ts.map +1 -0
- package/lib/cjs/errors.js +206 -0
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.d.ts.map +1 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/search.d.ts +50 -0
- package/lib/cjs/search.d.ts.map +1 -0
- package/lib/cjs/search.js +55 -0
- package/lib/cjs/types.d.ts +84 -0
- package/lib/cjs/types.d.ts.map +1 -0
- package/lib/cjs/types.js +2 -0
- package/lib/cjs/utils.d.ts +87 -0
- package/lib/cjs/utils.d.ts.map +1 -0
- package/lib/cjs/utils.js +282 -0
- package/lib/esm/Expression.d.ts +19 -0
- package/lib/esm/Expression.d.ts.map +1 -0
- package/lib/esm/Expression.js +35 -0
- package/lib/esm/Functions.d.ts +262 -0
- package/lib/esm/Functions.d.ts.map +1 -0
- package/lib/esm/Functions.js +616 -0
- package/lib/esm/Lexer.d.ts +19 -0
- package/lib/esm/Lexer.d.ts.map +1 -0
- package/lib/esm/Lexer.js +268 -0
- package/lib/esm/ParsedResult.d.ts +15 -0
- package/lib/esm/ParsedResult.d.ts.map +1 -0
- package/lib/esm/ParsedResult.js +30 -0
- package/lib/esm/Parser.d.ts +35 -0
- package/lib/esm/Parser.d.ts.map +1 -0
- package/lib/esm/Parser.js +503 -0
- package/lib/esm/PowertoolsFunctions.d.ts +36 -0
- package/lib/esm/PowertoolsFunctions.d.ts.map +1 -0
- package/lib/esm/PowertoolsFunctions.js +66 -0
- package/lib/esm/TreeInterpreter.d.ts +28 -0
- package/lib/esm/TreeInterpreter.d.ts.map +1 -0
- package/lib/esm/TreeInterpreter.js +310 -0
- package/lib/esm/ast.d.ts +185 -0
- package/lib/esm/ast.d.ts.map +1 -0
- package/lib/esm/ast.js +251 -0
- package/lib/esm/constants.d.ts +60 -0
- package/lib/esm/constants.d.ts.map +1 -0
- package/lib/esm/constants.js +88 -0
- package/lib/esm/envelopes.d.ts +69 -0
- package/lib/esm/envelopes.d.ts.map +1 -0
- package/lib/esm/envelopes.js +74 -0
- package/lib/esm/errors.d.ts +183 -0
- package/lib/esm/errors.d.ts.map +1 -0
- package/lib/esm/errors.js +195 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.d.ts.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/search.d.ts +50 -0
- package/lib/esm/search.d.ts.map +1 -0
- package/lib/esm/search.js +52 -0
- package/lib/esm/types.d.ts +84 -0
- package/lib/esm/types.d.ts.map +1 -0
- package/lib/esm/types.js +1 -0
- package/lib/esm/utils.d.ts +87 -0
- package/lib/esm/utils.d.ts.map +1 -0
- package/lib/esm/utils.js +275 -0
- package/package.json +51 -12
- package/lib/index.d.ts +0 -0
- package/lib/index.js +0 -4
|
@@ -0,0 +1,310 @@
|
|
|
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
|
+
import { isIntegerNumber, isRecord, isStrictEqual, } from '@aws-lambda-powertools/commons/typeutils';
|
|
14
|
+
import { ArityError, JMESPathError, JMESPathTypeError, UnknownFunctionError, VariadicArityError, } from './errors.js';
|
|
15
|
+
import { Expression } from './Expression.js';
|
|
16
|
+
import { Functions } from './Functions.js';
|
|
17
|
+
import { isTruthy, sliceArray } from './utils.js';
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* A tree interpreter for JMESPath ASTs.
|
|
21
|
+
*
|
|
22
|
+
* The tree interpreter is responsible for visiting nodes in the AST and
|
|
23
|
+
* evaluating them to produce a result.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
class TreeInterpreter {
|
|
28
|
+
/**
|
|
29
|
+
* @param options The options to use for the interpreter.
|
|
30
|
+
*/
|
|
31
|
+
constructor(options) {
|
|
32
|
+
_TreeInterpreter_instances.add(this);
|
|
33
|
+
_TreeInterpreter_functions.set(this, void 0);
|
|
34
|
+
if (options?.customFunctions) {
|
|
35
|
+
__classPrivateFieldSet(this, _TreeInterpreter_functions, options.customFunctions, "f");
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
__classPrivateFieldSet(this, _TreeInterpreter_functions, new Functions(), "f");
|
|
39
|
+
}
|
|
40
|
+
__classPrivateFieldGet(this, _TreeInterpreter_functions, "f").introspectMethods();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Visit a node in the AST.
|
|
44
|
+
*
|
|
45
|
+
* The function will call the appropriate method to visit the node based on its type.
|
|
46
|
+
*
|
|
47
|
+
* @param node The node to visit.
|
|
48
|
+
* @param value The current value to visit.
|
|
49
|
+
*/
|
|
50
|
+
visit(node, value) {
|
|
51
|
+
const nodeType = node.type;
|
|
52
|
+
const visitMethods = {
|
|
53
|
+
subexpression: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitSubexpressionOrIndexExpressionOrPipe),
|
|
54
|
+
field: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitField),
|
|
55
|
+
comparator: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitComparator),
|
|
56
|
+
current: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitCurrent),
|
|
57
|
+
expref: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitExpref),
|
|
58
|
+
function_expression: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitFunctionExpression),
|
|
59
|
+
filter_projection: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitFilterProjection),
|
|
60
|
+
flatten: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitFlatten),
|
|
61
|
+
identity: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitIdentity),
|
|
62
|
+
index: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitIndex),
|
|
63
|
+
index_expression: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitSubexpressionOrIndexExpressionOrPipe),
|
|
64
|
+
slice: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitSlice),
|
|
65
|
+
key_val_pair: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitKeyValPair),
|
|
66
|
+
literal: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitLiteral),
|
|
67
|
+
multi_select_object: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitMultiSelectObject),
|
|
68
|
+
multi_select_list: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitMultiSelectList),
|
|
69
|
+
or_expression: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitOrExpression),
|
|
70
|
+
and_expression: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitAndExpression),
|
|
71
|
+
not_expression: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitNotExpression),
|
|
72
|
+
pipe: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitSubexpressionOrIndexExpressionOrPipe),
|
|
73
|
+
projection: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitProjection),
|
|
74
|
+
value_projection: __classPrivateFieldGet(this, _TreeInterpreter_instances, "m", _TreeInterpreter_visitValueProjection),
|
|
75
|
+
};
|
|
76
|
+
const visitMethod = visitMethods[nodeType];
|
|
77
|
+
if (visitMethod) {
|
|
78
|
+
return visitMethod.call(this, node, value);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw new JMESPathError(`Not Implemented: Invalid node type: ${node.type}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
_TreeInterpreter_functions = new WeakMap(), _TreeInterpreter_instances = new WeakSet(), _TreeInterpreter_visitSubexpressionOrIndexExpressionOrPipe = function _TreeInterpreter_visitSubexpressionOrIndexExpressionOrPipe(node, value) {
|
|
86
|
+
let result = value;
|
|
87
|
+
for (const child of node.children) {
|
|
88
|
+
result = this.visit(child, result);
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}, _TreeInterpreter_visitField = function _TreeInterpreter_visitField(node, value) {
|
|
92
|
+
if (!node.value)
|
|
93
|
+
return null;
|
|
94
|
+
if (isRecord(value) &&
|
|
95
|
+
typeof node.value === 'string' &&
|
|
96
|
+
node.value in value) {
|
|
97
|
+
return value[node.value];
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}, _TreeInterpreter_visitComparator = function _TreeInterpreter_visitComparator(node, value) {
|
|
103
|
+
const comparator = node.value;
|
|
104
|
+
const left = this.visit(node.children[0], value);
|
|
105
|
+
const right = this.visit(node.children[1], value);
|
|
106
|
+
if (typeof comparator === 'string' &&
|
|
107
|
+
['eq', 'ne', 'gt', 'gte', 'lt', 'lte'].includes(comparator)) {
|
|
108
|
+
// Common cases: comparator is == or !=
|
|
109
|
+
if (comparator === 'eq') {
|
|
110
|
+
return isStrictEqual(left, right);
|
|
111
|
+
}
|
|
112
|
+
else if (comparator === 'ne') {
|
|
113
|
+
return !isStrictEqual(left, right);
|
|
114
|
+
}
|
|
115
|
+
else if (typeof left === 'number' && typeof right === 'number') {
|
|
116
|
+
// Ordering operators only work on numbers. Evaluating them on other
|
|
117
|
+
// types will return null.
|
|
118
|
+
if (comparator === 'lt') {
|
|
119
|
+
return left < right;
|
|
120
|
+
}
|
|
121
|
+
else if (comparator === 'lte') {
|
|
122
|
+
return left <= right;
|
|
123
|
+
}
|
|
124
|
+
else if (comparator === 'gt') {
|
|
125
|
+
return left > right;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
return left >= right;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
throw new JMESPathError(`Invalid comparator: ${comparator}`);
|
|
134
|
+
}
|
|
135
|
+
}, _TreeInterpreter_visitCurrent = function _TreeInterpreter_visitCurrent(_node, value) {
|
|
136
|
+
return value;
|
|
137
|
+
}, _TreeInterpreter_visitExpref = function _TreeInterpreter_visitExpref(node, _value) {
|
|
138
|
+
return new Expression(node.children[0], this);
|
|
139
|
+
}, _TreeInterpreter_visitFunctionExpression = function _TreeInterpreter_visitFunctionExpression(node, value) {
|
|
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);
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
// We know that methodName is a key of this.#functions, but TypeScript
|
|
158
|
+
// doesn't know that, so we have to use @ts-ignore to tell it that it's
|
|
159
|
+
// okay. We could use a type assertion like `as keyof Functions`, but
|
|
160
|
+
// we also want to keep the args generic, so for now we'll just ignore it.
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
162
|
+
// @ts-ignore-next-line
|
|
163
|
+
return __classPrivateFieldGet(this, _TreeInterpreter_functions, "f")[funcName](args);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
if (error instanceof JMESPathTypeError ||
|
|
167
|
+
error instanceof VariadicArityError ||
|
|
168
|
+
error instanceof ArityError) {
|
|
169
|
+
error.setEvaluatedFunctionName(node.value);
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}, _TreeInterpreter_visitFilterProjection = function _TreeInterpreter_visitFilterProjection(node, value) {
|
|
174
|
+
const base = this.visit(node.children[0], value);
|
|
175
|
+
if (!Array.isArray(base)) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
const comparatorNode = node.children[2];
|
|
179
|
+
const collected = [];
|
|
180
|
+
for (const item of base) {
|
|
181
|
+
if (isTruthy(this.visit(comparatorNode, item))) {
|
|
182
|
+
const current = this.visit(node.children[1], item);
|
|
183
|
+
if (current !== null) {
|
|
184
|
+
collected.push(current);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return collected;
|
|
189
|
+
}, _TreeInterpreter_visitFlatten = function _TreeInterpreter_visitFlatten(node, value) {
|
|
190
|
+
const base = this.visit(node.children[0], value);
|
|
191
|
+
if (!Array.isArray(base)) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
const mergedList = [];
|
|
195
|
+
for (const item of base) {
|
|
196
|
+
if (Array.isArray(item)) {
|
|
197
|
+
mergedList.push(...item);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
mergedList.push(item);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return mergedList;
|
|
204
|
+
}, _TreeInterpreter_visitIdentity = function _TreeInterpreter_visitIdentity(_node, value) {
|
|
205
|
+
return value;
|
|
206
|
+
}, _TreeInterpreter_visitIndex = function _TreeInterpreter_visitIndex(node, value) {
|
|
207
|
+
if (!Array.isArray(value)) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
// The Python implementation doesn't support string indexing
|
|
211
|
+
// even though we could, so we won't either for now.
|
|
212
|
+
if (typeof node.value !== 'number') {
|
|
213
|
+
throw new JMESPathError(`Invalid index: ${node.value}`);
|
|
214
|
+
}
|
|
215
|
+
const index = node.value < 0 ? value.length + node.value : node.value;
|
|
216
|
+
const found = value[index];
|
|
217
|
+
if (found === undefined) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
return found;
|
|
221
|
+
}, _TreeInterpreter_visitSlice = function _TreeInterpreter_visitSlice(node, value) {
|
|
222
|
+
const step = isIntegerNumber(node.children[2]) ? node.children[2] : 1;
|
|
223
|
+
if (step === 0) {
|
|
224
|
+
throw new Error('Invalid slice, step cannot be 0');
|
|
225
|
+
}
|
|
226
|
+
if (!Array.isArray(value)) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
if (value.length === 0) {
|
|
230
|
+
return [];
|
|
231
|
+
}
|
|
232
|
+
return sliceArray({
|
|
233
|
+
array: value,
|
|
234
|
+
start: node.children[0],
|
|
235
|
+
end: node.children[1],
|
|
236
|
+
step,
|
|
237
|
+
});
|
|
238
|
+
}, _TreeInterpreter_visitKeyValPair = function _TreeInterpreter_visitKeyValPair(node, value) {
|
|
239
|
+
return this.visit(node.children[0], value);
|
|
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;
|
|
245
|
+
}
|
|
246
|
+
const collected = {};
|
|
247
|
+
for (const child of node.children) {
|
|
248
|
+
if (typeof child.value === 'string') {
|
|
249
|
+
collected[child.value] = this.visit(child, value);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return collected;
|
|
253
|
+
}, _TreeInterpreter_visitMultiSelectList = function _TreeInterpreter_visitMultiSelectList(node, value) {
|
|
254
|
+
if (Object.is(value, null)) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
const collected = [];
|
|
258
|
+
for (const child of node.children) {
|
|
259
|
+
collected.push(this.visit(child, value));
|
|
260
|
+
}
|
|
261
|
+
return collected;
|
|
262
|
+
}, _TreeInterpreter_visitOrExpression = function _TreeInterpreter_visitOrExpression(node, value) {
|
|
263
|
+
const matched = this.visit(node.children[0], value);
|
|
264
|
+
if (!isTruthy(matched)) {
|
|
265
|
+
return this.visit(node.children[1], value);
|
|
266
|
+
}
|
|
267
|
+
return matched;
|
|
268
|
+
}, _TreeInterpreter_visitAndExpression = function _TreeInterpreter_visitAndExpression(node, value) {
|
|
269
|
+
const matched = this.visit(node.children[0], value);
|
|
270
|
+
if (!isTruthy(matched)) {
|
|
271
|
+
return matched;
|
|
272
|
+
}
|
|
273
|
+
return this.visit(node.children[1], value);
|
|
274
|
+
}, _TreeInterpreter_visitNotExpression = function _TreeInterpreter_visitNotExpression(node, value) {
|
|
275
|
+
const originalResult = this.visit(node.children[0], value);
|
|
276
|
+
if (typeof originalResult === 'number' && originalResult === 0) {
|
|
277
|
+
// Special case for 0, !0 should be false, not true.
|
|
278
|
+
// 0 is not a special cased integer in jmespath.
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
return !isTruthy(originalResult);
|
|
282
|
+
}, _TreeInterpreter_visitProjection = function _TreeInterpreter_visitProjection(node, value) {
|
|
283
|
+
const base = this.visit(node.children[0], value);
|
|
284
|
+
if (!Array.isArray(base)) {
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
const collected = [];
|
|
288
|
+
for (const item of base) {
|
|
289
|
+
const current = this.visit(node.children[1], item);
|
|
290
|
+
if (current !== null) {
|
|
291
|
+
collected.push(current);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return collected;
|
|
295
|
+
}, _TreeInterpreter_visitValueProjection = function _TreeInterpreter_visitValueProjection(node, value) {
|
|
296
|
+
const base = this.visit(node.children[0], value);
|
|
297
|
+
if (!isRecord(base)) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const values = Object.values(base);
|
|
301
|
+
const collected = [];
|
|
302
|
+
for (const item of values) {
|
|
303
|
+
const current = this.visit(node.children[1], item);
|
|
304
|
+
if (current !== null) {
|
|
305
|
+
collected.push(current);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return collected;
|
|
309
|
+
};
|
|
310
|
+
export { TreeInterpreter };
|
package/lib/esm/ast.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { Node } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* AST node representing a comparator expression.
|
|
5
|
+
*
|
|
6
|
+
* A comparator expression is a binary expression that compares two values.
|
|
7
|
+
*
|
|
8
|
+
* @param name The name of the comparator
|
|
9
|
+
* @param first The left-hand side of the comparator
|
|
10
|
+
* @param second The right-hand side of the comparator
|
|
11
|
+
*/
|
|
12
|
+
declare const comparator: (name: string, first: Node, second: Node) => Node;
|
|
13
|
+
/**
|
|
14
|
+
* AST node representing the current node.
|
|
15
|
+
*
|
|
16
|
+
* The current node is a reference to the current value being processed.
|
|
17
|
+
* In JMESPath, the current node is represented by the `@` symbol.
|
|
18
|
+
*/
|
|
19
|
+
declare const currentNode: () => Node;
|
|
20
|
+
/**
|
|
21
|
+
* AST node representing an expression reference.
|
|
22
|
+
*
|
|
23
|
+
* An expression reference is a reference to another expression.
|
|
24
|
+
* In JMESPath, an expression reference is represented by the `&` symbol.
|
|
25
|
+
*
|
|
26
|
+
* @param expression The expression to reference
|
|
27
|
+
*/
|
|
28
|
+
declare const expref: (expression: Node) => Node;
|
|
29
|
+
/**
|
|
30
|
+
* AST node representing a function expression.
|
|
31
|
+
*
|
|
32
|
+
* A function expression is a reference to a function and its arguments.
|
|
33
|
+
* The JMESPath specification defines a set of built-in functions that can
|
|
34
|
+
* be used in expressions like `length(@)`, `map(@, &foo)`, etc.
|
|
35
|
+
*
|
|
36
|
+
* Custom functions can be added by extending the `Functions` class.
|
|
37
|
+
*
|
|
38
|
+
* @param name The name of the function
|
|
39
|
+
* @param args The arguments to the function
|
|
40
|
+
*/
|
|
41
|
+
declare const functionExpression: (name: string, args: Node[]) => Node;
|
|
42
|
+
/**
|
|
43
|
+
* AST node representing a field reference.
|
|
44
|
+
*
|
|
45
|
+
* A field reference is a reference to a field in an object.
|
|
46
|
+
*/
|
|
47
|
+
declare const field: (name: JSONValue) => Node;
|
|
48
|
+
/**
|
|
49
|
+
* AST node representing a filter projection.
|
|
50
|
+
*
|
|
51
|
+
* A filter projection is a binary expression that filters the left-hand side
|
|
52
|
+
* based on the right-hand side.
|
|
53
|
+
*
|
|
54
|
+
* In JMESPath, a filter projection is represented by the `[]` operator.
|
|
55
|
+
* For example, `people[?age > 18]` filters the `people` array based on the
|
|
56
|
+
* `age` field.
|
|
57
|
+
*
|
|
58
|
+
* @param left The left-hand side of the filter projection
|
|
59
|
+
* @param right The right-hand side of the filter projection
|
|
60
|
+
* @param comparator The comparator to use for the filter
|
|
61
|
+
*/
|
|
62
|
+
declare const filterProjection: (left: Node, right: Node, comparator: Node) => Node;
|
|
63
|
+
/**
|
|
64
|
+
* AST node representing a flatten expression.
|
|
65
|
+
*
|
|
66
|
+
* A flatten expression is a unary expression that flattens an array of arrays
|
|
67
|
+
* into a single array.
|
|
68
|
+
*
|
|
69
|
+
* In JMESPath, a flatten expression is represented by the `[]` operator.
|
|
70
|
+
* For example, `people[].name` flattens the `people` array and returns the
|
|
71
|
+
* `name` field of each object in the array.
|
|
72
|
+
*
|
|
73
|
+
* @param node The node to flatten
|
|
74
|
+
*/
|
|
75
|
+
declare const flatten: (node: Node) => Node;
|
|
76
|
+
/**
|
|
77
|
+
* AST node representing an identity expression.
|
|
78
|
+
*/
|
|
79
|
+
declare const identity: () => Node;
|
|
80
|
+
/**
|
|
81
|
+
* AST node representing an index reference.
|
|
82
|
+
*
|
|
83
|
+
* An index reference is a reference to an index in an array.
|
|
84
|
+
* For example, `people[0]` references the first element in the `people` array.
|
|
85
|
+
*
|
|
86
|
+
* @param index The index to reference
|
|
87
|
+
*/
|
|
88
|
+
declare const index: (index: JSONValue) => Node;
|
|
89
|
+
/**
|
|
90
|
+
* AST node representing an index expression.
|
|
91
|
+
*
|
|
92
|
+
* An index expression holds the index and the children of the expression.
|
|
93
|
+
*
|
|
94
|
+
* @param children The children of the index expression
|
|
95
|
+
*/
|
|
96
|
+
declare const indexExpression: (children: Node[]) => Node;
|
|
97
|
+
/**
|
|
98
|
+
* AST node representing a key-value pair.
|
|
99
|
+
*
|
|
100
|
+
* @param keyName The name of the key
|
|
101
|
+
* @param node The value of the key
|
|
102
|
+
*/
|
|
103
|
+
declare const keyValPair: (keyName: JSONValue, node: Node) => Node;
|
|
104
|
+
/**
|
|
105
|
+
* AST node representing a literal value.
|
|
106
|
+
*
|
|
107
|
+
* A literal value is a value that is not a reference to another node.
|
|
108
|
+
*
|
|
109
|
+
* @param literalValue The value of the literal
|
|
110
|
+
*/
|
|
111
|
+
declare const literal: (literalValue: JSONValue) => Node;
|
|
112
|
+
/**
|
|
113
|
+
* AST node representing a multi-select object.
|
|
114
|
+
*
|
|
115
|
+
* A multi-select object is a reference to multiple nodes in an object.
|
|
116
|
+
*
|
|
117
|
+
* @param nodes
|
|
118
|
+
*/
|
|
119
|
+
declare const multiSelectObject: (nodes: Node[]) => Node;
|
|
120
|
+
/**
|
|
121
|
+
* AST node representing a multi-select list.
|
|
122
|
+
*
|
|
123
|
+
* @param nodes
|
|
124
|
+
*/
|
|
125
|
+
declare const multiSelectList: (nodes: Node[]) => Node;
|
|
126
|
+
/**
|
|
127
|
+
* AST node representing an or expression.
|
|
128
|
+
*
|
|
129
|
+
* @param left The left-hand side of the or expression
|
|
130
|
+
* @param right The right-hand side of the or expression
|
|
131
|
+
*/
|
|
132
|
+
declare const orExpression: (left: Node, right: Node) => Node;
|
|
133
|
+
/**
|
|
134
|
+
* AST node representing an and expression.
|
|
135
|
+
*
|
|
136
|
+
* @param left The left-hand side of the and expression
|
|
137
|
+
* @param right The right-hand side of the and expression
|
|
138
|
+
*/
|
|
139
|
+
declare const andExpression: (left: Node, right: Node) => Node;
|
|
140
|
+
/**
|
|
141
|
+
* AST node representing a not expression.
|
|
142
|
+
*
|
|
143
|
+
* @param left The left-hand side of the not expression
|
|
144
|
+
* @param right The right-hand side of the not expression
|
|
145
|
+
*/
|
|
146
|
+
declare const notExpression: (expr: Node) => Node;
|
|
147
|
+
/**
|
|
148
|
+
* AST node representing a pipe expression.
|
|
149
|
+
*
|
|
150
|
+
* @param left The left-hand side of the pipe expression
|
|
151
|
+
* @param right The right-hand side of the pipe expression
|
|
152
|
+
*/
|
|
153
|
+
declare const pipe: (left: Node, right: Node) => Node;
|
|
154
|
+
/**
|
|
155
|
+
* AST node representing a projection.
|
|
156
|
+
*
|
|
157
|
+
* @param left The left-hand side of the projection
|
|
158
|
+
* @param right The right-hand side of the projection
|
|
159
|
+
*/
|
|
160
|
+
declare const projection: (left: Node, right: Node) => Node;
|
|
161
|
+
/**
|
|
162
|
+
* AST node representing a subexpression.
|
|
163
|
+
*
|
|
164
|
+
* @param children The children of the subexpression
|
|
165
|
+
*/
|
|
166
|
+
declare const subexpression: (children: Node[]) => Node;
|
|
167
|
+
/**
|
|
168
|
+
* AST node representing a slice.
|
|
169
|
+
*
|
|
170
|
+
* A slice is a reference to a range of values in an array.
|
|
171
|
+
*
|
|
172
|
+
* @param start The start of the slice
|
|
173
|
+
* @param end The end of the slice
|
|
174
|
+
* @param step The step of the slice
|
|
175
|
+
*/
|
|
176
|
+
declare const slice: (start: JSONValue, end: JSONValue, step: JSONValue) => Node;
|
|
177
|
+
/**
|
|
178
|
+
* AST node representing a value projection.
|
|
179
|
+
*
|
|
180
|
+
* @param left The left-hand side of the value projection
|
|
181
|
+
* @param right The right-hand side of the value projection
|
|
182
|
+
*/
|
|
183
|
+
declare const valueProjection: (left: Node, right: Node) => Node;
|
|
184
|
+
export { andExpression, comparator, currentNode, expref, field, filterProjection, flatten, functionExpression, identity, index, indexExpression, keyValPair, literal, multiSelectObject, multiSelectList, notExpression, orExpression, pipe, projection, slice, subexpression, valueProjection, };
|
|
185
|
+
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../../src/ast.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AACtE,OAAO,KAAK,EAAE,IAAI,EA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