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

chore(core): isCfnResource allows any type as input #28001

Merged
merged 4 commits into from
Nov 29, 2023

Conversation

tmokmss
Copy link
Contributor

@tmokmss tmokmss commented Nov 15, 2023

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,

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.

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:

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

@aws-cdk-automation aws-cdk-automation requested a review from a team November 15, 2023 06:05
@github-actions github-actions bot added p2 star-contributor [Pilot] contributed between 25-49 PRs to the CDK labels Nov 15, 2023
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Nov 15, 2023
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;
Copy link
Contributor

@go-to-k go-to-k Nov 15, 2023

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 });

Copy link
Contributor Author

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.

Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks!

@aws-cdk-automation aws-cdk-automation added pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Nov 15, 2023
@vinayak-kukreja vinayak-kukreja self-assigned this Nov 28, 2023
@vinayak-kukreja vinayak-kukreja added the pr/do-not-merge This PR should not be merged at this time. label Nov 28, 2023
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 28, 2023
@vinayak-kukreja vinayak-kukreja removed the pr/do-not-merge This PR should not be merged at this time. label Nov 29, 2023
Copy link
Contributor

mergify bot commented Nov 29, 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 dismissed vinayak-kukreja’s stale review November 29, 2023 01:25

Pull request has been modified.

Copy link
Contributor

mergify bot commented Nov 29, 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: 2a973e3
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@mergify mergify bot merged commit e21916d into aws:main Nov 29, 2023
9 checks passed
Copy link
Contributor

mergify bot commented Nov 29, 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).

@tmokmss tmokmss deleted the is_cfnresource_allow_undefined branch November 29, 2023 05:10
chenjane-dev pushed a commit to chenjane-dev/aws-cdk that referenced this pull request Dec 5, 2023
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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2 star-contributor [Pilot] contributed between 25-49 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants