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(core): allow embedding condition expression as strings #2007

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this instead have extended some IToken interface? I wonder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#79

}
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();
}
};