Skip to content

Commit 035c4bb

Browse files
committed
chore: update types to include object
1 parent 061fa41 commit 035c4bb

File tree

6 files changed

+46
-40
lines changed

6 files changed

+46
-40
lines changed

packages/jmespath/src/Expression.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
21
import type { TreeInterpreter } from './TreeInterpreter.js';
3-
import type { Node } from './types.js';
2+
import type { JSONObject, Node } from './types.js';
43

54
/**
65
* Apply a JMESPath expression to a JSON value.
@@ -21,7 +20,7 @@ class Expression {
2120
* @param node The node to visit.
2221
* @returns The result of applying the expression to the value.
2322
*/
24-
public visit(value: JSONValue, node?: Node): JSONValue {
23+
public visit(value: JSONObject, node?: Node): JSONObject {
2524
return this.#interpreter.visit(node ?? this.#expression, value);
2625
}
2726
}

packages/jmespath/src/Functions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { JMESPathTypeError } from './errors.js';
1313
import type {
1414
FunctionSignatureDecorator,
1515
FunctionSignatureOptions,
16+
JSONObject as JSONObjectType,
1617
} from './types.js';
1718
import { arityCheck, typeCheck } from './utils.js';
1819

@@ -176,8 +177,11 @@ class Functions {
176177
@Functions.signature({
177178
argumentsSpecs: [['any'], ['array']],
178179
})
179-
public funcMap(expression: Expression, args: JSONArray): JSONArray {
180-
return args.map((arg: JSONValue) => {
180+
public funcMap(
181+
expression: Expression,
182+
args: JSONArray
183+
): JSONArray | Array<unknown> {
184+
return args.map((arg: JSONObjectType) => {
181185
return expression.visit(arg) || null;
182186
});
183187
}

packages/jmespath/src/ParsedResult.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
21
import { TreeInterpreter } from './TreeInterpreter.js';
32
import {
43
ArityError,
54
JMESPathTypeError,
65
UnknownFunctionError,
76
VariadicArityError,
87
} from './errors.js';
9-
import type { Node, ParsingOptions } from './types.js';
8+
import type { Node, ParsingOptions, JSONObject } from './types.js';
109

1110
class ParsedResult {
1211
public expression: string;
@@ -23,7 +22,7 @@ class ParsedResult {
2322
* @param value The JSON value to search
2423
* @param options The parsing options to use
2524
*/
26-
public search(value: JSONValue, options?: ParsingOptions): unknown {
25+
public search(value: JSONObject, options?: ParsingOptions): unknown {
2726
const interpreter = new TreeInterpreter(options);
2827

2928
try {

packages/jmespath/src/TreeInterpreter.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
21
import {
32
isIntegerNumber,
43
isRecord,
@@ -13,7 +12,7 @@ import {
1312
} from './errors.js';
1413
import { Expression } from './Expression.js';
1514
import { Functions } from './Functions.js';
16-
import type { Node, TreeInterpreterOptions } from './types.js';
15+
import type { Node, TreeInterpreterOptions, JSONObject } from './types.js';
1716
import { isTruthy, sliceArray } from './utils.js';
1817

1918
/**
@@ -47,7 +46,7 @@ class TreeInterpreter {
4746
* @param node The node to visit.
4847
* @param value The current value to visit.
4948
*/
50-
public visit(node: Node, value: JSONValue): JSONValue | undefined {
49+
public visit(node: Node, value: JSONObject): JSONObject | null {
5150
const nodeType = node.type;
5251
if (nodeType === 'subexpression') {
5352
return this.#visitSubexpression(node, value);
@@ -116,7 +115,7 @@ class TreeInterpreter {
116115
* @param node The subexpression node to visit.
117116
* @param value The current value to visit.
118117
*/
119-
#visitSubexpression(node: Node, value: JSONValue): JSONValue {
118+
#visitSubexpression(node: Node, value: JSONObject): JSONObject {
120119
let result = value;
121120
for (const child of node.children) {
122121
result = this.visit(child, result);
@@ -131,14 +130,14 @@ class TreeInterpreter {
131130
* @param node The field node to visit.
132131
* @param value The current value to visit.
133132
*/
134-
#visitField(node: Node, value: JSONValue): JSONValue {
133+
#visitField(node: Node, value: JSONObject): JSONObject {
135134
if (!node.value) return null;
136135
if (
137136
isRecord(value) &&
138137
typeof node.value === 'string' &&
139138
node.value in value
140139
) {
141-
return value[node.value];
140+
return value[node.value] as JSONObject;
142141
} else {
143142
return null;
144143
}
@@ -150,7 +149,7 @@ class TreeInterpreter {
150149
* @param node The comparator node to visit.
151150
* @param value The current value to visit.
152151
*/
153-
#visitComparator(node: Node, value: JSONValue): JSONValue {
152+
#visitComparator(node: Node, value: JSONObject): JSONObject {
154153
const comparator = node.value;
155154
const left = this.visit(node.children[0], value);
156155
const right = this.visit(node.children[1], value);
@@ -187,7 +186,7 @@ class TreeInterpreter {
187186
* @param node The current node to visit.
188187
* @param value The current value to visit.
189188
*/
190-
#visitCurrent(_node: Node, value: JSONValue): JSONValue {
189+
#visitCurrent(_node: Node, value: JSONObject): JSONObject {
191190
return value;
192191
}
193192

@@ -197,7 +196,7 @@ class TreeInterpreter {
197196
* @param node The expref node to visit.
198197
* @param value The current value to visit.
199198
*/
200-
#visitExpref(node: Node, _value: JSONValue): Expression {
199+
#visitExpref(node: Node, _value: JSONObject): Expression {
201200
return new Expression(node.children[0], this);
202201
}
203202

@@ -207,7 +206,7 @@ class TreeInterpreter {
207206
* @param node The function expression node to visit.
208207
* @param value The current value to visit.
209208
*/
210-
#visitFunctionExpression(node: Node, value: JSONValue): JSONValue {
209+
#visitFunctionExpression(node: Node, value: JSONObject): JSONObject {
211210
const args = [];
212211
for (const child of node.children) {
213212
args.push(this.visit(child, value));
@@ -271,7 +270,7 @@ class TreeInterpreter {
271270
* @param node The filter projection node to visit.
272271
* @param value The current value to visit.
273272
*/
274-
#visitFilterProjection(node: Node, value: JSONValue): JSONValue {
273+
#visitFilterProjection(node: Node, value: JSONObject): JSONObject {
275274
const base = this.visit(node.children[0], value);
276275
if (!Array.isArray(base)) {
277276
return null;
@@ -296,7 +295,7 @@ class TreeInterpreter {
296295
* @param node The flatten node to visit.
297296
* @param value The current value to visit.
298297
*/
299-
#visitFlatten(node: Node, value: JSONValue): JSONValue {
298+
#visitFlatten(node: Node, value: JSONObject): JSONObject {
300299
const base = this.visit(node.children[0], value);
301300
if (!Array.isArray(base)) {
302301
return null;
@@ -319,7 +318,7 @@ class TreeInterpreter {
319318
* @param node The identity node to visit.
320319
* @param value The current value to visit.
321320
*/
322-
#visitIdentity(_node: Node, value: JSONValue): JSONValue {
321+
#visitIdentity(_node: Node, value: JSONObject): JSONObject {
323322
return value;
324323
}
325324

@@ -329,7 +328,7 @@ class TreeInterpreter {
329328
* @param node The index node to visit.
330329
* @param value The current value to visit.
331330
*/
332-
#visitIndex(node: Node, value: JSONValue): JSONValue {
331+
#visitIndex(node: Node, value: JSONObject): JSONObject {
333332
if (!Array.isArray(value)) {
334333
return null;
335334
}
@@ -353,7 +352,7 @@ class TreeInterpreter {
353352
* @param node The index expression node to visit.
354353
* @param value The current value to visit.
355354
*/
356-
#visitIndexExpression(node: Node, value: JSONValue): JSONValue {
355+
#visitIndexExpression(node: Node, value: JSONObject): JSONObject {
357356
let result = value;
358357
for (const child of node.children) {
359358
result = this.visit(child, result);
@@ -368,7 +367,7 @@ class TreeInterpreter {
368367
* @param node The slice node to visit.
369368
* @param value The current value to visit.
370369
*/
371-
#visitSlice(node: Node, value: JSONValue): JSONValue {
370+
#visitSlice(node: Node, value: JSONObject): JSONObject {
372371
const step = isIntegerNumber(node.children[2]) ? node.children[2] : 1;
373372
if (step === 0) {
374373
throw new Error('Invalid slice, step cannot be 0');
@@ -394,7 +393,7 @@ class TreeInterpreter {
394393
* @param node The key-value pair node to visit.
395394
* @param value The current value to visit.
396395
*/
397-
#visitKeyValPair(node: Node, value: JSONValue): JSONValue {
396+
#visitKeyValPair(node: Node, value: JSONObject): JSONObject {
398397
return this.visit(node.children[0], value);
399398
}
400399

@@ -404,7 +403,7 @@ class TreeInterpreter {
404403
* @param node The literal node to visit.
405404
* @param value The current value to visit.
406405
*/
407-
#visitLiteral(node: Node, _value: JSONValue): JSONValue {
406+
#visitLiteral(node: Node, _value: JSONObject): JSONObject {
408407
return node.value;
409408
}
410409

@@ -414,11 +413,11 @@ class TreeInterpreter {
414413
* @param node The multi-select object node to visit.
415414
* @param value The current value to visit.
416415
*/
417-
#visitMultiSelectObject(node: Node, value: JSONValue): JSONValue {
416+
#visitMultiSelectObject(node: Node, value: JSONObject): JSONObject {
418417
if (Object.is(value, null)) {
419418
return null;
420419
}
421-
const collected: JSONValue = {};
420+
const collected: Record<string, JSONObject> = {};
422421
for (const child of node.children) {
423422
if (typeof child.value === 'string') {
424423
collected[child.value] = this.visit(child, value);
@@ -434,7 +433,7 @@ class TreeInterpreter {
434433
* @param node The multi-select list node to visit.
435434
* @param value The current value to visit.
436435
*/
437-
#visitMultiSelectList(node: Node, value: JSONValue): JSONValue {
436+
#visitMultiSelectList(node: Node, value: JSONObject): JSONObject {
438437
if (Object.is(value, null)) {
439438
return null;
440439
}
@@ -452,7 +451,7 @@ class TreeInterpreter {
452451
* @param node The or expression node to visit.
453452
* @param value The current value to visit.
454453
*/
455-
#visitOrExpression(node: Node, value: JSONValue): JSONValue {
454+
#visitOrExpression(node: Node, value: JSONObject): JSONObject {
456455
const matched = this.visit(node.children[0], value);
457456
if (!isTruthy(matched)) {
458457
return this.visit(node.children[1], value);
@@ -467,7 +466,7 @@ class TreeInterpreter {
467466
* @param node The and expression node to visit.
468467
* @param value The current value to visit.
469468
*/
470-
#visitAndExpression(node: Node, value: JSONValue): JSONValue {
469+
#visitAndExpression(node: Node, value: JSONObject): JSONObject {
471470
const matched = this.visit(node.children[0], value);
472471
if (!isTruthy(matched)) {
473472
return matched;
@@ -482,7 +481,7 @@ class TreeInterpreter {
482481
* @param node The not expression node to visit.
483482
* @param value The current value to visit.
484483
*/
485-
#visitNotExpression(node: Node, value: JSONValue): JSONValue {
484+
#visitNotExpression(node: Node, value: JSONObject): JSONObject {
486485
const originalResult = this.visit(node.children[0], value);
487486
if (typeof originalResult === 'number' && originalResult === 0) {
488487
// Special case for 0, !0 should be false, not true.
@@ -499,7 +498,7 @@ class TreeInterpreter {
499498
* @param node The pipe node to visit.
500499
* @param value The current value to visit.
501500
*/
502-
#visitPipe(node: Node, value: JSONValue): JSONValue {
501+
#visitPipe(node: Node, value: JSONObject): JSONObject {
503502
let result = value;
504503
for (const child of node.children) {
505504
result = this.visit(child, result);
@@ -514,7 +513,7 @@ class TreeInterpreter {
514513
* @param node The projection node to visit.
515514
* @param value The current value to visit.
516515
*/
517-
#visitProjection(node: Node, value: JSONValue): JSONValue {
516+
#visitProjection(node: Node, value: JSONObject): JSONObject {
518517
const base = this.visit(node.children[0], value);
519518
if (!Array.isArray(base)) {
520519
return null;
@@ -536,12 +535,12 @@ class TreeInterpreter {
536535
* @param node The value projection node to visit.
537536
* @param value The current value to visit.
538537
*/
539-
#visitValueProjection(node: Node, value: JSONValue): JSONValue {
538+
#visitValueProjection(node: Node, value: JSONObject): JSONObject {
540539
const base = this.visit(node.children[0], value);
541540
if (!isRecord(base)) {
542541
return null;
543542
}
544-
const values = Object.values(base);
543+
const values = Object.values(base) as JSONObject[];
545544
const collected = [];
546545
for (const item of values) {
547546
const current = this.visit(node.children[1], item);

packages/jmespath/src/search.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
21
import { Parser } from './Parser.js';
3-
import type { ParsingOptions } from './types.js';
2+
import type { ParsingOptions, JSONObject } from './types.js';
43

54
const parser = new Parser();
65

@@ -52,7 +51,7 @@ const parser = new Parser();
5251
*/
5352
const search = (
5453
expression: string,
55-
data: JSONValue,
54+
data: JSONObject,
5655
options?: ParsingOptions
5756
): unknown => {
5857
return parser.parse(expression).search(data, options);

packages/jmespath/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
1+
import type {
2+
JSONValue,
3+
JSONArray,
4+
} from '@aws-lambda-powertools/commons/types';
25
import type { Functions } from './Functions.js';
36
import { BINDING_POWER } from './constants.js';
47

@@ -91,11 +94,14 @@ type FunctionSignatureOptions = {
9194
variadic?: boolean;
9295
};
9396

97+
type JSONObject = JSONArray | JSONValue | object;
98+
9499
export type {
95100
FunctionSignatureDecorator,
96101
FunctionSignatureOptions,
97102
Node,
98103
ParsingOptions,
99104
Token,
100105
TreeInterpreterOptions,
106+
JSONObject,
101107
};

0 commit comments

Comments
 (0)