-
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
chore(core): isCfnResource allows any type as input #28001
Conversation
public static isCfnResource(construct: IConstruct): construct is CfnResource { | ||
return (construct as any).cfnResourceType !== undefined; | ||
public static isCfnResource(x: any): x is CfnResource { | ||
return x !== null && typeof(x) === 'object' && x.cfnResourceType !== undefined; |
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.
Other similar isXxx methods check by Symbol
, is there any reason not to do the following? I thought it would work as it is more safe and also keeps the consistent signatures.
Because the cfnResourceType
can be considered unique property?
const CFN_RESOURCE_SYMBOL = Symbol.for('@aws-cdk/core.CfnResource');
// ...
public static isCfnResource(x: any): x is CfnResource {
return x !== null && typeof(x) === 'object' && CFN_RESOURCE_SYMBOL in x;
}
// ...
constructor(scope: Construct, id: string, props: CfnResourceProps) {
// ...
Object.defineProperty(this, CFN_RESOURCE_SYMBOL, { value: true });
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.
Thanks. I'd like to keep this as it is to prevent from possible breaking change unless there is a strong reason to do so.
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.
I see. Thanks!
public static isCfnResource(construct: IConstruct): construct is CfnResource { | ||
return (construct as any).cfnResourceType !== undefined; | ||
public static isCfnResource(x: any): x is CfnResource { | ||
return x !== null && typeof(x) === 'object' && x.cfnResourceType !== undefined; |
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.
I see. Thanks!
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). |
Pull request has been modified.
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 CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
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). |
It is more or less frustrating that we have to check if a variable is undefined or not before calling the `CfnResource.isCfnResource` method. For example, ```ts const bucket1 = new Bucket(stack, 'Bucket1'); const bucket1Resource = bucket1.node.defaultChild; if (bucket1Resource !== undefined && // Currently we need this! cdk.CfnResource.isCfnResource(bucket1Resource) ) { bucket1Resource.addDependency(...); } ``` With this PR, `isCfnResource` now accepts `any` type as input and performs the necessary validations inside. ```ts const bucket1 = new Bucket(stack, 'Bucket1'); const bucket1Resource = bucket1.node.defaultChild; if (cdk.CfnResource.isCfnResource(bucket1Resource)) { // much smoother bucket1Resource.addDependency(...); } ``` Actually, other `isXxx` methods have consistent signatures like the one below: ```ts public static isStack(x: any): x is Stack public static isReference(x: any): x is Reference public static isCfnElement(x: any): x is CfnElement // and more... ``` This change also makes the `isCfnResource` consistent with these signatures. Note that this is not a breaking change, because the input constraint is relaxed, not tightened, so all the old code will work without change. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
It is more or less frustrating that we have to check if a variable is undefined or not before calling the
CfnResource.isCfnResource
method. For example,With this PR,
isCfnResource
now acceptsany
type as input and performs the necessary validations inside.Actually, other
isXxx
methods have consistent signatures like the one below:This change also makes the
isCfnResource
consistent with these signatures.Note that this is not a breaking change, because the input constraint is relaxed, not tightened, so all the old code will work without change.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license