From 76abc6aef7e4be9f480f32149bc14360d83fc110 Mon Sep 17 00:00:00 2001 From: spg Date: Mon, 10 Jun 2019 11:00:59 -0700 Subject: [PATCH 1/7] fix(sfn) Pass: support more types for result --- packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts index 4bb97d4374f39..c4f0c26684a09 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts @@ -51,7 +51,7 @@ export interface PassProps { * * @default No injected result */ - readonly result?: {[key: string]: any}; + readonly result?: {[key: string]: any} | string | number | boolean | null | any[]; } /** From b5794ad3d60b2f4d0721e08a887a26cb46ed51ee Mon Sep 17 00:00:00 2001 From: spg Date: Fri, 14 Jun 2019 16:54:19 -0700 Subject: [PATCH 2/7] Add union-like class --- .../aws-stepfunctions/lib/states/pass.ts | 82 +++++++++++++++++-- .../aws-stepfunctions/test/test.pass.ts | 33 ++++++++ 2 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts index c4f0c26684a09..6aafef9a10cdb 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts @@ -1,7 +1,77 @@ import cdk = require('@aws-cdk/cdk'); -import { Chain } from '../chain'; -import { IChainable, INextable } from '../types'; -import { renderJsonPath, State, StateType } from './state'; +import {Chain} from '../chain'; +import {IChainable, INextable} from '../types'; +import {renderJsonPath, State, StateType} from './state'; + +export abstract class Result { + public static fromString(value: string): StringResult { + return new StringResult(value); + } + + public static fromNumber(value: number): NumberResult { + return new NumberResult(value); + } + + public static fromBoolean(value: boolean): BooleanResult { + return new BooleanResult(value); + } + + public static fromMap(value: {[key: string]: any}): MapResult { + return new MapResult(value); + } + + public abstract value: any; +} + +export class StringResult extends Result { + public readonly value: string; + + constructor(value: string) { + super(); + + this.value = value; + } +} + +export class NumberResult extends Result { + public readonly value: number; + + constructor(value: number) { + super(); + + this.value = value; + } +} + +export class BooleanResult extends Result { + readonly value: boolean; + + constructor(value: boolean) { + super(); + + this.value = value; + } +} + +export class ArrayResult extends Result { + public readonly value: any[]; + + constructor(value: any[]) { + super(); + + this.value = value; + } +} + +export class MapResult extends Result { + public readonly value: {[key: string]: any}; + + constructor(value: {[key: string]: any}) { + super(); + + this.value = value; + } +} /** * Properties for defining a Pass state @@ -51,7 +121,7 @@ export interface PassProps { * * @default No injected result */ - readonly result?: {[key: string]: any} | string | number | boolean | null | any[]; + readonly result?: Result; } /** @@ -62,7 +132,7 @@ export interface PassProps { export class Pass extends State implements INextable { public readonly endStates: INextable[]; - private readonly result?: any; + private readonly result?: Result; constructor(scope: cdk.Construct, id: string, props: PassProps = {}) { super(scope, id, props); @@ -86,7 +156,7 @@ export class Pass extends State implements INextable { return { Type: StateType.Pass, Comment: this.comment, - Result: this.result, + Result: this.result ? this.result.value : undefined, ResultPath: renderJsonPath(this.resultPath), ...this.renderInputOutput(), ...this.renderNextEnd(), diff --git a/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts b/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts new file mode 100644 index 0000000000000..a294ed0eba572 --- /dev/null +++ b/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts @@ -0,0 +1,33 @@ +import { Test } from 'nodeunit'; +import { Result } from "../lib"; + +export = { + 'fromString has proper value'(test: Test) { + const testValue = 'test string'; + const result = Result.fromString(testValue); + test.equal(result.value, testValue); + + test.done(); + }, + 'fromNumber has proper value'(test: Test) { + const testValue = 1; + const result = Result.fromNumber(testValue); + test.equal(result.value, testValue); + + test.done(); + }, + 'fromBoolean has proper value'(test: Test) { + const testValue = false; + const result = Result.fromBoolean(testValue); + test.equal(result.value, testValue); + + test.done(); + }, + 'fromMap has proper value'(test: Test) { + const testValue = {a: 1}; + const result = Result.fromMap(testValue); + test.deepEqual(result.value, testValue); + + test.done(); + }, +}; From af216fff50da4d2489a3346f2434b29d4048bc7e Mon Sep 17 00:00:00 2001 From: spg Date: Mon, 17 Jun 2019 10:28:18 -0700 Subject: [PATCH 3/7] Fix --- .../aws-stepfunctions/lib/states/pass.ts | 80 ++++++------------- .../aws-stepfunctions/test/test.pass.ts | 11 ++- 2 files changed, 34 insertions(+), 57 deletions(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts index 6aafef9a10cdb..4f10ddbdef6aa 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts @@ -3,74 +3,44 @@ import {Chain} from '../chain'; import {IChainable, INextable} from '../types'; import {renderJsonPath, State, StateType} from './state'; -export abstract class Result { - public static fromString(value: string): StringResult { - return new StringResult(value); - } - - public static fromNumber(value: number): NumberResult { - return new NumberResult(value); - } +export interface ResultValue { + readonly stringValue?: string + readonly numberValue?: number + readonly booleanValue?: boolean + readonly mapValue?: {[key: string]: any} +} - public static fromBoolean(value: boolean): BooleanResult { - return new BooleanResult(value); +export class Result { + public static fromString(value: string): Result { + return new Result(ResultType.Text, value); } - public static fromMap(value: {[key: string]: any}): MapResult { - return new MapResult(value); + public static fromNumber(value: number): Result { + return new Result(ResultType.Number, value); } - public abstract value: any; -} - -export class StringResult extends Result { - public readonly value: string; - - constructor(value: string) { - super(); - - this.value = value; + public static fromBoolean(value: boolean): Result { + return new Result(ResultType.Boolean, value); } -} - -export class NumberResult extends Result { - public readonly value: number; - constructor(value: number) { - super(); - - this.value = value; + public static fromObject(value: {[key: string]: any}): Result { + return new Result(ResultType.Object, value); } -} - -export class BooleanResult extends Result { - readonly value: boolean; - constructor(value: boolean) { - super(); - - this.value = value; + public static fromArray(value: any[]): Result { + return new Result(ResultType.Array, value); } -} -export class ArrayResult extends Result { - public readonly value: any[]; - - constructor(value: any[]) { - super(); - - this.value = value; + private constructor(public readonly type: ResultType, public readonly value: any) { } } -export class MapResult extends Result { - public readonly value: {[key: string]: any}; - - constructor(value: {[key: string]: any}) { - super(); - - this.value = value; - } +export enum ResultType { + Text, + Number, + Boolean, + Object, + Array, } /** @@ -159,7 +129,7 @@ export class Pass extends State implements INextable { Result: this.result ? this.result.value : undefined, ResultPath: renderJsonPath(this.resultPath), ...this.renderInputOutput(), - ...this.renderNextEnd(), + ...this.renderNextEnd() }; } } diff --git a/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts b/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts index a294ed0eba572..3c1fc489627b2 100644 --- a/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts @@ -23,9 +23,16 @@ export = { test.done(); }, - 'fromMap has proper value'(test: Test) { + 'fromObject has proper value'(test: Test) { const testValue = {a: 1}; - const result = Result.fromMap(testValue); + const result = Result.fromObject(testValue); + test.deepEqual(result.value, testValue); + + test.done(); + }, + 'fromArray has proper value'(test: Test) { + const testValue = [1]; + const result = Result.fromArray(testValue); test.deepEqual(result.value, testValue); test.done(); From 6b7ec89b9f7203f001b28fda2f3d2f6fd87212ba Mon Sep 17 00:00:00 2001 From: spg Date: Mon, 17 Jun 2019 11:21:03 -0700 Subject: [PATCH 4/7] Fix aws-stepfunctions-tasks tests --- .../@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts | 2 +- .../@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts index 14e4d3789219d..b7d2e653f28bc 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.ts @@ -28,7 +28,7 @@ taskDefinition.addContainer('TheContainer', { // Build state machine const definition = new sfn.Pass(stack, 'Start', { - result: { SomeKey: 'SomeValue' } + result: sfn.Result.fromObject({ SomeKey: 'SomeValue' }) }).next(new sfn.Task(stack, 'Run', { task: new tasks.RunEcsEc2Task({ cluster, taskDefinition, containerOverrides: [ diff --git a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts index 751c06dd35f5b..59e5539defcde 100644 --- a/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts +++ b/packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.ts @@ -27,7 +27,7 @@ taskDefinition.addContainer('TheContainer', { // Build state machine const definition = new sfn.Pass(stack, 'Start', { - result: { SomeKey: 'SomeValue' } + result: sfn.Result.fromObject({ SomeKey: 'SomeValue' }) }).next(new sfn.Task(stack, 'FargateTask', { task: new tasks.RunEcsFargateTask({ cluster, taskDefinition, assignPublicIp: true, From c6ee349c0bc2256479f010bd710201b399138e60 Mon Sep 17 00:00:00 2001 From: spg Date: Mon, 17 Jun 2019 12:59:17 -0700 Subject: [PATCH 5/7] Dead code --- packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts index 4f10ddbdef6aa..bc6bc6cc56c00 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts @@ -3,13 +3,6 @@ import {Chain} from '../chain'; import {IChainable, INextable} from '../types'; import {renderJsonPath, State, StateType} from './state'; -export interface ResultValue { - readonly stringValue?: string - readonly numberValue?: number - readonly booleanValue?: boolean - readonly mapValue?: {[key: string]: any} -} - export class Result { public static fromString(value: string): Result { return new Result(ResultType.Text, value); From 3d79cf8ab8f8235884fa2f534d3afb4022eaf69d Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 21 Jun 2019 13:49:55 +0200 Subject: [PATCH 6/7] Remove resultType, leave subclassing of Result open --- .../aws-stepfunctions/lib/states/pass.ts | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts index bc6bc6cc56c00..48419dbebfd23 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts @@ -5,37 +5,29 @@ import {renderJsonPath, State, StateType} from './state'; export class Result { public static fromString(value: string): Result { - return new Result(ResultType.Text, value); + return new Result(value); } public static fromNumber(value: number): Result { - return new Result(ResultType.Number, value); + return new Result(value); } public static fromBoolean(value: boolean): Result { - return new Result(ResultType.Boolean, value); + return new Result(value); } public static fromObject(value: {[key: string]: any}): Result { - return new Result(ResultType.Object, value); + return new Result(value); } public static fromArray(value: any[]): Result { - return new Result(ResultType.Array, value); + return new Result(value); } - private constructor(public readonly type: ResultType, public readonly value: any) { + protected constructor(public readonly value: any) { } } -export enum ResultType { - Text, - Number, - Boolean, - Object, - Array, -} - /** * Properties for defining a Pass state */ From cd4278114fcb225b41fa4a2c63fc875ecb707448 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 21 Jun 2019 16:31:31 +0200 Subject: [PATCH 7/7] Add docs --- .../aws-stepfunctions/lib/states/pass.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts index 48419dbebfd23..f8630562d5d5f 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts @@ -3,23 +3,41 @@ import {Chain} from '../chain'; import {IChainable, INextable} from '../types'; import {renderJsonPath, State, StateType} from './state'; +/** + * The result of a Pass operation + */ export class Result { + /** + * The result of the operation is a string + */ public static fromString(value: string): Result { return new Result(value); } + /** + * The result of the operation is a number + */ public static fromNumber(value: number): Result { return new Result(value); } + /** + * The result of the operation is a boolean + */ public static fromBoolean(value: boolean): Result { return new Result(value); } + /** + * The result of the operation is an object + */ public static fromObject(value: {[key: string]: any}): Result { return new Result(value); } + /** + * The result of the operation is an array + */ public static fromArray(value: any[]): Result { return new Result(value); }