Skip to content

Commit 7b8ef5b

Browse files
michaelwilesMichael Wilesshivlaks
authored
feat(stepfunctions-tasks): assign boolean value in DynamoDB from state input (Json path) (#9088)
It is now possible to map a boolean value from state input using a Json path. function booleanFromJsonPath added to DynamoAttributeValue which accepts a Json path Closes #9007 Co-authored-by: Michael Wiles <michaelwiles@lumkani.com> Co-authored-by: Shiv Lakshminarayan <shivlaks@amazon.com>
1 parent e8c0b58 commit 7b8ef5b

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

packages/@aws-cdk/aws-stepfunctions-tasks/lib/dynamodb/private/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ export function transformAttributeValueMap(attrMap?: { [key: string]: DynamoAttr
2222
}
2323
return attrMap ? transformedValue : undefined;
2424
}
25+
26+
export function validateJsonPath(value: string) {
27+
if (!value.startsWith('$')) {
28+
throw new Error("Data JSON path values must either be exactly equal to '$' or start with '$.'");
29+
}
30+
}

packages/@aws-cdk/aws-stepfunctions-tasks/lib/dynamodb/shared-types.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { transformAttributeValueMap } from './private/utils';
1+
import { transformAttributeValueMap, validateJsonPath } from './private/utils';
22

33
/**
44
* Determines the level of detail about provisioned throughput consumption that is returned.
@@ -205,9 +205,7 @@ export class DynamoAttributeValue {
205205
* @param value Json path that specifies state input to be used
206206
*/
207207
public static mapFromJsonPath(value: string) {
208-
if (!value.startsWith('$')) {
209-
throw new Error("Data JSON path values must either be exactly equal to '$' or start with '$.'");
210-
}
208+
validateJsonPath(value);
211209
return new DynamoAttributeValue({ 'M.$': value });
212210
}
213211

@@ -232,6 +230,17 @@ export class DynamoAttributeValue {
232230
return new DynamoAttributeValue({ BOOL: value });
233231
}
234232

233+
/**
234+
* Sets an attribute of type Boolean from state input through Json path.
235+
* For example: "BOOL": true
236+
*
237+
* @param value Json path that specifies state input to be used
238+
*/
239+
public static booleanFromJsonPath(value: string) {
240+
validateJsonPath(value);
241+
return new DynamoAttributeValue({ BOOL: value.toString() });
242+
}
243+
235244
/**
236245
* Represents the data for the attribute. Data can be
237246
* i.e. "S": "Hello"

packages/@aws-cdk/aws-stepfunctions-tasks/test/dynamodb/integ.call-dynamodb.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
{
194194
"Ref": "AWS::Partition"
195195
},
196-
":states:::dynamodb:putItem\",\"Parameters\":{\"Item\":{\"MessageId\":{\"S\":\"1234\"},\"Text\":{\"S.$\":\"$.bar\"},\"TotalCount\":{\"N\":\"18\"}},\"TableName\":\"",
196+
":states:::dynamodb:putItem\",\"Parameters\":{\"Item\":{\"MessageId\":{\"S\":\"1234\"},\"Text\":{\"S.$\":\"$.bar\"},\"TotalCount\":{\"N\":\"18\"},\"Activated\":{\"BOOL.$\":\"$.foo\"}},\"TableName\":\"",
197197
{
198198
"Ref": "Messages804FA4EB"
199199
},

packages/@aws-cdk/aws-stepfunctions-tasks/test/dynamodb/integ.call-dynamodb.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CallDynamoDBStack extends cdk.Stack {
3636
MessageId: tasks.DynamoAttributeValue.fromString(MESSAGE_ID),
3737
Text: tasks.DynamoAttributeValue.fromString(sfn.JsonPath.stringAt('$.bar')),
3838
TotalCount: tasks.DynamoAttributeValue.fromNumber(firstNumber),
39+
Activated: tasks.DynamoAttributeValue.booleanFromJsonPath(sfn.JsonPath.stringAt('$.foo')),
3940
},
4041
table,
4142
});

packages/@aws-cdk/aws-stepfunctions-tasks/test/dynamodb/put-item.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test('PutItem task', () => {
2727
expressionAttributeNames: { OTHER_KEY: '#OK' },
2828
expressionAttributeValues: {
2929
':val': tasks.DynamoAttributeValue.numberFromString(sfn.JsonPath.stringAt('$.Item.TotalCount.N')),
30+
':bool': tasks.DynamoAttributeValue.booleanFromJsonPath(sfn.JsonPath.stringAt('$.Item.flag')),
3031
},
3132
returnConsumedCapacity: tasks.DynamoConsumedCapacity.TOTAL,
3233
returnItemCollectionMetrics: tasks.DynamoItemCollectionMetrics.SIZE,
@@ -56,7 +57,10 @@ test('PutItem task', () => {
5657
},
5758
ConditionExpression: 'ForumName <> :f and Subject <> :s',
5859
ExpressionAttributeNames: { OTHER_KEY: '#OK' },
59-
ExpressionAttributeValues: { ':val': { 'N.$': '$.Item.TotalCount.N' } },
60+
ExpressionAttributeValues: {
61+
':val': { 'N.$': '$.Item.TotalCount.N' },
62+
':bool': { 'BOOL.$': '$.Item.flag' },
63+
},
6064
ReturnConsumedCapacity: 'TOTAL',
6165
ReturnItemCollectionMetrics: 'SIZE',
6266
ReturnValues: 'ALL_NEW',

packages/@aws-cdk/aws-stepfunctions-tasks/test/dynamodb/shared-types.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,33 @@ describe('DynamoAttributeValue', () => {
234234
});
235235
});
236236

237+
238+
test('from invalid boolean with json path', () => {
239+
// GIVEN
240+
const m = 'invalid';
241+
242+
// WHEN / THEN
243+
expect(() => {
244+
tasks.DynamoAttributeValue.booleanFromJsonPath(m);
245+
}).toThrow("Data JSON path values must either be exactly equal to '$' or start with '$.'");
246+
247+
});
248+
249+
250+
test('from boolean with json path', () => {
251+
// GIVEN
252+
const m = '$.path';
253+
// WHEN
254+
const attribute = tasks.DynamoAttributeValue.booleanFromJsonPath(sfn.JsonPath.stringAt(m));
255+
256+
// THEN
257+
expect(sfn.FieldUtils.renderObject(attribute)).toEqual({
258+
attributeValue: {
259+
'BOOL.$': m,
260+
},
261+
});
262+
});
263+
237264
test('from boolean', () => {
238265
// WHEN
239266
const attribute = tasks.DynamoAttributeValue.fromBoolean(true);

0 commit comments

Comments
 (0)