Documentation ¶
Overview ¶
Package hclwrite aims to provide some facilities making it easier/safer to generate HCL code for testing purposes. It aims at:
- Close to how HCL is written. - Provide formatted string representation. - Avoid issues when raw HCL strings are used on tests in general.
It is not a replacement to hclwrite: https://pkg.go.dev/github.com/hashicorp/hcl/v2/hclwrite It is just easier/nicer to use on tests + circumvents some limitations like:
- https://stackoverflow.com/questions/67945463/how-to-use-hcl-write-to-set-expressions-with
We are missing a way to build objects and lists with a similar syntax to blocks. For now we circumvent this with the AttributeValue function.
Index ¶
- func Format(code string) string
- type Block
- func (b *Block) AddBlock(child *Block)
- func (b *Block) AddBoolean(name string, v bool)
- func (b *Block) AddExpr(name string, expr string)
- func (b *Block) AddLabel(name string)
- func (b *Block) AddNumberInt(name string, v int64)
- func (b *Block) AddString(name string, v string)
- func (b *Block) AttributesValues() map[string]cty.Value
- func (b *Block) Build(parent *Block)
- func (b *Block) HasExpressions() bool
- func (b *Block) String() string
- type BlockBuilder
- func AttributeValue(t *testing.T, name string, expr string) BlockBuilder
- func Boolean(name string, val bool) BlockBuilder
- func Expression(name string, expr string) BlockBuilder
- func Labels(labels ...string) BlockBuilder
- func NumberInt(name string, val int64) BlockBuilder
- func String(name string, val string) BlockBuilder
- type BlockBuilderFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
Block represents an HCL block.
func BuildBlock ¶
func BuildBlock(name string, builders ...BlockBuilder) *Block
BuildBlock builds a block with the given name and N block builders.
func (*Block) AddBoolean ¶
AddBoolean adds boolean on block.
func (*Block) AddExpr ¶
AddExpr adds an expression to the block. The expressions are kept as is on the final document.
func (*Block) AddNumberInt ¶
AddNumberInt adds a number to the block.
func (*Block) AttributesValues ¶
AttributesValues gets all attributes that are evaluated values. Added expressions are ignored.
func (*Block) HasExpressions ¶
HasExpressions returns true if block has any non-evaluated expressions.
type BlockBuilder ¶
type BlockBuilder interface {
Build(*Block)
}
BlockBuilder provides a general purpose way to build blocks.
func AttributeValue ¶
func AttributeValue(t *testing.T, name string, expr string) BlockBuilder
AttributeValue accepts an expr as the attribute value, similar to Expression, but will evaluate the expr and store the resulting value so it will be available as an attribute value instead of as an expression. If evaluation fails the test caller will fail.
The evaluation is quite limited, only suitable for evaluating objects/lists/etc, but won't work with any references to namespaces except default Terraform function calls.
func Boolean ¶
func Boolean(name string, val bool) BlockBuilder
Boolean adds a boolean attribute to the block.
func Expression ¶
func Expression(name string, expr string) BlockBuilder
Expression adds the attribute with the given name with the given expression, the expression won't be evaluated and this won't affect the attribute values of the block.
The given expression will be added on the generated output verbatim.
func Labels ¶
func Labels(labels ...string) BlockBuilder
Labels creates a block builder that adds labels to block.
func NumberInt ¶
func NumberInt(name string, val int64) BlockBuilder
NumberInt adds a number attribute to the block.
func String ¶
func String(name string, val string) BlockBuilder
String adds a string attribute to the block.
type BlockBuilderFunc ¶
type BlockBuilderFunc func(*Block)
BlockBuilderFunc is an adapter to allow the use of ordinary functions as BlockBuilders.
func (BlockBuilderFunc) Build ¶
func (builder BlockBuilderFunc) Build(b *Block)
Build calls the underlying builder function to build the given block.