-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathfields.ts
238 lines (211 loc) · 6.62 KB
/
fields.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
import { Token } from '@aws-cdk/core';
import { findReferencedPaths, jsonPathString, JsonPathToken, renderObject } from './json-path';
/**
* Extract a field from the State Machine data or context
* that gets passed around between states
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html
*/
export class JsonPath {
/**
* Special string value to discard state input, output or result
*/
public static readonly DISCARD = 'DISCARD';
/**
* Instead of using a literal string, get the value from a JSON path
*/
public static stringAt(path: string): string {
validateJsonPath(path);
return new JsonPathToken(path).toString();
}
/**
* Instead of using a literal string list, get the value from a JSON path
*/
public static listAt(path: string): string[] {
// does not apply to task context
validateDataPath(path);
return Token.asList(new JsonPathToken(path));
}
/**
* Instead of using a literal number, get the value from a JSON path
*/
public static numberAt(path: string): number {
validateJsonPath(path);
return Token.asNumber(new JsonPathToken(path));
}
/**
* Use the entire data structure
*
* Will be an object at invocation time, but is represented in the CDK
* application as a string.
*/
public static get entirePayload(): string {
return new JsonPathToken('$').toString();
}
/**
* Determines if the indicated string is an encoded JSON path
*
* @param value string to be evaluated
*/
public static isEncodedJsonPath(value: string): boolean {
return !!jsonPathString(value);
}
/**
* Return the Task Token field
*
* External actions will need this token to report step completion
* back to StepFunctions using the `SendTaskSuccess` or `SendTaskFailure`
* calls.
*/
public static get taskToken(): string {
return new JsonPathToken('$$.Task.Token').toString();
}
/**
* Use the entire context data structure
*
* Will be an object at invocation time, but is represented in the CDK
* application as a string.
*/
public static get entireContext(): string {
return new JsonPathToken('$$').toString();
}
private constructor() {}
}
/**
* Extract a field from the State Machine data that gets passed around between states
*
* @deprecated replaced by `JsonPath`
*/
export class Data {
/**
* Instead of using a literal string, get the value from a JSON path
*/
public static stringAt(path: string): string {
validateDataPath(path);
return new JsonPathToken(path).toString();
}
/**
* Instead of using a literal string list, get the value from a JSON path
*/
public static listAt(path: string): string[] {
validateDataPath(path);
return Token.asList(new JsonPathToken(path));
}
/**
* Instead of using a literal number, get the value from a JSON path
*/
public static numberAt(path: string): number {
validateDataPath(path);
return Token.asNumber(new JsonPathToken(path));
}
/**
* Use the entire data structure
*
* Will be an object at invocation time, but is represented in the CDK
* application as a string.
*/
public static get entirePayload(): string {
return new JsonPathToken('$').toString();
}
/**
* Determines if the indicated string is an encoded JSON path
*
* @param value string to be evaluated
*/
public static isJsonPathString(value: string): boolean {
return !!jsonPathString(value);
}
private constructor() {}
}
/**
* Extract a field from the State Machine Context data
*
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-to-resource.html#wait-token-contextobject
*
* @deprecated replaced by `JsonPath`
*/
export class Context {
/**
* Instead of using a literal string, get the value from a JSON path
*/
public static stringAt(path: string): string {
validateContextPath(path);
return new JsonPathToken(path).toString();
}
/**
* Instead of using a literal number, get the value from a JSON path
*/
public static numberAt(path: string): number {
validateContextPath(path);
return Token.asNumber(new JsonPathToken(path));
}
/**
* Return the Task Token field
*
* External actions will need this token to report step completion
* back to StepFunctions using the `SendTaskSuccess` or `SendTaskFailure`
* calls.
*/
public static get taskToken(): string {
return new JsonPathToken('$$.Task.Token').toString();
}
/**
* Use the entire context data structure
*
* Will be an object at invocation time, but is represented in the CDK
* application as a string.
*/
public static get entireContext(): string {
return new JsonPathToken('$$').toString();
}
private constructor() {}
}
/**
* Helper functions to work with structures containing fields
*/
export class FieldUtils {
/**
* Render a JSON structure containing fields to the right StepFunctions structure
*/
public static renderObject(obj?: { [key: string]: any }): { [key: string]: any } | undefined {
return renderObject(obj);
}
/**
* Return all JSON paths used in the given structure
*/
public static findReferencedPaths(obj?: { [key: string]: any }): string[] {
return Array.from(findReferencedPaths(obj)).sort();
}
/**
* Returns whether the given task structure contains the TaskToken field anywhere
*
* The field is considered included if the field itself or one of its containing
* fields occurs anywhere in the payload.
*/
public static containsTaskToken(obj?: { [key: string]: any }): boolean {
const paths = findReferencedPaths(obj);
return paths.has('$$.Task.Token') || paths.has('$$.Task') || paths.has('$$');
}
private constructor() {}
}
function validateJsonPath(path: string) {
if (path !== '$'
&& !path.startsWith('$.')
&& path !== '$$'
&& !path.startsWith('$$.')
&& !path.startsWith('$[')
&& ['Format', 'StringToJson', 'JsonToString', 'Array'].every(fn => !path.startsWith(`States.${fn}`))
) {
throw new Error(`JSON path values must be exactly '$', '$$', start with '$.', start with '$$.', start with '$[', or start with an intrinsic function: States.Format, States.StringToJson, States.JsonToString, or States.Array. Received: ${path}`);
}
}
function validateDataPath(path: string) {
if (path !== '$' && !path.startsWith('$.')) {
throw new Error("Data JSON path values must either be exactly equal to '$' or start with '$.'");
}
}
function validateContextPath(path: string) {
if (path !== '$$' && !path.startsWith('$$.')) {
throw new Error("Context JSON path values must either be exactly equal to '$$' or start with '$$.'");
}
}