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

Support full reconstruction of HCL from output dictionary #177

Conversation

weaversam8
Copy link
Contributor

This is a follow up to #169, to more fully address the use case discussed in #23.

Along the way, I also found and fixed #171, #172, and #173. (To view those changes in isolation, please look at the diffs of the commits linked to those issues specifically.)

I also believe that this supersedes @zabacad's PR #143 to support nested interpolation. (Sorry Ian! I didn't see your PR and recreated your work - but we landed on similar approaches!)

I'm submitting this PR as a draft right now, because while this is technically working, there are a few more things I'd like to add. Namely:

  • Better documentation about how to use this feature
  • Better whitespace and formatting embedded in the AST generated from the dictionary
    • (this draft implementation produces syntactically equivalent output but it's very ugly)
  • Helper functions to control the formatting of the generated output by embedding metadata in the Python dictionary
  • Better error messages when parsing an invalid dictionary
  • More tests for all of the above.

This PR adds:

Still a draft PR, as described above, but open to hear any comments or suggestions!

@weaversam8 weaversam8 force-pushed the feature/hcl2-reverse-transformer branch from a28991c to 8652de4 Compare October 24, 2024 14:34
@weaversam8
Copy link
Contributor Author

OK, I think this is ready for a proper review. Since the draft, I added:

  • More extensive documentation on how to use these features
  • A rudimentary hcl2.Builder class allowing users to create fresh HCL documents from Python (the reason why this is necessary is described in the docs I added)
  • Better formatting defaults on output from hcl2.reverse_transform(...).
  • Tests for the hcl2.Builder class.

@weaversam8 weaversam8 marked this pull request as ready for review October 24, 2024 15:09
@weaversam8 weaversam8 requested a review from a team as a code owner October 24, 2024 15:09
@kkozik-amplify kkozik-amplify self-assigned this Oct 25, 2024
@weaversam8
Copy link
Contributor Author

weaversam8 commented Nov 4, 2024

@kkozik-amplify - still reviewing this? any questions I can answer about my changes?

@kkozik-amplify
Copy link
Collaborator

kkozik-amplify commented Nov 5, 2024

@weaversam8 Yeah, need some more time. In the meantime - could you look into the errors that Codacy Analysis PR check reported?

@Nfsaavedra
Copy link

Hi!

While trying to use the code in this branch I found that when parsing the following file:

locals {
  tags = {
    application = "Modernisation Platform: ${terraform.workspace}"
  }
}

The program gets stuck in a regex match. I am almost sure it is related to the changes to better support string interpolation (that's why I'm using this branch).

hcl2/hcl2.lark Outdated
Comment on lines 59 to 60
STRING_LIT : "\"" (STRING_CHARS)* "\""
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/+ // any character except '"' unless inside a interpolation string
Copy link

@Nfsaavedra Nfsaavedra Nov 15, 2024

Choose a reason for hiding this comment

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

Suggested change
STRING_LIT : "\"" (STRING_CHARS)* "\""
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/+ // any character except '"' unless inside a interpolation string
STRING_LIT : "\"" STRING_CHARS? "\""
STRING_CHARS : /(?:(?!\${)([^"\\]|\\.))+/ // any character except '"'

This seems to fix the problem, but I did not run the tests to check it. It does parse this file correctly tho.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Nfsaavedra Hey! Thanks for the suggestion. Since it's been some time already, I added your fix to the PR myself, all tests are passing.

@guilinden
Copy link

@weaversam8 any plans when this will be merged?

@kkozik-amplify kkozik-amplify force-pushed the feature/hcl2-reverse-transformer branch from 2c6d36a to d9639af Compare January 14, 2025 13:14
@kkozik-amplify kkozik-amplify force-pushed the feature/hcl2-reverse-transformer branch from a367ac2 to 993ef6b Compare January 15, 2025 14:39
Copy link

@Nfsaavedra Nfsaavedra left a comment

Choose a reason for hiding this comment

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

Based on my understanding of the code, LGTM. Do the new tests consider the case where the previous version of the program would get stuck as described here? It's important to avoid regressions. If so, then I have nothing to add :)

@kkozik-amplify kkozik-amplify force-pushed the feature/hcl2-reverse-transformer branch from 880ad1c to 9e49e8a Compare January 16, 2025 13:40
@@ -1,3 +1,3 @@
locals {
embedded_interpolation = "${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo"
embedded_interpolation = "(long substring without interpolation); ${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo"
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Nfsaavedra I updated this test case to take issue you found into consideration.
It seems like the original regex would take longer to match non-interpolated string with each character, to the point where for 20 characters it would basically get stuck.

Choose a reason for hiding this comment

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

Makes sense! :)

@kkozik-amplify kkozik-amplify merged commit f8a2c88 into amplify-education:main Jan 16, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terraform escape sequences are not processed when parsing strings
5 participants