-
Notifications
You must be signed in to change notification settings - Fork 604
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 support for a list of traversals #347
Comments
@apparentlymart can you suggest how to best accomplish this? |
Hi @rifelpet, Unfortunately right now the The intent here was to eventually expand the API for package main
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)
func main() {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
resBlock := rootBody.AppendNewBlock("resource", []string{"aws_route53_record", "foo"})
resBody := resBlock.Body()
traversal := hcl.Traversal{
hcl.TraverseRoot{Name: "local"},
hcl.TraverseAttr{Name: "name"},
}
resBody.SetAttributeTraversal("name", traversal)
recordsExpr := hclwrite.NewExpressionTuple(
hclwrite.NewExpressionAbsTraversal(hcl.Traversal{
hcl.TraverseRoot{Name: "local"},
hcl.TraverseAttr{Name: "foo"},
}),
hclwrite.NewExpressionAbsTraversal(hcl.Traversal{
hcl.TraverseRoot{Name: "local"},
hcl.TraverseAttr{Name: "bar"},
}),
)
resBody.SetAttributeExpr(recordsExpr)
fmt.Printf("%s\n", f.Bytes())
} If memory serves (I'm afraid it's been a while) the tricky thing here was in robustly managing the attaching and detaching of AST nodes. We started with In the long run, I'd hoped to implement a model somewhat like an HTML/XML DOM where it's possible for leaf nodes to start their lives detached and then be attached into a document by a subsequent call. That requires additional state internally so that the nodes can know what they are attached to (and whether they are attached to anything at all), which is there in part but I think not 100% complete in the current implementation. |
@apparentlymart I truly appreciate the time and effort you put into your responses to these issues. They're always in-depth and extremely informative. I've learned a lot by just reading your responses. Thanks a bunch. |
Thank you for the detailed response! I'll go ahead and use |
Hi, I'm working on writing some terraform configuration using
hclwrite
and I'm wondering how to best define an attribute's value as a list of traversals. I see examples for lists viacty.Value
andSetAttributeValue
but nothing for traversals. Is it possible withSetAttributeTraversal
or do I need to resort toSetAttributeRaw
?Heres a simplified version of what I have:
HCL Template
What I'm trying to achieve is something like:
I don't see any examples in the docs or any test cases that do this, so any example code for this would be appreciated. thanks!
The text was updated successfully, but these errors were encountered: