Skip to content

Commit

Permalink
fix(sfn): Pass support non-object Result types (#2811)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Pierre Gingras authored and rix0rrr committed Jun 21, 2019
1 parent ad72271 commit 5282a08
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 9 deletions.
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();
},
};

0 comments on commit 5282a08

Please sign in to comment.