-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
feat(cdk): allow Tokens to be encoded as lists #1144
Conversation
Add the ability for Tokens to be encoded into lists using the `token.toList()` method. This is useful for Tokens that intrinsically represent lists of strings, so we can pass them around in the type system as `string[]` (and interchange them with literal `string[]` values). The encoding is reversible just like string encodings are reversible. Contrary to strings encodings, concatenation operations are not allowed (they cannot be applied after decoding since CloudFormation does not have any list concatenation operators). This change does not change any CloudFormation resources yet to take advantage of the new encoding. Implements the most important remaining part of #744.
// Optimization: if we can immediately resolve this, don't bother | ||
// registering a Token. | ||
if (valueType === 'string' || valueType === 'number' || valueType === 'boolean') { | ||
return this.valueOrFunction.toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will return a string
not string[]
. Shouldn't that be an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes. Copy pasta.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't understand why TSC doesn't complain about that :(
// | ||
// arrays - resolve all values, remove undefined and remove empty arrays | ||
// | ||
|
||
if (Array.isArray(obj)) { | ||
if (containsListToken(obj)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something is odd here... Isn't a list token always size=1? Why are we checking if the array "contains a list token"? Is this for the use case where an array contains other arrays as elements (that is solved in the recursion).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the issue is to detect the case where someone has done:
const xs = token.toList();
xs.push('hello');
// Or
const xs = ['hello'].concat(token.toList());
In that case, we want to detect that the token as constructed was meant to be a Token but is now illegal because of the concatenation.
Now that we can represent attributes as native strings and string lists, this covers the majority of resource attributes in CloudFormation. This change: * String attributes are represented as `string` (like before). * String list attribute are now represented as `string[]`. * Any other attribute types are represented as `cdk.Token`. Attributes that are represented as Tokens as of this change: * amazonmq has a bunch of `Integer` attributes (will be solved by #1455) * iot1click has a bunch of `Boolean` attributes * cloudformation has a JSON attribute * That's all. A few improvements to cfn2ts: * Auto-detect cfn2ts scope from package.json so it is more self-contained and doesn't rely on cdk-build-tools to run. * Added a "cfn2ts" npm script to all modules so it is now possible to regenerate all L1 via "lerna run cfn2ts". * Removed the premature optimization for avoiding code regeneration (it saved about 0.5ms). Fixes #1406 BREAKING CHANGE: any `CfnXxx` resource attributes that represented a list of strings are now typed as `string[]`s (via #1144). Attributes that represent strings, are still typed as `string` (#712) and all other attribute types are represented as `cdk.Token`.
Add the ability for Tokens to be encoded into lists using the
token.toList()
method.This is useful for Tokens that intrinsically represent lists of strings,
so we can pass them around in the type system as
string[]
(andinterchange them with literal
string[]
values).The encoding is reversible just like string encodings are reversible.
Contrary to strings encodings, concatenation operations are not
allowed (they cannot be applied after decoding since CloudFormation
does not have any list concatenation operators).
This change does not change any CloudFormation resources yet to
take advantage of the new encoding.
Implements the most important remaining part of #744.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.