-
Notifications
You must be signed in to change notification settings - Fork 4.3k
chore(mixins-preview): implement Mixins RFC #36013
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
Conversation
23aad97 to
83cc751
Compare
83cc751 to
c7ec67c
Compare
c7ec67c to
bdd296e
Compare
bdd296e to
22cf314
Compare
### Reason for this change Refactor repeated pattern of anonymous classes that throw `UnscopedValidationError` when accessing `node` or `env` properties. This pattern exists because certain legacy CDK APIs like `ManagedPolicy.fromAwsManagedPolicyName()` and CloudFront's `fromManagedCachePolicy()` return construct-like objects without requiring a `scope` parameter. These APIs predate modern CDK patterns where all constructs require a scope. They cannot be changed without breaking existing customer code, so they return "detached" constructs that throw errors when used in APIs that require a proper construct tree context. But we also sometimes add new features to resource interfaces like `IManagedPolicy`. When this happens, we have to update this fake implementation in a number of places. It also prevents us (and users) from providing augmentations for the `IConstruct` interface like proposed in #36013. With this change, we have reduced the number of places to update to one. It's now also possible to augment `IConstruct`. ### Description of changes - Created `DetachedConstruct` base class in `core/lib/private/detached-construct.ts` that encapsulates the pattern of throwing `UnscopedValidationError` when accessing `node` or `env` - Refactored CloudFront policy classes (`CachePolicy`, `OriginRequestPolicy`, `ResponseHeadersPolicy`) to extend `DetachedConstruct` instead of implementing the pattern inline - Refactored `ManagedPolicy.fromAwsManagedPolicyName()` to use `DetachedConstruct` - Refactored `ElasticBeanstalkDeployAction` to use `DetachedConstruct` for managed policy reference - Added unit tests for `DetachedConstruct` to verify error throwing behavior This change reduces code duplication and makes the pattern more maintainable. The `DetachedConstruct` class is marked as internal and documented to discourage use in new APIs. ### Describe any new or updated permissions being added No new or updated permissions. ### Description of how you validated changes - Added unit tests in `core/test/private/detached-construct.test.ts` that verify `UnscopedValidationError` is thrown when accessing `node` and `env` properties - Existing tests continue to pass, ensuring no behavioral changes ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
3a93231 to
94e15f9
Compare
|
➡️ PR build request submitted to A maintainer must now check the pipeline and add the |
94e15f9 to
56a98db
Compare
56a98db to
7861e58
Compare
| import type { IMixin } from './core'; | ||
| import { Mixins, ConstructSelector } from './core'; | ||
|
|
||
| declare module 'constructs' { |
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.
Does this work for consumers of this library?
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.
It does work if they import the submodule as described in the readme.
| /** | ||
| * Validates the construct before applying the mixin. | ||
| */ | ||
| validate?(construct: IConstruct): string[]; |
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.
Is this not failing the jsii build?
I also still think validate() doesn't need to be here.
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.
why should it be failing the build (it doesn't)?
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.
That might be an oversight. There is approximately 0% chance this gets translated appropriately to Java.
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.
Looks like jsii is silently ignoring it 🙈
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "Validates the construct before applying the mixin."
},
"locationInModule": {
"filename": "lib/core/mixins.ts",
"line": 33
},
"name": "validate",
"parameters": [
{
"name": "construct",
"type": {
"fqn": "constructs.IConstruct"
}
}
],
"returns": {
"type": {
"collection": {
"elementtype": {
"primitive": "string"
},
"kind": "array"
}
}
}
}7861e58 to
1c327df
Compare
1c327df to
95339b8
Compare
95339b8 to
a903824
Compare
|
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). |
|
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). |
1 similar comment
|
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). |
|
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). |
|
Comments on closed issues and PRs are hard for our team to see. |
Reason for this change
This PR implements the foundational infrastructure for the CDK Mixins framework, introducing a composable abstraction system for applying functionality to CDK constructs. It is based on the current state of the RFC.
While the RFC is not yet approved and finalized, this PR aims to implement it including all its flaws so we can move forward with other implementing depending on this. We will update the package as the RFC evolves.
Description of changes
Core Framework:
IMixininterface andMixinbase class for creating composable abstractionsMixins.of()API for applying mixins to constructs withapply()andmustApply()methodsConstructSelectorfor filtering constructs by type, ID pattern, or CloudFormation resource type.with()augmentation to constructs for fluent mixin applicationTesting:
Documentation:
Description of how you validated changes
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license