hclparser

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Copyright 2026 YLD Limited SPDX-License-Identifier: Apache-2.0 Package hclparser evaluates HCL expressions into cty values. It handles literal values, template strings, binary operations, unary operations, scope traversals (variable references), and other HCL expression types. Variables can be registered and resolved during expression evaluation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseCtyValue

func ParseCtyValue(value cty.Value, allowedTypes []string) (any, error)

ParseCtyValue converts a cty.Value to a native Go value, restricted to the given allowed type names.

Types

type BinaryOpExpr

type BinaryOpExpr struct {
	// contains filtered or unexported fields
}

BinaryOpExpr evaluates an HCL binary operation (e.g. +, -, ==) with variable support.

func NewBinaryOpExpr

func NewBinaryOpExpr(expression *hclsyntax.BinaryOpExpr, hv *HCLVars) *BinaryOpExpr

NewBinaryOpExpr creates a BinaryOpExpr for the given expression and variable store.

func (*BinaryOpExpr) Parse

func (boe *BinaryOpExpr) Parse() (cty.Value, error)

Parse evaluates the binary operation and returns the resulting cty value.

type Expression

type Expression struct {
	// contains filtered or unexported fields
}

Expression wraps a generic HCL expression for direct evaluation.

func NewExpression

func NewExpression(expression hcl.Expression) *Expression

NewExpression creates an Expression wrapper for the given HCL expression.

func (*Expression) Parse

func (e *Expression) Parse() (cty.Value, error)

Parse evaluates the expression and returns its cty value.

type HCLParser

type HCLParser struct {
	// contains filtered or unexported fields
}

HCLParser evaluates a single HCL expression, resolving variable references.

func New

func New(expression hcl.Expression, hv *HCLVars) *HCLParser

New creates an HCLParser for the given expression and variable store.

func (*HCLParser) Parse

func (hp *HCLParser) Parse() error

Parse evaluates the expression and stores the result.

func (*HCLParser) Result

func (hp *HCLParser) Result() cty.Value

Result returns the evaluated cty value after Parse has been called. String results have their line endings normalized to LF.

func (*HCLParser) Variables

func (hp *HCLParser) Variables() *HCLVars

Variables returns the variable store used by this parser.

type HCLVars

type HCLVars struct {
	// contains filtered or unexported fields
}

HCLVars is a key-value store for HCL variable values.

func NewHCLVars

func NewHCLVars() *HCLVars

NewHCLVars creates an empty HCLVars store.

func (*HCLVars) Add

func (av *HCLVars) Add(key string, value cty.Value)

Add stores a variable value under the given key.

func (*HCLVars) GetValue

func (av *HCLVars) GetValue(attr string, idx *int64) (cty.Value, error)

GetValue retrieves a variable by key, optionally indexing into a list.

func (*HCLVars) GetValueByIndex

func (av *HCLVars) GetValueByIndex(key string, idx int64) (cty.Value, error)

GetValueByIndex returns an element from a list variable by key and index.

func (*HCLVars) GetValueByKey

func (av *HCLVars) GetValueByKey(key string) (cty.Value, error)

GetValueByKey returns the variable value for the given key.

func (*HCLVars) HeadComment added in v0.7.0

func (av *HCLVars) HeadComment(r hcl.Range) string

HeadComment returns the run of whole-line comments written directly above r, as YAML wants it: each line still carrying its "#", joined by newlines.

The run is unbroken. A blank line between the comment and what it sits above ends it, the same way it reads on the page, and a comment sharing a line with code is not part of it: that one belongs to whatever it trails, and taking it would move it somewhere it was not written.

A comment is not part of any decoded value, so a lex over the source is the only way back to one.

func (*HCLVars) SetSources added in v0.7.0

func (av *HCLVars) SetSources(sources map[string][]byte)

SetSources hands the store the bytes each file was parsed from, keyed by filename, as hclparse.Parser.Sources returns them.

A comment is not part of any decoded value, so the only way back to one is the source text. The parser already holds every byte it read, and the store is the one thing threaded to each Parse along the way, so it carries them rather than a second argument added to every signature between here and the attribute.

func (*HCLVars) TrailingComment added in v0.7.0

func (av *HCLVars) TrailingComment(r hcl.Range) string

TrailingComment returns the # comment sharing a line with the end of r, or empty string if the line ends without one.

Reading from what was parsed rather than from disk keeps the comment matched to the text the rest of the parse ran against, and costs one map lookup where re-reading cost a syscall per attribute.

type LiteralValueExpr

type LiteralValueExpr struct {
	// contains filtered or unexported fields
}

LiteralValueExpr wraps an HCL literal value (string, number, bool).

func NewLiteralValueExpr

func NewLiteralValueExpr(expression *hclsyntax.LiteralValueExpr) *LiteralValueExpr

NewLiteralValueExpr creates a LiteralValueExpr for the given HCL literal.

func (*LiteralValueExpr) Parse

func (lve *LiteralValueExpr) Parse() (cty.Value, error)

Parse returns the literal's cty value directly.

type ScopeTraversalExpr

type ScopeTraversalExpr struct {
	// contains filtered or unexported fields
}

ScopeTraversalExpr resolves an HCL scope traversal (e.g. var.name or var.list[0]) against a variable store.

func NewScopeTraversalExpr

func NewScopeTraversalExpr(expression *hclsyntax.ScopeTraversalExpr, hv *HCLVars) *ScopeTraversalExpr

NewScopeTraversalExpr creates a ScopeTraversalExpr for the given traversal expression and variable store.

func (*ScopeTraversalExpr) Parse

func (ste *ScopeTraversalExpr) Parse() (cty.Value, error)

Parse walks the traversal segments, extracts the attribute name and optional index, and looks up the value.

type TemplateExpr

type TemplateExpr struct {
	// contains filtered or unexported fields
}

TemplateExpr evaluates an HCL template string expression.

func NewTemplateExpr

func NewTemplateExpr(expression *hclsyntax.TemplateExpr) *TemplateExpr

NewTemplateExpr creates a TemplateExpr for the given HCL template.

func (*TemplateExpr) Parse

func (te *TemplateExpr) Parse() (cty.Value, error)

Parse evaluates the template and returns its value. A template of one part keeps that part's type, so a bare number or bool is not turned into a string.

Returning on the first part truncated every template that has more than one: "prefix ${1} suffix" came out as "prefix ", written to the file with no error and no warning.

type VariableConfig

type VariableConfig struct {
	Id    string    `hcl:"id,label"`
	Value cty.Value `hcl:"value,attr"`
}

VariableConfig represents a single HCL variable declaration with an id and value.

type VariableRef

type VariableRef struct {
	Attr  string
	Index *int64
}

VariableRef holds the resolved attribute name and optional index from a traversal expression.

type VariablesConfig

type VariablesConfig []VariableConfig

VariablesConfig is a slice of VariableConfig decoded from HCL variable blocks.

func (*VariablesConfig) Parse

func (config *VariablesConfig) Parse(hv *HCLVars) error

Parse registers all variable values into the given HCLVars store.

Jump to

Keyboard shortcuts

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