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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

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

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.

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
3 participants