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

fix(cfn-include): handle numbers expressed as strings in templates #9525

Merged
merged 16 commits into from
Aug 17, 2020

Conversation

comcalvi
Copy link
Contributor

@comcalvi comcalvi commented Aug 7, 2020

Closes #9524


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

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 7, 2020
@mergify
Copy link
Contributor

mergify bot commented Aug 7, 2020

Title does not follow the guidelines of Conventional Commits. Please adjust title before merge.

@comcalvi comcalvi changed the title fix(cfn-include): allow cfn-include to handle numbers expressed as strings in json templates fix(cfn-include): handle numbers expressed as strings in json templates Aug 7, 2020
Copy link
Contributor

@skinny85 skinny85 left a comment

Choose a reason for hiding this comment

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

This is a little to permissive right now.

Also, @rix0rrr already said he'd veto this change. Please add him as a reviewer as well.

@comcalvi comcalvi requested a review from skinny85 August 8, 2020 01:04
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.

As I said to Adam before, I am not okay with this, for the simple reason that we have type contracts to uphold. Both inside TypeScript as well as across our jsii language barrier.

What you are saying is that you are cool with the following:

interface CorsRule {
  // ...
  readonly maxAge: number;
}

const rule: CorsRule = readRuleFromTemplate("some-user-controlled-template.json");


console.log(rule.maxAge + 1);  // <-- Type error: cannot add a string and a number

// What do you mean maxAge is a "string"? It says "number" in the type right there!

I appreciate you are trying to roundtrip the quotation here. I have 2 responses to that:

  • Do your bookkeeping another way. Make a subclass that keeps track of whether properties have been modified, and just outputs the original unparsed string if not. Or (nasty nasty suggestion) tack a magic property onto that number you parsed to tell it to render out with quotes again.
  • Why do we even want so desperately to roundtrip those quotes? If it's only for the benefit of cdk diff, then shouldn't we be updating cdk diff to consider 10 and "10" the same value? That might even be useful in more cases than just the one you are trying to solve here.

Personally I'm leaning towards the second solution. Anything but destroying the value of our type system guarantees for --honestly-- little benefit.

@@ -218,8 +218,11 @@ export function validateString(x: any): ValidationResult {

export function validateNumber(x: any): ValidationResult {
if (canInspect(x) && typeof x !== 'number') {
return new ValidationResult(`${JSON.stringify(x)} should be a number`);
if (typeof x === 'string' && isNaN(parseInt(x))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why must it be an int? Is "3.141592654" not allowed? Why does "10" get preferential treatment?

@comcalvi comcalvi requested a review from rix0rrr August 10, 2020 20:36
@comcalvi comcalvi requested a review from skinny85 August 10, 2020 21:20
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.

Thanks Calvin! I like this much better.

Got some niggles about some comments: I'd like to see some clarification on code that's deviates from what you'd expect, so in the future I don't have to go and do code archeology when I pass by there again.

In this PR:

  • deepEqual: has a looser definition of "equality" now than you would expect from the name -- why is that?
  • getNumber: doesn't actually always return a number -- why is that?

These kinds of comments make it easier for me to decide whether this kind of unexpectedness was intentional or an oversight/bug.

Other than that, ready to ship it!

}
}

// otherwise, return the input
Copy link
Contributor

Choose a reason for hiding this comment

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

...and let the validator handle the fact that it's not the type we want.

That's a pretty important detail to understanding the behavior of the parser here. At first glance this code would be wrong, but it becomes correct in combination with some other code somewhere else.

@comcalvi comcalvi requested a review from rix0rrr August 12, 2020 18:31
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.

👍

@comcalvi comcalvi removed the request for review from skinny85 August 17, 2020 17:31
@comcalvi comcalvi requested a review from skinny85 August 17, 2020 17:31
@skinny85 skinny85 changed the title fix(cfn-include): handle numbers expressed as strings in json templates fix(cfn-include): handle numbers expressed as strings in templates Aug 17, 2020
@mergify
Copy link
Contributor

mergify bot commented Aug 17, 2020

Thank you for contributing! Your pull request will be updated from master 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: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: cc0e857
  • 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 17, 2020

Thank you for contributing! Your pull request will be updated from master 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 e9a4102 into aws:master Aug 17, 2020
misterjoshua pushed a commit to misterjoshua/aws-cdk that referenced this pull request Aug 19, 2020
…ws#9525)

Closes aws#9524

----

*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
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cfn-Include should accept strings where numbers are expected
4 participants