-
Notifications
You must be signed in to change notification settings - Fork 595
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
hclwrite: add support for tuple,nested object and function call #502
hclwrite: add support for tuple,nested object and function call #502
Conversation
Hi @incubator4! Thanks for working on this. It does seem reasonable to me to expand the set of functions we have to help construct common patterns of tokens. An interesting thing about tuple constructor and object constructor expressions (which is what we call the With those in place, think Given that, I'd like to simplify this a little and also refine some of the terminology to match how we talk about these constructs in the relevant section of the native syntax spec:
With the above signatures I think this can cover anything that could be achieved using the Static List and Static Map operations, which calling applications can use in order to inspect the static syntax directly rather than producing a real value. Terraform's // Might generate the following, assuming particular values for
// callerProviderConfig and calleeProviderConfig:
//
// providers = {
// aws.foo = aws.bar
// }
body.SetAttributeRaw("providers", hclwrite.TokensForObject([]hclwrite.ObjectAttrTokens{
{
Name: hclwrite.TokensForTraversal(callerProviderConfig),
Value: hclwrite.TokensForTraversal(calleeProviderConfig),
},
})) I added (The object vs. map terminology and identifier vs. keyword terminology is a bit funny here in the spec because there's a blend of syntax-specific terminology vs. the syntax-agnostic infoset terms which the native syntax spec is refining. That inconsistency unfortunately does end up showing up in the API here too, but I think it's better for the API to be consistent with the spec even if the spec itself is a little inconsistent in its terminology. I hope all of that seems reasonable! Aside from the API feedback, my only other main feedback here would be to make sure that the tokens generated for tuple and object constructors here follow the same newline-inclusion conventions as the corresponding behavior for tuples and objects in Thanks again! If you feel adventurous here it might be nice to complete the set of static-analysis-supporting constructs (which are the situations where
I'm imagining // Should generate:
// type = list(string)
body.SetAttributeRaw("type", hclwrite.TokensForFunctionCall(
"list",
hclwrite.TokensForIdentifier("string"),
) I don't mean to suggest that you must add this in order for this PR to be accepted, since of course we can always add new functionality like this later, but I wanted to mention it because it seems like you have an underlying Terraform-specific use-case and so maybe it would help you to have support for the full repertiore of HCL static analysis features that the Terraform language relies on. One day I would like to also have a similar set of functions in the family that currently includes and |
@apparentlymart I finished most of the changes except |
The "TokensFor..." family of functions all aim to construct valid raw token sequences representing particular syntax constructs. Previously we had only "leaf" functions TokensForValue and TokensForTraversal, but nothing to help with constructing compound structures. Here we add TokensForTuple, TokensForObject, and TokensForFunctionCall which together cover all of the constructs that HCL allows static analysis of, and thus constructs where it's likely that someone would want to generate an expression that is interpreted purely by its syntax and not resolved into a value. What all of these have in common is that they take other Tokens values as arguments and include them verbatim as part of their result, with the caller being responsible for making sure these smaller units are themselves valid expression tokens. This also adds TokensForIdentifier as a convenient shorthand for generating single-identifier tokens, which is particularly useful for populating the attribute names in TokensForObject.
fdcfd19
to
962b970
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this, @incubator4!
I made some small amendments to your commits to add a few more test cases and some longer docstring comments to the new functions, just because it seemed more efficient than doing another code review round-trip, but this looks great otherwise, and I'm going to merge it now for inclusion in the next HCL release.
(We don't really have a regular release schedule for HCL, so I can't promise it'll be tagged at any particular time in the future, but we typically create a new tag after there are a few new features accumulated, or when there's an important bug to fix. Until then, you should be able to use HCL from the main
branch to get this new functionality immediately.)
Add several function for
SetAttributeRaw()
enhancement, which include ways transferTraverse
toTokens
TokensForListTraversal
support for listtraversal
, mentioned in #347struct like this
and got
TokensForListTokens
for mixedtraversal
value and cty value, trans all traversal and cty value intoTokens
, then create a list.struct like this
and got
TokensForObject
for nest struct of any value, including ctx value,traversal
, listtraversal
, even another objectand got