|
1 |
| -import { Token } from '@aws-cdk/core'; |
2 |
| -import { findReferencedPaths, jsonPathString, JsonPathToken, renderObject } from './json-path'; |
| 1 | +import { Token, IResolvable } from '@aws-cdk/core'; |
| 2 | +import { findReferencedPaths, jsonPathString, JsonPathToken, renderObject, renderInExpression, jsonPathFromAny } from './private/json-path'; |
3 | 3 |
|
4 | 4 | /**
|
5 | 5 | * Extract a field from the State Machine data or context
|
@@ -38,6 +38,14 @@ export class JsonPath {
|
38 | 38 | return Token.asNumber(new JsonPathToken(path));
|
39 | 39 | }
|
40 | 40 |
|
| 41 | + /** |
| 42 | + * Reference a complete (complex) object in a JSON path location |
| 43 | + */ |
| 44 | + public static objectAt(path: string): IResolvable { |
| 45 | + validateJsonPath(path); |
| 46 | + return new JsonPathToken(path); |
| 47 | + } |
| 48 | + |
41 | 49 | /**
|
42 | 50 | * Use the entire data structure
|
43 | 51 | *
|
@@ -78,6 +86,82 @@ export class JsonPath {
|
78 | 86 | return new JsonPathToken('$$').toString();
|
79 | 87 | }
|
80 | 88 |
|
| 89 | + /** |
| 90 | + * Make an intrinsic States.Array expression |
| 91 | + * |
| 92 | + * Combine any number of string literals or JsonPath expressions into an array. |
| 93 | + * |
| 94 | + * Use this function if the value of an array element directly has to come |
| 95 | + * from a JSON Path expression (either the State object or the Context object). |
| 96 | + * |
| 97 | + * If the array contains object literals whose values come from a JSON path |
| 98 | + * expression, you do not need to use this function. |
| 99 | + * |
| 100 | + * @see https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html |
| 101 | + */ |
| 102 | + public static array(...values: string[]): string { |
| 103 | + return new JsonPathToken(`States.Array(${values.map(renderInExpression).join(', ')})`).toString(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Make an intrinsic States.Format expression |
| 108 | + * |
| 109 | + * This can be used to embed JSON Path variables inside a format string. |
| 110 | + * |
| 111 | + * For example: |
| 112 | + * |
| 113 | + * ```ts |
| 114 | + * sfn.JsonPath.format('Hello, my name is {}.', sfn.JsonPath.stringAt('$.name')) |
| 115 | + * ``` |
| 116 | + * |
| 117 | + * @see https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html |
| 118 | + */ |
| 119 | + public static format(formatString: string, ...values: string[]): string { |
| 120 | + const allArgs = [formatString, ...values]; |
| 121 | + return new JsonPathToken(`States.Format(${allArgs.map(renderInExpression).join(', ')})`).toString(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Make an intrinsic States.StringToJson expression |
| 126 | + * |
| 127 | + * During the execution of the Step Functions state machine, parse the given |
| 128 | + * argument as JSON into its object form. |
| 129 | + * |
| 130 | + * For example: |
| 131 | + * |
| 132 | + * ```ts |
| 133 | + * sfn.JsonPath.stringToJson(sfn.JsonPath.stringAt('$.someJsonBody')) |
| 134 | + * ``` |
| 135 | + * |
| 136 | + * @see https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html |
| 137 | + */ |
| 138 | + public static stringToJson(jsonString: string): IResolvable { |
| 139 | + return new JsonPathToken(`States.StringToJson(${renderInExpression(jsonString)})`); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Make an intrinsic States.JsonToString expression |
| 144 | + * |
| 145 | + * During the execution of the Step Functions state machine, encode the |
| 146 | + * given object into a JSON string. |
| 147 | + * |
| 148 | + * For example: |
| 149 | + * |
| 150 | + * ```ts |
| 151 | + * sfn.JsonPath.jsonToString(sfn.JsonPath.objectAt('$.someObject')) |
| 152 | + * ``` |
| 153 | + * |
| 154 | + * @see https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html |
| 155 | + */ |
| 156 | + public static jsonToString(value: any): string { |
| 157 | + const path = jsonPathFromAny(value); |
| 158 | + if (!path) { |
| 159 | + throw new Error('Argument to JsonPath.jsonToString() must be a JsonPath object'); |
| 160 | + } |
| 161 | + |
| 162 | + return new JsonPathToken(`States.JsonToString(${path})`).toString(); |
| 163 | + } |
| 164 | + |
81 | 165 | private constructor() {}
|
82 | 166 | }
|
83 | 167 |
|
|
0 commit comments