-
Notifications
You must be signed in to change notification settings - Fork 601
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
Implement HEREDOC generation in hclwrite #540
base: main
Are you sure you want to change the base?
Conversation
Currently, the hclwrite generator has a TODO on rendering HEREDOC. This patch will format multi-line string ending in a newline as a HEREDOC instead.
Hi @m4dcoder! Thanks for working on this. It looks like a great start! I believe I was that one that left that TODO in previous work, and I think I had the following concerns about robustly writing out an idiomatic heredoc:
These additional requirements do make the generation problem a little more "finicky" than what you did here, but hopefully not prohibitively so. Changes to what Would you be interested in trying for extending this to meet the above goals? One minor note I saw on first read, aside from the extra requirements, is that I think the expected token type for the heredoc end marker is |
@apparentlymart I can extend the work here to include these additional requirements you mentioned. I think I understand the first two points. Can you provide an example here on your last point regarding indenting extra two spaces? |
Hi @m4dcoder! Sorry for not being clearer about what I meant with that last point. Let's consider the example input string example = <<EOH
hello
world
EOH I think the more idiomatic way to write that would be as follows: example = <<-EOT
hello
world
EOT That extra Now that I've written this out I think I've remembered why I didn't implement this on the first pass: the codepath that deals with automatic indentation and horizontal alignment currently avoids touching literal template content at all, and so whatever whitespace is in the raw tokens between At the time I wasn't feeling sure about how best to address this, and honestly I'm still not feeling certain about it. One way forward would be to change the auto-formatter so that it treats the indented heredoc syntax ( I think if I were to go attack this now I would probably try for the following strategy:
I want to be up front that I don't feel sure that this path is viable -- there may be some other assumptions in the parser that make it impossible for us to generate I understand that this is now probably a lot more work than you had in mind when you submitted this PR, so of course I'll totally understand if you'd rather not wander down this unknown road that isn't certain to yield a viable outcome. The uncertainty here is, I think, why I left this |
The implementation of |
Hi @m4dcoder! I understand that this is more work than you had anticipated doing here, and that is totally reasonable. I don't think we want to support multiple optional different ways of generating the same data here, because the goal of the functions we are talking about is to produce a good default compromise serialization of a value. For more fine control we have the capability to insert tokens directly using methods like While I would like to split the change to use The one compromise that comes to my mind is that if you were to work on the rest of the changes I could potentially sign up to finish the indented heredoc mode before we merge it. However, unfortunately my focus is currently elsewhere and so I can't commit to when I'd be available to work on that, and so this compromise may involve your willingness to use HCL from the branch associated with this PR, rather than from a tagged release, until I can devote enough time to remind myself how the formatter works enough to implement that part of the solution I described; at the moment I can't say when that will be. Are you intending to use HCL in a situation where it would be viable to select an untagged commit as your HCL version in your |
Is there any progress on this PR? Generating heredoc is becoming more important every day as terraform use cases expand. Especially in the use cases that using terraform to maintain SaaS. However, there is still no way to generate heredoc and we feels unhappy with this situation. We know done is better than perfect, but we also know breaking changes are very horrible/cofusing things. But if we have a small way to generate heredoc as progressive improvement, we'll be very happy. |
Currently, the hclwrite generator has a TODO on rendering HEREDOC. This patch will format multi-line string ending in a newline as a HEREDOC instead.