Skip to content

Commit

Permalink
Add union-like class
Browse files Browse the repository at this point in the history
  • Loading branch information
spg committed Jun 14, 2019
1 parent 76abc6a commit b5794ad
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
82 changes: 76 additions & 6 deletions packages/@aws-cdk/aws-stepfunctions/lib/states/pass.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,7 +121,7 @@ export interface PassProps {
*
* @default No injected result
*/
readonly result?: {[key: string]: any} | string | number | boolean | null | any[];
readonly result?: Result;
}

/**
Expand All @@ -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);
Expand All @@ -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(),
Expand Down
33 changes: 33 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,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();
},
};

0 comments on commit b5794ad

Please sign in to comment.