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 ¶
- func ParseCtyValue(value cty.Value, allowedTypes []string) (any, error)
- type BinaryOpExpr
- type Expression
- type HCLParser
- type HCLVars
- func (av *HCLVars) Add(key string, value cty.Value)
- func (av *HCLVars) GetValue(attr string, idx *int64) (cty.Value, error)
- func (av *HCLVars) GetValueByIndex(key string, idx int64) (cty.Value, error)
- func (av *HCLVars) GetValueByKey(key string) (cty.Value, error)
- func (av *HCLVars) HeadComment(r hcl.Range) string
- func (av *HCLVars) SetSources(sources map[string][]byte)
- func (av *HCLVars) TrailingComment(r hcl.Range) string
- type LiteralValueExpr
- type ScopeTraversalExpr
- type TemplateExpr
- type VariableConfig
- type VariableRef
- type VariablesConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
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.
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.
type HCLVars ¶
type HCLVars struct {
// contains filtered or unexported fields
}
HCLVars is a key-value store for HCL variable values.
func (*HCLVars) GetValueByIndex ¶
GetValueByIndex returns an element from a list variable by key and index.
func (*HCLVars) GetValueByKey ¶
GetValueByKey returns the variable value for the given key.
func (*HCLVars) HeadComment ¶ added in v0.7.0
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
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
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.
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.
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 ¶
VariableConfig represents a single HCL variable declaration with an id and value.
type VariableRef ¶
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.