Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sfn) Pass: support more types for result #2811

Merged
merged 11 commits into from
Jun 21, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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,
Expand Down
57 changes: 50 additions & 7 deletions packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
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';

/**
* 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);
}

protected constructor(public readonly value: any) {
}
}

/**
* Properties for defining a Pass state
Expand Down Expand Up @@ -51,7 +94,7 @@ export interface PassProps {
*
* @default No injected result
*/
readonly result?: {[key: string]: any};
readonly result?: Result;
}

/**
Expand All @@ -62,7 +105,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);
Expand All @@ -86,10 +129,10 @@ 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(),
...this.renderNextEnd()
};
}
}
40 changes: 40 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions/test/test.pass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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();
},
'fromObject has proper value'(test: Test) {
const testValue = {a: 1};
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();
},
};