-
Notifications
You must be signed in to change notification settings - Fork 606
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
How does hclwrite
remove objectelem
inside a collectionValue
of an attribute
#695
Comments
Updated: I finally manage to do this via the following, though apparently not ideal: package main
import (
"fmt"
"log"
"slices"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)
func main() {
f, diags := hclwrite.ParseConfig([]byte(`
foo = {
bar = {
}
baz = "xxx"
}
`), "main.hcl", hcl.InitialPos)
if diags.HasErrors() {
log.Fatal(diags.Error())
}
foo := f.Body().Attributes()["foo"]
tks, diags := removeExpressionAttributes(foo.Expr(), "bar")
if diags.HasErrors() {
log.Fatal(diags.Error())
}
f.Body().SetAttributeRaw("foo", tks)
fmt.Println(string(f.Bytes()))
}
func removeExpressionAttributes(expr *hclwrite.Expression, attributes ...string) (hclwrite.Tokens, hcl.Diagnostics) {
tks := expr.BuildTokens(hclwrite.TokensForIdentifier("tmp"))
ftmp, diags := hclwrite.ParseConfig(tks.Bytes(), "tmp", hcl.InitialPos)
if diags.HasErrors() {
return nil, diags
}
body := ftmp.Body().Blocks()[0].Body()
bodyAttributes := body.Attributes()
var objectAttrTokens []hclwrite.ObjectAttrTokens
for attrName, attr := range bodyAttributes {
if slices.Contains(attributes, attrName) {
continue
}
objectAttrTokens = append(objectAttrTokens, hclwrite.ObjectAttrTokens{
Name: hclwrite.TokensForIdentifier(attrName),
Value: attr.Expr().BuildTokens(nil),
})
}
return hclwrite.TokensForObject(objectAttrTokens), nil
} Any idea about how to do this idiomatically would be appreciated! |
Hi @magodo, Unfortunately the Directly fiddling with tokens is, unfortunately, probably the only available answer right now. If I recall correctly, the rudimentary expression-reformatting rules in The following is some assorted context I'm leaving here in case it's useful to someone who might want to try to build out a more substantial Right now a For example, consider an expression like
The The way I'd expected this to evolve in future was that the expression parser would type-assert its Earlier work already established that function call expressions, object constructor expressions, and tuple constructor expressions are worthy of special support when we added the interim However, the "loose end" in the current design is exactly what API would make sense for interacting with these specialized forms of For the use-case given in this issue, at a high-level I'd expect to be able to:
Perhaps then there would be That design would get pretty chaotic if the desired end state were to offer an I don't have the time or motivation right now to work on any of this myself, but I hope the above is useful to someone else who might be interested in experimenting. Note that I'm not an HCL maintainer anymore, so if someone does want to work on this I'd suggest discussing your plans with the HCL maintainers first to make sure that such a contribution would be welcomed. |
I have a piece of code using
hclwrite
to modify some Terraform code, in that it will conditionally remove attributes/blocks. The question raises when I encounter, e.g. the following HCL (regarded as a TF attribute):(The above code is not prevalent in SDKv2 based providers, but will prevail for FW based ones as attributes are preferred to blocks)
I don't know how could I, e.g., remove the
bar
inside thefoo
attribute's expression (i.e. thecollectionValue
).I've tried with something similar to below, but doesn't output what I expected:
The text was updated successfully, but these errors were encountered: