Skip to content

Commit

Permalink
fix(core): allow embedding condition expression as strings (#2007)
Browse files Browse the repository at this point in the history
Expose the underlying `toString` in the `IConditionExpression` interface
so they can be embedded as string values in jsii languages.

Fixes #1984
  • Loading branch information
Elad Ben-Israel authored Mar 13, 2019
1 parent 946b444 commit 6afa87f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/@aws-cdk/cdk/lib/cloudformation/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,23 @@ export interface IConditionExpression {
* Returns a JSON node that represents this condition expression
*/
resolve(context: ResolveContext): any;

/**
* Returns a string token representation of this condition expression, which
* resolves to the CloudFormation condition JSON during synthesis.
*
* You can use `toString` when you wish to embed a condition expression
* in a property value that accepts a `string`. For example:
*
* ```ts
* new sqs.Queue(this, 'MyQueue', {
* queueName: Fn.conditionIf('Condition', 'Hello', 'World').toString()
* });
* ```
*
* NOTE: we need this explicitly here despite the fact that in JavaScript this would
* "just work" since conditions are eventually tokens that implement `toString`,
* in order for jsii languages like Java to proxy this to jsii.
*/
toString(): string;
}
28 changes: 28 additions & 0 deletions packages/@aws-cdk/cdk/test/cloudformation/test.condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,33 @@ export = {
{ 'Fn::Not': [ { Condition: 'Condition3' } ] } ] } } });

test.done();
},

'condition expressions can be embedded as strings'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const propValue: string = cdk.Fn.conditionIf('Cond', 'A', 'B').toString();

// WHEN
new cdk.Resource(stack, 'MyResource', {
type: 'AWS::Foo::Bar',
properties: {
StringProp: propValue
}
});

// THEN
test.ok(cdk.unresolved(propValue));
test.deepEqual(stack.toCloudFormation(), {
Resources: {
MyResource: {
Type: 'AWS::Foo::Bar',
Properties: {
StringProp: { 'Fn::If': [ 'Cond', 'A', 'B' ] }
}
}
}
});
test.done();
}
};

0 comments on commit 6afa87f

Please sign in to comment.