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(core): Fn.findInMap supports default value #26543

Merged
merged 18 commits into from
Aug 8, 2023

Conversation

scanlonp
Copy link
Contributor

@scanlonp scanlonp commented Jul 27, 2023

Cloudformation recently added support for a default value in the FindInMap intrinsic function. This adds that support.

Closes #26125.


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

@github-actions github-actions bot added the p2 label Jul 27, 2023
@aws-cdk-automation aws-cdk-automation requested a review from a team July 27, 2023 21:18
@scanlonp scanlonp changed the title feat(core): Fn.find_in feat(core): Fn.find_in_map supports Default Value Jul 27, 2023
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jul 27, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1 and removed p2 labels Jul 27, 2023
@scanlonp scanlonp added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Jul 27, 2023
@scanlonp scanlonp marked this pull request as draft July 27, 2023 21:28
@aws-cdk-automation aws-cdk-automation dismissed their stale review July 27, 2023 22:13

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@scanlonp scanlonp marked this pull request as ready for review July 27, 2023 22:13
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jul 27, 2023
Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

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

This one should have an integration test!

Also if you read the announcement carefully, you'll notice it mentions the Language Extension Transform. What do you make of that in regards to this PR?

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jul 27, 2023
@scanlonp scanlonp marked this pull request as draft August 1, 2023 23:15
@scanlonp scanlonp removed the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Aug 3, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation dismissed their stale review August 3, 2023 19:24

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@scanlonp scanlonp marked this pull request as ready for review August 3, 2023 20:24
Copy link
Contributor Author

@scanlonp scanlonp left a comment

Choose a reason for hiding this comment

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

I had a couple of lingering questions about comment guidelines and where code should live. I feel confident with the PR as is, but wanted to get feedback on those points in particular.

@@ -1058,6 +1058,28 @@ declare const regionTable: CfnMapping;
regionTable.findInMap(Aws.REGION, 'regionName');
```

An optional default value can also be passed to `findInMap`. If either key is not found in the map and the mapping is lazy, `findInMap` will return the default value. If the mapping is not lazy or either key is an unresolved token, the call to `findInMap` will return a token that resolves to `{ "Fn::FindInMap": [ "MapName", "TopLevelKey", "SecondLevelKey", { "DefaultValue": "DefaultValue" } ] }`. Note that the `AWS::LanguageExtentions` transform is added to enable the default value functionality.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the last line necessary?

packages/aws-cdk-lib/core/lib/cfn-fn.ts Show resolved Hide resolved
Comment on lines 236 to 237
* Prefer to use `CfnMapping.findInMap`.
* Warning: do not use with lazy maps.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this how we like to convey warnings / best practices?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should explain succinctly why you should not use with lazy maps

packages/aws-cdk-lib/core/lib/cfn-fn.ts Show resolved Hide resolved
@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Aug 4, 2023
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Nice! I've got some detail notes, but conditionally approved. Do with my comments as you will and feel free to remove the pr/do-not-merge label yourself when you are feel you have addressed them to the degree that you want to 😉.

One final thing: in the title, you're describing the feature using the Python casing Fn.find_in_map. In TypeScript the function is called Fn.findInMap, and we usually use that casing to describe changes.

packages/aws-cdk-lib/core/lib/cfn-fn.ts Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/cfn-fn.ts Outdated Show resolved Hide resolved
@@ -63,30 +63,41 @@ export class CfnMapping extends CfnRefElement {
}

/**
* @returns A reference to a value in the map based on the two keys.
* @returns A reference to a value in the map based on the two keys. If mapping is lazy, the value from the map or default value is returned instead of the reference.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure "lazy" means: only render the actual mapping to the template if there is at least one value that actually needs to be looked up from it at deploy time.

Conversely: if no value is ever read from the map, or all values can be resolved at synth time, then there is no need to render the map.

The line of documentation you added here doesn't help me understand that :).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Felt that it wasn't accurate to say it returns a reference is sometimes it just passes a value. Also could help debug if the short circuit was not documented. Can add a line about the mapping behavior too

*/
public findInMap(key1: string, key2: string): string {
public findInMap(key1: string, key2: string, defaultValue?: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ohmigod. Can this method be simplified if we reorganize it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would be glad to, but not seeing a clear way. Think we ship it and someone enterprising can tidy it up later.

@rix0rrr rix0rrr changed the title feat(core): Fn.find_in_map supports Default Value feat(core): Fn.findInMap supports default value Aug 4, 2023
Copy link
Contributor

@kaizencc kaizencc left a comment

Choose a reason for hiding this comment

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

Have a few minor nits of my own lmao. Feel free to remove do-not-merge when you address the one comment

packages/aws-cdk-lib/core/README.md Outdated Show resolved Hide resolved
Comment on lines 236 to 237
* Prefer to use `CfnMapping.findInMap`.
* Warning: do not use with lazy maps.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should explain succinctly why you should not use with lazy maps

packages/aws-cdk-lib/core/lib/cfn-fn.ts Show resolved Hide resolved
@scanlonp scanlonp removed the pr/do-not-merge This PR should not be merged at this time. label Aug 7, 2023
@mergify
Copy link
Contributor

mergify bot commented Aug 8, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: b3fc6ae
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Aug 8, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 8526feb into aws:main Aug 8, 2023
8 checks passed
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. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

core: Fn.findInMap Should Support Default Value
5 participants