hcl

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(ctx *hcl.EvalContext, body hcl.Body, val any) hcl.Diagnostics

Decode decodes the given HCL body into the given value. The value must be a pointer to a struct.

It works similar to the Decode function from the gohcl package, but it has improved support for decoding values into maps and slices. It also supports encoding.TextUnmarshaler interface and additional Unmarshaler interface that allows to decode values into custom types directly from cty.Value.

Only fields with the "hcl" tag will be decoded. The tag contains the name of the attribute or block and additional options separated by comma.

Supported options are

  • attr - the field is an attribute, it will be decoded from the HCL body attributes. It is the default tag and can be omitted.
  • label - a label of the parent block. Multiple labels can be defined.
  • optional - the field is optional, if it is not present in the HCL body, the field will be left as zero value.
  • ignore - the field will be ignored but still be a part of the schema.
  • block - the field is a block, it will be decoded from the HCL blocks. The field must be a struct, slice of structs or a map of structs.
  • remain - the field is populated with the remaining HCL body. The field must be hcl.Body.
  • body - the field is populated with the HCL body. The field must be hcl.BodyContent.
  • content - the field is populated with the HCL body content. The field must be hcl.BodyContent.
  • schema - the field is populated with the HCL body schema. The field must be hcl.BodySchema.
  • range - the block range. The field must be hcl.Range.

If name is omitted, the field name will be used.

func DecodeBlock added in v0.10.2

func DecodeBlock(ctx *hcl.EvalContext, block *hcl.Block, val any) hcl.Diagnostics

DecodeBlock decodes the given HCL block into the given value.

func DecodeExpression

func DecodeExpression(ctx *hcl.EvalContext, expr hcl.Expression, val any) hcl.Diagnostics

DecodeExpression decodes the given HCL expression into the given value.

func Encode added in v0.23.0

func Encode(val any, block *Block) hcl.Diagnostics

Encode encodes a struct to HCL. The value must be a struct.

func EncodeBlock added in v0.23.0

func EncodeBlock(val any, block *Block, typeName string, labels []string) hcl.Diagnostics

EncodeBlock encodes a struct to HCL block and appends it to the given block. The value must be a struct.

The typeName argument specifies the block type name. Labels are specified using the labels argument. If labels is nil, labels are read from the struct tags.

func ParseFile

func ParseFile(path string, subject *hcl.Range) (hcl.Body, hcl.Diagnostics)

ParseFile parses the given path into a hcl.File. The subject argument is optional. It is used to provide a range for the returned diagnostics.

func ParseFiles

func ParseFiles(paths []string, subject *hcl.Range) (hcl.Body, hcl.Diagnostics)

ParseFiles parses the HCL configuration files at the given paths. It returns a merged hcl.Body. The subject argument is optional. It is used to provide a range for the returned diagnostics.

func ParseSource added in v0.23.0

func ParseSource(name string, src []byte) (hcl.Body, hcl.Diagnostics)

ParseSource parses the given source into a hcl.File.

func ParseSources added in v0.23.0

func ParseSources(srcs [][]byte) (hcl.Body, hcl.Diagnostics)

ParseSources parses the HCL configuration sources. It returns a merged hcl.Body.

Types

type Attribute added in v0.23.0

type Attribute struct {
	Name  string
	Value any
}

Attribute represents a single attribute in a HCL block.

It is used to build am HCL configuration.

type Block added in v0.23.0

type Block struct {
	TypeName   string
	Labels     []string
	Attributes []*Attribute
	Blocks     []*Block
}

Block represents a single HCL block.

If typeName is empty, the block is a root block.

It is used to build an HCL configuration.

func (*Block) AppendAttribute added in v0.23.0

func (b *Block) AppendAttribute(name string, value any)

AppendAttribute appends a new attribute to the given block.

func (*Block) AppendBlock added in v0.23.0

func (b *Block) AppendBlock(typeName string, labels []string) *Block

AppendBlock appends a new block to the given block.

func (*Block) Bytes added in v0.23.0

func (b *Block) Bytes() ([]byte, hcl.Diagnostics)

Bytes returns the HCL configuration.

func (*Block) Write added in v0.23.0

func (b *Block) Write(hclBody *hclwrite.Body) hcl.Diagnostics

Write writes the block to the given HCL body.

type Marshaler added in v0.23.0

type Marshaler interface {
	MarshalHCL() (cty.Value, error)
}

Marshaler is the interface implemented by types that can marshal themselves into a cty.Value representation.

type OnEncodeBlock added in v0.23.0

type OnEncodeBlock interface {
	OnEncodeBlock(block *Block) hcl.Diagnostics
}

OnEncodeBlock is called during encoding of a struct to HCL.

type PostDecodeAttribute

type PostDecodeAttribute interface {
	PostDecodeAttribute(*hcl.EvalContext, *hcl.Attribute) hcl.Diagnostics
}

PostDecodeAttribute is called after an attribute is decoded.

type PostDecodeBlock

type PostDecodeBlock interface {
	PostDecodeBlock(*hcl.EvalContext, *hcl.BodySchema, *hcl.Block, *hcl.BodyContent) hcl.Diagnostics
}

PostDecodeBlock is called after a block is decoded.

type PreDecodeAttribute

type PreDecodeAttribute interface {
	PreDecodeAttribute(*hcl.EvalContext, *hcl.Attribute) hcl.Diagnostics
}

PreDecodeAttribute is called before an attribute is decoded.

type PreDecodeBlock

type PreDecodeBlock interface {
	PreDecodeBlock(*hcl.EvalContext, *hcl.BodySchema, *hcl.Block, *hcl.BodyContent) hcl.Diagnostics
}

PreDecodeBlock is called before a block is decoded.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalHCL(cty.Value) error
}

Unmarshaler is the interface implemented by types that can unmarshal a cty.Value representation of themselves.

Directories

Path Synopsis
ext

Jump to

Keyboard shortcuts

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