hcl/hclwrite/examples_test.go
Martin Atkins bafa0c5ace hcl/hclsyntax: Accept single-line block definitions
This relaxes our previous spec to include a special form from HCL 1:

    foo { bar = baz }

Although we normally require each argument to be on a line of its own, as
a special case we allow a block to be defined with a single nested
argument all on one line.

Only one nested argument definition is allowed, and a nested block
definition like "foo { bar {} }" is also disallowed in order to force the
more-readable split of bar {} onto a line of its own.

This is a pragmatic addition for broader compatibility with HCL 1-oriented
input. This single-line usage is not considered idiomatic HCL 2 and may
in future be undone by the formatter, though for now it is left as-is
aside from the spacing around the braces.

This also changes the behavior of the source code formatter to include
spaces on both sides of braces. This mimicks the formatting behavior of
HCL 1 for this situation, and (subjectively) reads better even for other
one-line braced expressions like object constructors and object for
expressions.
2018-12-14 13:45:03 -08:00

94 lines
2.1 KiB
Go

package hclwrite_test
import (
"fmt"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
func Example_generateFromScratch() {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
rootBody.SetAttributeValue("string", cty.StringVal("bar")) // this is overwritten later
rootBody.AppendNewline()
rootBody.SetAttributeValue("object", cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("foo"),
"bar": cty.NumberIntVal(5),
"baz": cty.True,
}))
rootBody.SetAttributeValue("string", cty.StringVal("foo"))
rootBody.SetAttributeValue("bool", cty.False)
rootBody.SetAttributeTraversal("path", hcl.Traversal{
hcl.TraverseRoot{
Name: "env",
},
hcl.TraverseAttr{
Name: "PATH",
},
})
rootBody.AppendNewline()
fooBlock := rootBody.AppendNewBlock("foo", nil)
fooBody := fooBlock.Body()
rootBody.AppendNewBlock("empty", nil)
rootBody.AppendNewline()
barBlock := rootBody.AppendNewBlock("bar", []string{"a", "b"})
barBody := barBlock.Body()
fooBody.SetAttributeValue("hello", cty.StringVal("world"))
bazBlock := barBody.AppendNewBlock("baz", nil)
bazBody := bazBlock.Body()
bazBody.SetAttributeValue("foo", cty.NumberIntVal(10))
bazBody.SetAttributeValue("beep", cty.StringVal("boop"))
bazBody.SetAttributeValue("baz", cty.ListValEmpty(cty.String))
fmt.Printf("%s", f.Bytes())
// Output:
// string = "foo"
//
// object = { bar = 5, baz = true, foo = "foo" }
// bool = false
// path = env.PATH
//
// foo {
// hello = "world"
// }
// empty {
// }
//
// bar "a" "b" {
// baz {
// foo = 10
// beep = "boop"
// baz = []
// }
// }
}
func ExampleExpression_RenameVariablePrefix() {
src := []byte(
"foo = a.x + a.y * b.c\n" +
"bar = max(a.z, b.c)\n",
)
f, diags := hclwrite.ParseConfig(src, "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
fmt.Printf("errors: %s", diags)
return
}
// Rename references of variable "a" to "z"
for _, attr := range f.Body().Attributes() {
attr.Expr().RenameVariablePrefix(
[]string{"a"},
[]string{"z"},
)
}
fmt.Printf("%s", f.Bytes())
// Output:
// foo = z.x + z.y * b.c
// bar = max(z.z, b.c)
}