-
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
(aws-cdk-lib/core): private->protected for CfnMapping.mapping and more #30811
Comments
Needs discussion with the team. |
In general, I don't see this as a direction we want to go but I'd like to better understand the use case for this.
We define stability at the module level. Breaking encapsulation here and declaring that the implementation details of that broken encapsulation isn't stable isn't stable both breaks the model we've promised to customers to the last several years and will likely setup customers for pain down the road. |
I had a quick search for more general discussions on the topic, but so far come up mainly with voices that agree protected should generally be avoided:
It seems like there's general support to draw no distinction between consumers of public APIs (i.e. consumers of constructs for us) and developers trying to subclass & customize. My specific use-case as mentioned above was to check (at build-time but after construction) whether a particular AWS region & other key was supported by a generated CfnMapping. For now I'm working around it by simply removing the check, which is not ideal because failure would be at deploy time & take longer... But it's not worth the effort of re-implementing Closing this issue for now & I guess will re-raise a specific public API request to retrieve the info if it comes back to bite us in future. Thanks for looking into it! |
|
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one. |
Describe the feature
CfnMapping.mapping (and maybe several other props?) would be less restrictive on derived constructs if they were defined as
protected
rather thanprivate
.More generally, I suspect this is a widespread issue and a lot of constructs might benefit from loosening up private properties?
Use Case
I'm trying to derive a child class from
CfnMapping
for a specific family of mappings where key 1 is AWS region and key 2 is an enumeration specific to my application.My construct has pre-defined data for a wide variety of regions, but users can pass in an
regions
prop at construction to filter this down to keep the generated template small.I'd like to offer convenience methods on the construct that expose information about the map as initialized: For example which regions are currently supported.
I would prefer to take this information directly from the
.mapping
in case it's been modified by any other method (likeCfnMapping.setValue()
)... But because the property is defined asprivate
my child class can't read it.Today it seems like my options are either 1/ to hack around the type system to convince TS to do what I want and linters to ignore my hubris, or 2/ re-implement the base construct myself?
Proposed Solution
I suggest to make
CfnMapping.mapping
aprotected
property rather thanprivate
, and maybe open a broader discussion on whether protected or private should be preferred in general.I appreciate this could create a perceived grey area between what is and isn't "a public interface" and "a breaking change" for constructs: and propose to take the stance that protected members should be considered unstable (like private ones) and subject to breaking change in any release.
Nevertheless, defining such members as
protected
overprivate
at least gives construct authors the choice to build on them - with encouragement that pinning CDK version and/or rigorously testing on any upgrades is a best-practice.Other Information
Today, I couldn't see any explicit direction in the design guidelines on the use of
protected
vsprivate
in constructs. There's this very old issue but I couldn't parse a clear direction on the subject from it.In some less strictly-typed languages (like Python), I believe users would already be able to access these properties pretty straightforwardly.
Acknowledgements
CDK version used
2.87.0, but consistent with current main branch
Environment details (OS name and version, etc.)
macOS, projen, cdklabs-projen-project-types/cdklabs-construct-lib
The text was updated successfully, but these errors were encountered: