MCPcopy
hub / github.com/hashicorp/hcl / ExampleExpression_RenameVariablePrefix

Function ExampleExpression_RenameVariablePrefix

hclwrite/examples_test.go:77–100  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

75}
76
77func ExampleExpression_RenameVariablePrefix() {
78 src := []byte(
79 "foo = a.x + a.y * b.c\n" +
80 "bar = max(a.z, b.c)\n",
81 )
82 f, diags := hclwrite.ParseConfig(src, "", hcl.Pos{Line: 1, Column: 1})
83 if diags.HasErrors() {
84 fmt.Printf("errors: %s", diags)
85 return
86 }
87
88 // Rename references of variable "a" to "z"
89 for _, attr := range f.Body().Attributes() {
90 attr.Expr().RenameVariablePrefix(
91 []string{"a"},
92 []string{"z"},
93 )
94 }
95
96 fmt.Printf("%s", f.Bytes())
97 // Output:
98 // foo = z.x + z.y * b.c
99 // bar = max(z.z, b.c)
100}

Callers

nothing calls this directly

Calls 7

ParseConfigFunction · 0.92
HasErrorsMethod · 0.80
AttributesMethod · 0.80
RenameVariablePrefixMethod · 0.80
ExprMethod · 0.80
BodyMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected