From a69e6aae75102189dc53486f210693710172c8e2 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 5 Sep 2023 07:44:26 -0500 Subject: [PATCH 1/8] feat(stepfunctions): add comment ability for when condition --- .../aws-stepfunctions-integ.assets.json | 6 +- .../aws-stepfunctions-integ.template.json | 4 +- .../integ.state-machine.js.snapshot/cdk.out | 2 +- .../integ.json | 2 +- .../manifest.json | 4 +- .../integ.state-machine.js.snapshot/tree.json | 90 ++++++++++++------- .../test/integ.state-machine.ts | 18 +++- .../aws-cdk-lib/aws-stepfunctions/README.md | 9 ++ .../aws-stepfunctions/lib/states/choice.ts | 4 +- .../aws-stepfunctions/lib/states/state.ts | 14 ++- .../test/state-machine.test.ts | 28 ++++++ 11 files changed, 133 insertions(+), 48 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.assets.json index 666810943028f..3adf6880c8981 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.assets.json @@ -1,7 +1,7 @@ { - "version": "32.0.0", + "version": "34.0.0", "files": { - "2b683032648fa932811d781e11acb71f29e8dcde21411e7cf232807cc2565770": { + "6fc80a94a8b95de238e82bf832c22a76af712d24dc10c332baa36571da0a4a41": { "source": { "path": "aws-stepfunctions-integ.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "2b683032648fa932811d781e11acb71f29e8dcde21411e7cf232807cc2565770.json", + "objectKey": "6fc80a94a8b95de238e82bf832c22a76af712d24dc10c332baa36571da0a4a41.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.template.json index 3c632bd05bb54..ae04405b0ce67 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/aws-stepfunctions-integ.template.json @@ -115,13 +115,13 @@ "StateMachine2E01A3A5": { "Type": "AWS::StepFunctions::StateMachine", "Properties": { + "DefinitionString": "{\"StartAt\":\"wait time\",\"States\":{\"wait time\":{\"Type\":\"Wait\",\"SecondsPath\":\"$.waitSeconds\",\"Next\":\"choice\"},\"choice\":{\"Type\":\"Choice\",\"Comment\":\"this is a comment for the choice state\",\"Choices\":[{\"Variable\":\"$.success\",\"IsPresent\":true,\"Next\":\"success\",\"Comment\":\"this is a comment for the when condition\"},{\"Variable\":\"$.noComment\",\"IsPresent\":true,\"Next\":\"short wait time\"}],\"Default\":\"success\"},\"success\":{\"Type\":\"Succeed\"},\"short wait time\":{\"Type\":\"Wait\",\"Seconds\":1,\"Next\":\"success\"}},\"Comment\":\"a super cool state machine\"}", "RoleArn": { "Fn::GetAtt": [ "StateMachineRoleB840431D", "Arn" ] - }, - "DefinitionString": "{\"StartAt\":\"wait time\",\"States\":{\"wait time\":{\"Type\":\"Wait\",\"SecondsPath\":\"$.waitSeconds\",\"End\":true}},\"Comment\":\"a super cool state machine\"}" + } }, "DependsOn": [ "StateMachineRoleB840431D" diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/cdk.out index f0b901e7c06e5..2313ab5436501 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/cdk.out +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"32.0.0"} \ No newline at end of file +{"version":"34.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/integ.json index 9fa389156dc96..4f14228e8f81e 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/integ.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/integ.json @@ -1,5 +1,5 @@ { - "version": "32.0.0", + "version": "34.0.0", "testCases": { "integ.state-machine": { "stacks": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/manifest.json index 43167ec92aa41..053b21f471c6e 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "32.0.0", + "version": "34.0.0", "artifacts": { "aws-stepfunctions-integ.assets": { "type": "cdk:asset-manifest", @@ -17,7 +17,7 @@ "validateOnSynth": false, "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/2b683032648fa932811d781e11acb71f29e8dcde21411e7cf232807cc2565770.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/6fc80a94a8b95de238e82bf832c22a76af712d24dc10c332baa36571da0a4a41.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/tree.json index fa4dffce7b9a1..9622a95aef1ba 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.js.snapshot/tree.json @@ -12,8 +12,32 @@ "id": "wait time", "path": "aws-stepfunctions-integ/wait time", "constructInfo": { - "fqn": "aws-cdk-lib.aws_stepfunctions.Wait", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" + } + }, + "short wait time": { + "id": "short wait time", + "path": "aws-stepfunctions-integ/short wait time", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.70" + } + }, + "choice": { + "id": "choice", + "path": "aws-stepfunctions-integ/choice", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.70" + } + }, + "success": { + "id": "success", + "path": "aws-stepfunctions-integ/success", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "Role": { @@ -24,8 +48,8 @@ "id": "ImportRole", "path": "aws-stepfunctions-integ/Role/ImportRole", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "Resource": { @@ -49,8 +73,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnRole", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "DefaultPolicy": { @@ -139,20 +163,20 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnPolicy", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Policy", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Role", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "StateMachine": { @@ -167,8 +191,8 @@ "id": "ImportRole", "path": "aws-stepfunctions-integ/StateMachine/Role/ImportRole", "constructInfo": { - "fqn": "aws-cdk-lib.Resource", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "Resource": { @@ -192,14 +216,14 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.CfnRole", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_iam.Role", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "Resource": { @@ -208,46 +232,46 @@ "attributes": { "aws:cdk:cloudformation:type": "AWS::StepFunctions::StateMachine", "aws:cdk:cloudformation:props": { + "definitionString": "{\"StartAt\":\"wait time\",\"States\":{\"wait time\":{\"Type\":\"Wait\",\"SecondsPath\":\"$.waitSeconds\",\"Next\":\"choice\"},\"choice\":{\"Type\":\"Choice\",\"Comment\":\"this is a comment for the choice state\",\"Choices\":[{\"Variable\":\"$.success\",\"IsPresent\":true,\"Next\":\"success\",\"Comment\":\"this is a comment for the when condition\"},{\"Variable\":\"$.noComment\",\"IsPresent\":true,\"Next\":\"short wait time\"}],\"Default\":\"success\"},\"success\":{\"Type\":\"Succeed\"},\"short wait time\":{\"Type\":\"Wait\",\"Seconds\":1,\"Next\":\"success\"}},\"Comment\":\"a super cool state machine\"}", "roleArn": { "Fn::GetAtt": [ "StateMachineRoleB840431D", "Arn" ] - }, - "definitionString": "{\"StartAt\":\"wait time\",\"States\":{\"wait time\":{\"Type\":\"Wait\",\"SecondsPath\":\"$.waitSeconds\",\"End\":true}},\"Comment\":\"a super cool state machine\"}" + } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_stepfunctions.CfnStateMachine", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_stepfunctions.StateMachine", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "BootstrapVersion": { "id": "BootstrapVersion", "path": "aws-stepfunctions-integ/BootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnParameter", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "CheckBootstrapVersion": { "id": "CheckBootstrapVersion", "path": "aws-stepfunctions-integ/CheckBootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnRule", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } }, "Tree": { @@ -255,13 +279,13 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.26" + "version": "10.2.70" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.App", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.2.70" } } } \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts index 28de085110615..4d69c528eafc6 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts @@ -14,12 +14,28 @@ const wait = new sfn.Wait(stack, 'wait time', { time: sfn.WaitTime.secondsPath('$.waitSeconds'), }); +const shortWait = new sfn.Wait(stack, 'short wait time', { + time: sfn.WaitTime.duration(cdk.Duration.seconds(1)), +}); + +const choice = new sfn.Choice(stack, 'choice', { + comment: 'this is a comment for the choice state', +}); + +const success = new sfn.Succeed(stack, 'success'); + +choice.when(sfn.Condition.isPresent('$.success'), success, 'this is a comment for the when condition'); +choice.when(sfn.Condition.isPresent('$.noComment'), shortWait); +choice.otherwise(success); +wait.next(choice); +shortWait.next(success); + const role = new iam.Role(stack, 'Role', { assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'), }); const stateMachine = new sfn.StateMachine(stack, 'StateMachine', { - definition: wait, + definitionBody: sfn.DefinitionBody.fromChainable(wait), comment: 'a super cool state machine', }); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/README.md b/packages/aws-cdk-lib/aws-stepfunctions/README.md index 51d17d16c6204..59453e23c32d4 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/README.md +++ b/packages/aws-cdk-lib/aws-stepfunctions/README.md @@ -351,6 +351,15 @@ const shipTheItem = new sfn.Pass(this, 'ShipTheItem'); choice.afterwards().next(shipTheItem); ``` +You can add comments to `Choice` states as well as conditions that use `choice.when`. + +```ts +const choice = new sfn.Choice(stack, 'What color is it?', { + comment: 'color comment', +}); +choice.when(sfn.Condition.stringEquals('$.color', 'BLUE'), handleBlueItem, 'blue item comment'); +``` + If your `Choice` doesn't have an `otherwise()` and none of the conditions match the JSON state, a `NoChoiceMatched` error will be thrown. Wrap the state machine in a `Parallel` state if you want to catch and recover from this. diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index b27f046c4c1d2..f02f5c19e9e7f 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -53,8 +53,8 @@ export class Choice extends State { /** * If the given condition matches, continue execution with the given state */ - public when(condition: Condition, next: IChainable): Choice { - super.addChoice(condition, next.startState); + public when(condition: Condition, next: IChainable, comment?: string): Choice { + super.addChoice(condition, next.startState, comment); return this; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 66165ebd7f198..b9906e5328ea7 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -318,8 +318,8 @@ export abstract class State extends Construct implements IChainable { /** * Add a choice branch to this state */ - protected addChoice(condition: Condition, next: State) { - this.choices.push({ condition, next }); + protected addChoice(condition: Condition, next: State, comment?: string) { + this.choices.push({ condition, next, comment }); next.startState.addIncoming(this); if (this.containingGraph) { next.startState.bindToGraph(this.containingGraph); @@ -327,7 +327,7 @@ export abstract class State extends Construct implements IChainable { } /** - * Add a paralle branch to this state + * Add a parallel branch to this state */ protected addBranch(branch: StateGraph) { this.branches.push(branch); @@ -489,6 +489,13 @@ interface ChoiceTransition { * Condition for this transition */ condition: Condition; + + /** + * An optional description for this choice transition + * + * @default No comment + */ + comment?: string; } /** @@ -498,6 +505,7 @@ function renderChoice(c: ChoiceTransition) { return { ...c.condition.renderCondition(), Next: c.next.stateId, + Comment: c.comment, }; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts index 9a34e1feee46c..15beb79986ded 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts @@ -622,4 +622,32 @@ describe('State Machine', () => { StateMachineRevisionId: { 'Fn::GetAtt': ['MyStateMachine6C968CA5', 'StateMachineRevisionId'] }, }); }); + + test('comments rendered properly', () => { + // GIVEN + const stack = new cdk.Stack(); + + const choice = new sfn.Choice(stack, 'choice', { + comment: 'nebraska', + }); + const success = new sfn.Succeed(stack, 'success'); + choice.when(sfn.Condition.isPresent('$.success'), success, 'london'); + choice.otherwise(success); + + // WHEN + const stateMachine = new sfn.StateMachine(stack, 'MyStateMachine', { + stateMachineName: 'MyStateMachine', + definitionBody: sfn.DefinitionBody.fromChainable(choice), + }); + + new sfn.CfnStateMachineVersion(stack, 'MyStateMachineVersion', { + stateMachineRevisionId: stateMachine.stateMachineRevisionId, + stateMachineArn: stateMachine.stateMachineArn, + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::StepFunctions::StateMachine', { + DefinitionString: '{"StartAt":"choice","States":{"choice":{"Type":"Choice","Comment":"nebraska","Choices":[{"Variable":"$.success","IsPresent":true,"Next":"success","Comment":"london"}],"Default":"success"},"success":{"Type":"Succeed"}}}', + }); + }); }); From 274b4aee3c75c14678879ed0e7d4425e64d7f810 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 5 Sep 2023 08:22:23 -0500 Subject: [PATCH 2/8] Update readme --- packages/aws-cdk-lib/aws-stepfunctions/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/README.md b/packages/aws-cdk-lib/aws-stepfunctions/README.md index 59453e23c32d4..51a1488815b09 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/README.md +++ b/packages/aws-cdk-lib/aws-stepfunctions/README.md @@ -354,10 +354,13 @@ choice.afterwards().next(shipTheItem); You can add comments to `Choice` states as well as conditions that use `choice.when`. ```ts -const choice = new sfn.Choice(stack, 'What color is it?', { +const choice = new sfn.Choice(this, 'What color is it?', { comment: 'color comment', }); +const handleBlueItem = new sfn.Pass(this, 'HandleBlueItem'); +const handleOtherItemColor = new sfn.Pass(this, 'HanldeOtherItemColor'); choice.when(sfn.Condition.stringEquals('$.color', 'BLUE'), handleBlueItem, 'blue item comment'); +choice.otherwise(handleOtherItemColor); ``` If your `Choice` doesn't have an `otherwise()` and none of the conditions match From 9539cd3175c02a90fe7e52caa088f8fabee7ebe1 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 5 Sep 2023 13:34:32 -0500 Subject: [PATCH 3/8] Add feedback from Kaizen --- .../aws-stepfunctions/test/integ.state-machine.ts | 4 +++- packages/aws-cdk-lib/aws-stepfunctions/README.md | 4 +++- .../aws-stepfunctions/lib/states/choice.ts | 6 +++--- .../aws-stepfunctions/lib/states/state.ts | 13 +++++++++---- .../aws-stepfunctions/test/state-machine.test.ts | 4 +++- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts index 4d69c528eafc6..5ff6711106589 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions/test/integ.state-machine.ts @@ -24,7 +24,9 @@ const choice = new sfn.Choice(stack, 'choice', { const success = new sfn.Succeed(stack, 'success'); -choice.when(sfn.Condition.isPresent('$.success'), success, 'this is a comment for the when condition'); +choice.when(sfn.Condition.isPresent('$.success'), success, { + comment: 'this is a comment for the when condition', +}); choice.when(sfn.Condition.isPresent('$.noComment'), shortWait); choice.otherwise(success); wait.next(choice); diff --git a/packages/aws-cdk-lib/aws-stepfunctions/README.md b/packages/aws-cdk-lib/aws-stepfunctions/README.md index 51a1488815b09..b0af8ce337f37 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/README.md +++ b/packages/aws-cdk-lib/aws-stepfunctions/README.md @@ -359,7 +359,9 @@ const choice = new sfn.Choice(this, 'What color is it?', { }); const handleBlueItem = new sfn.Pass(this, 'HandleBlueItem'); const handleOtherItemColor = new sfn.Pass(this, 'HanldeOtherItemColor'); -choice.when(sfn.Condition.stringEquals('$.color', 'BLUE'), handleBlueItem, 'blue item comment'); +choice.when(sfn.Condition.stringEquals('$.color', 'BLUE'), handleBlueItem, { + comment: 'blue item comment', +}); choice.otherwise(handleOtherItemColor); ``` diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index f02f5c19e9e7f..cb876a239fae0 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -1,6 +1,6 @@ import { Construct } from 'constructs'; import { StateType } from './private/state-type'; -import { State } from './state'; +import { ChoiceTransitionOptions, State } from './state'; import { Chain } from '../chain'; import { Condition } from '../condition'; import { IChainable, INextable } from '../types'; @@ -53,8 +53,8 @@ export class Choice extends State { /** * If the given condition matches, continue execution with the given state */ - public when(condition: Condition, next: IChainable, comment?: string): Choice { - super.addChoice(condition, next.startState, comment); + public when(condition: Condition, next: IChainable, options: ChoiceTransitionOptions = {}): Choice { + super.addChoice(condition, next.startState, options); return this; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index b9906e5328ea7..29eeba5c8a5ec 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -318,8 +318,8 @@ export abstract class State extends Construct implements IChainable { /** * Add a choice branch to this state */ - protected addChoice(condition: Condition, next: State, comment?: string) { - this.choices.push({ condition, next, comment }); + protected addChoice(condition: Condition, next: State, options: ChoiceTransitionOptions) { + this.choices.push({ condition, next, ...options }); next.startState.addIncoming(this); if (this.containingGraph) { next.startState.bindToGraph(this.containingGraph); @@ -479,7 +479,7 @@ export interface FindStateOptions { /** * A Choice Transition */ -interface ChoiceTransition { +interface ChoiceTransition extends ChoiceTransitionOptions { /** * State to transition to */ @@ -489,9 +489,14 @@ interface ChoiceTransition { * Condition for this transition */ condition: Condition; +} +/** + * Options for Choice Transition + */ +export interface ChoiceTransitionOptions { /** - * An optional description for this choice transition + * An optional description for the choice transition * * @default No comment */ diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts index 15beb79986ded..df71c7d4fa392 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/state-machine.test.ts @@ -631,7 +631,9 @@ describe('State Machine', () => { comment: 'nebraska', }); const success = new sfn.Succeed(stack, 'success'); - choice.when(sfn.Condition.isPresent('$.success'), success, 'london'); + choice.when(sfn.Condition.isPresent('$.success'), success, { + comment: 'london', + }); choice.otherwise(success); // WHEN From 7165b37cc7540ad388045c3a26a5957618356057 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 5 Sep 2023 13:45:53 -0500 Subject: [PATCH 4/8] Trigger Build From d28c13976e9ab3c6a782bfa5d204184646129228 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 5 Sep 2023 14:21:03 -0500 Subject: [PATCH 5/8] Update to IChoiceTransitionOptions --- packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts | 4 ++-- packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index cb876a239fae0..e0d8152f2f664 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -1,6 +1,6 @@ import { Construct } from 'constructs'; import { StateType } from './private/state-type'; -import { ChoiceTransitionOptions, State } from './state'; +import { IChoiceTransitionOptions, State } from './state'; import { Chain } from '../chain'; import { Condition } from '../condition'; import { IChainable, INextable } from '../types'; @@ -53,7 +53,7 @@ export class Choice extends State { /** * If the given condition matches, continue execution with the given state */ - public when(condition: Condition, next: IChainable, options: ChoiceTransitionOptions = {}): Choice { + public when(condition: Condition, next: IChainable, options: IChoiceTransitionOptions = {}): Choice { super.addChoice(condition, next.startState, options); return this; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 29eeba5c8a5ec..06420eabea16e 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -318,7 +318,7 @@ export abstract class State extends Construct implements IChainable { /** * Add a choice branch to this state */ - protected addChoice(condition: Condition, next: State, options: ChoiceTransitionOptions) { + protected addChoice(condition: Condition, next: State, options: IChoiceTransitionOptions) { this.choices.push({ condition, next, ...options }); next.startState.addIncoming(this); if (this.containingGraph) { @@ -479,7 +479,7 @@ export interface FindStateOptions { /** * A Choice Transition */ -interface ChoiceTransition extends ChoiceTransitionOptions { +interface ChoiceTransition extends IChoiceTransitionOptions { /** * State to transition to */ @@ -494,7 +494,7 @@ interface ChoiceTransition extends ChoiceTransitionOptions { /** * Options for Choice Transition */ -export interface ChoiceTransitionOptions { +export interface IChoiceTransitionOptions { /** * An optional description for the choice transition * From 927f62847cd3bb9ab8029a525b049ea60a4b173f Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Tue, 5 Sep 2023 15:08:17 -0500 Subject: [PATCH 6/8] options can be undefined --- packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts | 2 +- packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index e0d8152f2f664..b6bb68767a919 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -53,7 +53,7 @@ export class Choice extends State { /** * If the given condition matches, continue execution with the given state */ - public when(condition: Condition, next: IChainable, options: IChoiceTransitionOptions = {}): Choice { + public when(condition: Condition, next: IChainable, options?: IChoiceTransitionOptions): Choice { super.addChoice(condition, next.startState, options); return this; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 06420eabea16e..0b158eb6efa0e 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -318,7 +318,7 @@ export abstract class State extends Construct implements IChainable { /** * Add a choice branch to this state */ - protected addChoice(condition: Condition, next: State, options: IChoiceTransitionOptions) { + protected addChoice(condition: Condition, next: State, options?: IChoiceTransitionOptions) { this.choices.push({ condition, next, ...options }); next.startState.addIncoming(this); if (this.containingGraph) { From a17422f17c66ec8eed2ca671efe85fb1cf5fe271 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Fri, 8 Sep 2023 07:47:55 -0500 Subject: [PATCH 7/8] change comment to readonly per Jonathan --- .../aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts | 4 ++-- .../aws-cdk-lib/aws-stepfunctions/lib/states/state.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts index b6bb68767a919..55db170551b34 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/choice.ts @@ -1,6 +1,6 @@ import { Construct } from 'constructs'; import { StateType } from './private/state-type'; -import { IChoiceTransitionOptions, State } from './state'; +import { ChoiceTransitionOptions, State } from './state'; import { Chain } from '../chain'; import { Condition } from '../condition'; import { IChainable, INextable } from '../types'; @@ -53,7 +53,7 @@ export class Choice extends State { /** * If the given condition matches, continue execution with the given state */ - public when(condition: Condition, next: IChainable, options?: IChoiceTransitionOptions): Choice { + public when(condition: Condition, next: IChainable, options?: ChoiceTransitionOptions): Choice { super.addChoice(condition, next.startState, options); return this; } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 0b158eb6efa0e..5bdfe52e95146 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -318,7 +318,7 @@ export abstract class State extends Construct implements IChainable { /** * Add a choice branch to this state */ - protected addChoice(condition: Condition, next: State, options?: IChoiceTransitionOptions) { + protected addChoice(condition: Condition, next: State, options?: ChoiceTransitionOptions) { this.choices.push({ condition, next, ...options }); next.startState.addIncoming(this); if (this.containingGraph) { @@ -479,7 +479,7 @@ export interface FindStateOptions { /** * A Choice Transition */ -interface ChoiceTransition extends IChoiceTransitionOptions { +export interface ChoiceTransition extends ChoiceTransitionOptions { /** * State to transition to */ @@ -494,13 +494,13 @@ interface ChoiceTransition extends IChoiceTransitionOptions { /** * Options for Choice Transition */ -export interface IChoiceTransitionOptions { +export interface ChoiceTransitionOptions { /** * An optional description for the choice transition * * @default No comment */ - comment?: string; + readonly comment?: string; } /** From dd616be4a28df8a4c4a04fd09d0a7edc8e183646 Mon Sep 17 00:00:00 2001 From: Michael Sambol Date: Fri, 8 Sep 2023 07:57:35 -0500 Subject: [PATCH 8/8] remove export --- packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts index 5bdfe52e95146..2ea3e50124f72 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/state.ts @@ -479,7 +479,7 @@ export interface FindStateOptions { /** * A Choice Transition */ -export interface ChoiceTransition extends ChoiceTransitionOptions { +interface ChoiceTransition extends ChoiceTransitionOptions { /** * State to transition to */