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

refactor(core): refactor CloudFormationLang.toJSON() #11224

Merged
merged 8 commits into from
Mar 10, 2021

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Oct 30, 2020

Our previous implementation of toJSON() was quite hacky.

It replaced values inside the structure with objects that had a custom
toJSON() serializer, and then called JSON.stringify() on the result.

The resulting JSON would have special markers in it where the Token
values would be string-substituted back in.

It's actually easier and gives us more control to just implement
JSONification ourselves in a Token-aware recursive function.

This change has been split off from a larger, upcoming PR in order
to make the individual reviews smaller.

Incidentally also fixes #13465, as the type of encoded tokens is assumed to match
the type of the encoded value (e.g., a string[]-encoded token is assumed to
produce a list at deploy-time and so will not be quoted).


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

@rix0rrr rix0rrr requested a review from a team October 30, 2020 16:26
@rix0rrr rix0rrr self-assigned this Oct 30, 2020
@gitpod-io
Copy link

gitpod-io bot commented Oct 30, 2020

Our previous implementation of `toJSON()` was quite hacky.

It replaced values inside the structure with objects that had a custom
`toJSON()` serializer, and then called `JSON.stringify()` on the result.

The resulting JSON would have special markers in it where the Token
values would be string-substituted back in.

It's actually easier and gives us more control to just implement
JSONification ourselves in a Token-aware recursive function.

This change has been split off from a larger, upcoming PR in order
to make the individual reviews smaller.
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 30, 2020
@eladb eladb added the pr/do-not-merge This PR should not be merged at this time. label Nov 1, 2020
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Mar 9, 2021
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

A few questions

[key, value] = value;
}

recurse(key);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a case where key is not a string? Should we be defensive against it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Object.entries() will always and only return strings.

// Treat as an intrinsic if this LOOKS like a CFN intrinsic (`{ Ref: ... }`)
// AND it's the result of a token resolution. Otherwise, we just treat this
// value as a regular old JSON object (that happens to look a lot like an intrinsic).
if (isIntrinsic(obj) && resolvedTypeHint(obj)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

doesn't isIntrinsic() impliy resolvedTypeHint() always returns true? Maybe just assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can have type hints on things that aren't intrinsics (for example, as returned by a lazy).

We can also have structures that LOOK like intrinsics but shouldn't be treated as such (for example, the literal structure { Ref: X })

Copy link
Contributor

Choose a reason for hiding this comment

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

We can have type hints on things that aren't intrinsics (for example, as returned by a lazy).

This conditional won't cover non-intrinsics (it's an &&).

We can also have structures that LOOK like intrinsics but shouldn't be treated as such (for example, the literal structure { Ref: X })

Shouldn't isIntrinsic() cover this?

}

/**
* Quote string literals inside an intrinsic
Copy link
Contributor

Choose a reason for hiding this comment

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

I still feel I don't understand why this is needed. Maybe some examples? Why isn't it possible to just use JSON.stringify() of the intrinsic ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will add more realistic tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your intuition was correct though. I missed some edge cases. After thinking about it some more, just a straight up recursion over all strings should also do it. Added more tests to confirm.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -232,22 +292,47 @@ nodeunitShim({
},
},
});
});

test('Intrinsics can occur in key position', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Very cool!

@rix0rrr rix0rrr removed the pr/do-not-merge This PR should not be merged at this time. label Mar 10, 2021
@mergify
Copy link
Contributor

mergify bot commented Mar 10, 2021

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).

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

Shipit!

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: fc942e4
  • 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 Mar 10, 2021

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).

This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(core): Templating error on custom-resource introduced with version 1.92
3 participants