-
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(tokens): enable type coercion #2680
Conversation
relax restrictions on input types for Token.toXxx in order to allow flexible type coercion. this may be needed in situations where users want to force a token typed as one type to be represented as another type and generally allow tokens to be used as "type-system escape hatches". Previously, this did not work: const port = new Token({ "Fn::GetAtt": [ "ResourceId", "Port" ] }).toString(); new TcpPort(new Token(port).toNumber()); Also, this did not work: const port = new Token({ "Fn::GetAtt": [ "ResourceId", "Port" ]}).toNumber(); Fixes #2679
you need to instantiate a CfnReference instead of a Token. |
That was just an example. The point is that I think we don’t need to be defensive in Token.toXxxx methods. If you’ve reached the point where you actually need a token, you basically indicate to the framework that “you know what you are doing”. It’s a sort of an “as any” at the framework level. |
In my mind, there are 2 types of Tokens, both doing some kind of escape mechanism, but they're not the same escape mechanism:
I want to use different classes to represent different kinds of tokens, and so the code you wrote up there could not be written anymore ( I'm okay to merge it now if you really want to, though. |
Thanks so much for taking the time to contribute to the AWS CDK ❤️ We will shortly assign someone to review this pull request and help get it
|
Relax restrictions on input types for Token.toXxx in order to allow flexible type coercion.
This may be needed in situations where users want to force a token typed as one type to be represented as another type and generally allow tokens to be used as "type-system escape hatches".
Previously, this did not work:
Also, this did not work:
These were just examples. The point is that if a user reached the point where you actually need a token, they basically indicate to the framework that “I know what I am are doing”. It’s a sort of an “as any” at the framework level.
Fixes #2679
Pull Request Checklist
design
folderBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.