hclwrite

package
v0.9.0-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2024 License: MPL-2.0 Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(code string) string

Format formats the given HCL code.

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 BuildHCL

func BuildHCL(builders ...BlockBuilder) *Block

BuildHCL builds a root HCL document.

func (*Block) AddBlock

func (b *Block) AddBlock(child *Block)

AddBlock adds a nested block on the block.

func (*Block) AddBoolean

func (b *Block) AddBoolean(name string, v bool)

AddBoolean adds boolean on block.

func (*Block) AddExpr

func (b *Block) AddExpr(name string, expr string)

AddExpr adds an expression to the block. The expressions are kept as is on the final document.

func (*Block) AddLabel

func (b *Block) AddLabel(name string)

AddLabel adds a label to the block.

func (*Block) AddNumberInt

func (b *Block) AddNumberInt(name string, v int64)

AddNumberInt adds a number to the block.

func (*Block) AddString

func (b *Block) AddString(name string, v string)

AddString adds string on block.

func (*Block) AttributesValues

func (b *Block) AttributesValues() map[string]cty.Value

AttributesValues gets all attributes that are evaluated values. Added expressions are ignored.

func (*Block) Build

func (b *Block) Build(parent *Block)

Build builds the given parent block by adding itself on it.

func (*Block) HasExpressions

func (b *Block) HasExpressions() bool

HasExpressions returns true if block has any non-evaluated expressions.

func (*Block) String

func (b *Block) String() string

String returns a string representation of the block that should always be formatted HCL code.

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.

Directories

Path Synopsis
Package hclutils provides useful functions to build HCL documents.
Package hclutils provides useful functions to build HCL documents.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL