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

feat(cdk): allow Tokens to be encoded as lists #1144

Merged
merged 2 commits into from
Nov 12, 2018
Merged

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Nov 12, 2018

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.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

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.
packages/@aws-cdk/cdk/lib/core/tokens.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/cdk/lib/core/tokens.ts Outdated Show resolved Hide resolved
// Optimization: if we can immediately resolve this, don't bother
// registering a Token.
if (valueType === 'string' || valueType === 'number' || valueType === 'boolean') {
return this.valueOrFunction.toString();
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes. Copy pasta.

Copy link
Contributor Author

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)) {
Copy link
Contributor

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).

Copy link
Contributor Author

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.

packages/@aws-cdk/cdk/test/core/test.tokens.ts Outdated Show resolved Hide resolved
@rix0rrr rix0rrr merged commit cd7947c into master Nov 12, 2018
@rix0rrr rix0rrr deleted the huijbers/list-tokens branch November 12, 2018 13:00
eladb pushed a commit that referenced this pull request Jan 8, 2019
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`.
@NGL321 NGL321 added the contribution/core This is a PR that came from AWS. label Sep 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants