File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -74,4 +74,23 @@ export interface IConditionExpression {
74
74
* Returns a JSON node that represents this condition expression
75
75
*/
76
76
resolve ( context : ResolveContext ) : any ;
77
+
78
+ /**
79
+ * Returns a string token representation of this condition expression, which
80
+ * resolves to the CloudFormation condition JSON during synthesis.
81
+ *
82
+ * You can use `toString` when you wish to embed a condition expression
83
+ * in a property value that accepts a `string`. For example:
84
+ *
85
+ * ```ts
86
+ * new sqs.Queue(this, 'MyQueue', {
87
+ * queueName: Fn.conditionIf('Condition', 'Hello', 'World').toString()
88
+ * });
89
+ * ```
90
+ *
91
+ * NOTE: we need this explicitly here despite the fact that in JavaScript this would
92
+ * "just work" since conditions are eventually tokens that implement `toString`,
93
+ * in order for jsii languages like Java to proxy this to jsii.
94
+ */
95
+ toString ( ) : string ;
77
96
}
Original file line number Diff line number Diff line change @@ -28,5 +28,33 @@ export = {
28
28
{ 'Fn::Not' : [ { Condition : 'Condition3' } ] } ] } } } ) ;
29
29
30
30
test . done ( ) ;
31
+ } ,
32
+
33
+ 'condition expressions can be embedded as strings' ( test : Test ) {
34
+ // GIVEN
35
+ const stack = new cdk . Stack ( ) ;
36
+ const propValue : string = cdk . Fn . conditionIf ( 'Cond' , 'A' , 'B' ) . toString ( ) ;
37
+
38
+ // WHEN
39
+ new cdk . Resource ( stack , 'MyResource' , {
40
+ type : 'AWS::Foo::Bar' ,
41
+ properties : {
42
+ StringProp : propValue
43
+ }
44
+ } ) ;
45
+
46
+ // THEN
47
+ test . ok ( cdk . unresolved ( propValue ) ) ;
48
+ test . deepEqual ( stack . toCloudFormation ( ) , {
49
+ Resources : {
50
+ MyResource : {
51
+ Type : 'AWS::Foo::Bar' ,
52
+ Properties : {
53
+ StringProp : { 'Fn::If' : [ 'Cond' , 'A' , 'B' ] }
54
+ }
55
+ }
56
+ }
57
+ } ) ;
58
+ test . done ( ) ;
31
59
}
32
60
} ;
You can’t perform that action at this time.
0 commit comments