-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
json-path.ts
349 lines (305 loc) · 10.2 KB
/
json-path.ts
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
338
339
340
341
342
343
344
345
346
347
348
349
import { captureStackTrace, IResolvable, IResolveContext, Token, Tokenization } from '@aws-cdk/core';
import { IntrinsicParser, IntrinsicExpression } from './intrinstics';
const JSON_PATH_TOKEN_SYMBOL = Symbol.for('@aws-cdk/aws-stepfunctions.JsonPathToken');
export class JsonPathToken implements IResolvable {
public static isJsonPathToken(x: IResolvable): x is JsonPathToken {
return (x as any)[JSON_PATH_TOKEN_SYMBOL] === true;
}
public readonly creationStack: string[];
public displayHint: string;
constructor(public readonly path: string) {
this.creationStack = captureStackTrace();
this.displayHint = path.replace(/^[^a-zA-Z]+/, '');
Object.defineProperty(this, JSON_PATH_TOKEN_SYMBOL, { value: true });
}
public resolve(_ctx: IResolveContext): any {
return this.path;
}
public toString() {
return Token.asString(this, { displayHint: this.displayHint });
}
public toJSON() {
return `<path:${this.path}>`;
}
}
/**
* Deep render a JSON object to expand JSON path fields, updating the key to end in '.$'
*/
export function renderObject(obj: object | undefined): object | undefined {
return recurseObject(obj, {
handleString: renderString,
handleList: renderStringList,
handleNumber: renderNumber,
handleBoolean: renderBoolean,
handleResolvable: renderResolvable,
});
}
/**
* Return all JSON paths that are used in the given structure
*/
export function findReferencedPaths(obj: object | undefined): Set<string> {
const found = new Set<string>();
recurseObject(obj, {
handleString(_key: string, x: string) {
for (const p of findPathsInIntrinsicFunctions(jsonPathString(x))) {
found.add(p);
}
return {};
},
handleList(_key: string, x: string[]) {
for (const p of findPathsInIntrinsicFunctions(jsonPathStringList(x))) {
found.add(p);
}
return {};
},
handleNumber(_key: string, x: number) {
for (const p of findPathsInIntrinsicFunctions(jsonPathNumber(x))) {
found.add(p);
}
return {};
},
handleBoolean(_key: string, _x: boolean) {
return {};
},
handleResolvable(_key: string, x: IResolvable) {
for (const p of findPathsInIntrinsicFunctions(jsonPathFromAny(x))) {
found.add(p);
}
return {};
},
});
return found;
}
/**
* From an expression, return the list of JSON paths referenced in it
*/
function findPathsInIntrinsicFunctions(expression?: string): string[] {
if (!expression) { return []; }
const ret = new Array<string>();
try {
const parsed = new IntrinsicParser(expression).parseTopLevelIntrinsic();
recurse(parsed);
return ret;
} catch (e) {
// Not sure that our parsing is 100% correct. We don't want to break anyone, so
// fall back to legacy behavior if we can't parse this string.
return [expression];
}
function recurse(p: IntrinsicExpression) {
switch (p.type) {
case 'path':
ret.push(p.path);
break;
case 'fncall':
for (const arg of p.arguments) {
recurse(arg);
}
}
}
}
interface FieldHandlers {
handleString(key: string, x: string): {[key: string]: string};
handleList(key: string, x: string[]): {[key: string]: string[] | string };
handleNumber(key: string, x: number): {[key: string]: number | string};
handleBoolean(key: string, x: boolean): {[key: string]: boolean};
handleResolvable(key: string, x: IResolvable): {[key: string]: any};
}
export function recurseObject(obj: object | undefined, handlers: FieldHandlers, visited: object[] = []): object | undefined {
// If the argument received is not actually an object (string, number, boolean, undefined, ...) or null
// just return it as is as there's nothing to be rendered. This should only happen in the original call to
// recurseObject as any recursive calls to it are checking for typeof value === 'object' && value !== null
if (typeof obj !== 'object' || obj === null) {
return obj;
}
// Avoiding infinite recursion
if (visited.includes(obj)) { return {}; }
// Marking current object as visited for the current recursion path
visited.push(obj);
const ret: any = {};
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'string') {
Object.assign(ret, handlers.handleString(key, value));
} else if (typeof value === 'number') {
Object.assign(ret, handlers.handleNumber(key, value));
} else if (Array.isArray(value)) {
Object.assign(ret, recurseArray(key, value, handlers, visited));
} else if (typeof value === 'boolean') {
Object.assign(ret, handlers.handleBoolean(key, value));
} else if (value === null || value === undefined) {
// Nothing
} else if (typeof value === 'object') {
if (Tokenization.isResolvable(value)) {
Object.assign(ret, handlers.handleResolvable(key, value));
} else {
ret[key] = recurseObject(value, handlers, visited);
}
}
}
// Removing from visited after leaving the current recursion path
// Allowing it to be visited again if it's not causing a recursion (circular reference)
visited.pop();
return ret;
}
/**
* Render an array that may or may not contain a string list token
*/
function recurseArray(key: string, arr: any[], handlers: FieldHandlers, visited: object[] = []): {[key: string]: any[] | string} {
if (isStringArray(arr)) {
const path = jsonPathStringList(arr);
if (path !== undefined) {
return handlers.handleList(key, arr);
}
// Fall through to correctly reject encoded strings inside an array.
// They cannot be represented because there is no key to append a '.$' to.
}
return {
[key]: arr.map(value => {
if ((typeof value === 'string' && jsonPathString(value) !== undefined)
|| (typeof value === 'number' && jsonPathNumber(value) !== undefined)
|| (isStringArray(value) && jsonPathStringList(value) !== undefined)) {
throw new Error('Cannot use JsonPath fields in an array, they must be used in objects');
}
if (typeof value === 'object' && value !== null) {
return recurseObject(value, handlers, visited);
}
return value;
}),
};
}
function isStringArray(x: any): x is string[] {
return Array.isArray(x) && x.every(el => typeof el === 'string');
}
/**
* Render a parameter string
*
* If the string value starts with '$.', render it as a path string, otherwise as a direct string.
*/
function renderString(key: string, value: string): {[key: string]: string} {
const path = jsonPathString(value);
if (path !== undefined) {
return { [key + '.$']: path };
} else {
return { [key]: value };
}
}
/**
* Render a resolvable
*
* If we can extract a Path from it, render as a path string, otherwise as itself (will
* be resolved later
*/
function renderResolvable(key: string, value: IResolvable): {[key: string]: any} {
const path = jsonPathFromAny(value);
if (path !== undefined) {
return { [key + '.$']: path };
} else {
return { [key]: value };
}
}
/**
* Render a parameter string list
*
* If the string value starts with '$.', render it as a path string, otherwise as a direct string.
*/
function renderStringList(key: string, value: string[]): {[key: string]: string[] | string} {
const path = jsonPathStringList(value);
if (path !== undefined) {
return { [key + '.$']: path };
} else {
return { [key]: value };
}
}
/**
* Render a parameter number
*
* If the string value starts with '$.', render it as a path string, otherwise as a direct string.
*/
function renderNumber(key: string, value: number): {[key: string]: number | string} {
const path = jsonPathNumber(value);
if (path !== undefined) {
return { [key + '.$']: path };
} else {
return { [key]: value };
}
}
/**
* Render a parameter boolean
*/
function renderBoolean(key: string, value: boolean): {[key: string]: boolean} {
return { [key]: value };
}
/**
* If the indicated string is an encoded JSON path, return the path
*
* Otherwise return undefined.
*/
export function jsonPathString(x: string): string | undefined {
const fragments = Tokenization.reverseString(x);
const jsonPathTokens = fragments.tokens.filter(JsonPathToken.isJsonPathToken);
if (jsonPathTokens.length > 0 && fragments.length > 1) {
throw new Error(`Field references must be the entire string, cannot concatenate them (found '${x}')`);
}
if (jsonPathTokens.length > 0) {
return jsonPathTokens[0].path;
}
return undefined;
}
export function jsonPathFromAny(x: any) {
if (!x) { return undefined; }
if (typeof x === 'string') { return jsonPathString(x); }
return pathFromToken(Tokenization.reverse(x));
}
/**
* If the indicated string list is an encoded JSON path, return the path
*
* Otherwise return undefined.
*/
function jsonPathStringList(x: string[]): string | undefined {
return pathFromToken(Tokenization.reverseList(x));
}
/**
* If the indicated number is an encoded JSON path, return the path
*
* Otherwise return undefined.
*/
function jsonPathNumber(x: number): string | undefined {
return pathFromToken(Tokenization.reverseNumber(x));
}
function pathFromToken(token: IResolvable | undefined) {
return token && (JsonPathToken.isJsonPathToken(token) ? token.path : undefined);
}
/**
* Render the string or number value in a valid JSON Path expression.
*
* If the value is a Tokenized JSON path reference -- return the JSON path reference inside it.
* If the value is a number -- convert it to string.
* If the value is a string -- single-quote it.
* Otherwise, throw errors.
*
* Call this function whenever you're building compound JSONPath expressions, in
* order to avoid having tokens-in-tokens-in-tokens which become very hard to parse.
*/
export function renderInExpression(x: any) {
const path = jsonPathFromAny(x);
if (path) return path;
if (typeof x === 'number') return x.toString(10);
if (typeof x === 'string') return singleQuotestring(x);
throw new Error('Unxexpected value.');
}
function singleQuotestring(x: string) {
const ret = new Array<string>();
ret.push("'");
for (const c of x) {
if (c === "'") {
ret.push("\\'");
} else if (c === '\\') {
ret.push('\\\\');
} else if (c === '\n') {
ret.push('\\n');
} else {
ret.push(c);
}
}
ret.push("'");
return ret.join('');
}