hclparser

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 9 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.

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.

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 parts and returns the first resolved value.

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