eval

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package eval provides both full and partial evaluation of HCL.

Index

Constants

View Source
const (
	ErrPartial             errors.Kind = "partial evaluation failed"
	ErrInterpolation       errors.Kind = "interpolation failed"
	ErrForExprDisallowEval errors.Kind = "`for` expression disallow globals/terramate variables"
)

Errors returned when doing partial evaluation.

View Source
const ErrCannotExtendObject errors.Kind = "cannot extend object"

ErrCannotExtendObject is the error when an object cannot be extended.

View Source
const ErrEval errors.Kind = "eval expression"

ErrEval indicates a failure during the evaluation process

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

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

Context is used to evaluate HCL code.

func NewContext

func NewContext(funcs map[string]function.Function) *Context

NewContext creates a new HCL evaluation context. The basedir is the base directory used by any interpolation functions that accept filesystem paths as arguments. The basedir must be an absolute path to a directory.

func NewContextFrom

func NewContextFrom(ctx *hhcl.EvalContext) *Context

NewContextFrom creates a new evaluator from the hashicorp EvalContext.

func (*Context) Copy

func (c *Context) Copy() *Context

Copy the eval context.

func (*Context) DeleteNamespace

func (c *Context) DeleteNamespace(name string)

DeleteNamespace deletes the namespace name from the context. If name is not in the context, it's a no-op.

func (*Context) Eval

func (c *Context) Eval(expr hhcl.Expression) (cty.Value, error)

Eval will evaluate an expression given its context.

func (*Context) GetNamespace

func (c *Context) GetNamespace(name string) (cty.Value, bool)

GetNamespace will retrieve the value for the given namespace.

func (*Context) HasNamespace

func (c *Context) HasNamespace(name string) bool

HasNamespace returns true the evaluation context knows this namespace, false otherwise.

func (*Context) PartialEval

func (c *Context) PartialEval(expr hhcl.Expression) (hhcl.Expression, error)

PartialEval evaluates only the terramate variable expressions from the list of tokens, leaving all the rest as-is. It returns a modified list of tokens with no reference to terramate namespaced variables (globals and terramate) and functions (tm_ prefixed functions).

func (*Context) SetEnv

func (c *Context) SetEnv(environ []string)

SetEnv sets the given environment on the env namespace of the evaluation context. environ must be on the same format as os.Environ().

func (*Context) SetFunction

func (c *Context) SetFunction(name string, fn function.Function)

SetFunction sets the function in the context.

func (*Context) SetNamespace

func (c *Context) SetNamespace(name string, vals map[string]cty.Value)

SetNamespace will set the given values inside the given namespace on the evaluation context.

func (*Context) Unwrap

func (c *Context) Unwrap() *hhcl.EvalContext

Unwrap returns the internal hhcl.EvalContext.

type CtyValue

type CtyValue struct {
	cty.Value
	// contains filtered or unexported fields
}

CtyValue is a wrapper for a raw cty value.

func NewCtyValue

func NewCtyValue(val cty.Value, origin Info) CtyValue

NewCtyValue creates a new cty.Value wrapper. Note: The cty.Value val is marked with the origin path and must be unmarked before use with any hashicorp API otherwise it panics.

func (CtyValue) Info

func (v CtyValue) Info() Info

Info provides extra information for the value.

func (CtyValue) IsObject

func (v CtyValue) IsObject() bool

IsObject returns false for CtyValue values.

func (CtyValue) Raw

func (v CtyValue) Raw() cty.Value

Raw returns the original cty.Value value (unmarked).

type Info

type Info struct {
	// Dir defines the directory from there the value is being instantiated,
	// which means it's the scope directory (not the file where it's defined).
	// For values that comes from imports, the Dir will be the directory
	// which imports the value.
	Dir project.Path

	// DefinedAt provides the source file where the value is defined.
	DefinedAt project.Path
}

Info provides extra information for the configuration value.

type Object

type Object struct {

	// Keys is a map of key names to values.
	Keys map[string]Value
	// contains filtered or unexported fields
}

Object is an object container for cty.Value values supporting set at arbitrary accessor paths.

Eg.:

obj := eval.NewObject(origin)
obj.Set("val", eval.NewObject())

The snippet above creates the object below:

{
    val = {}
}

Then values can be set inside obj.val by doing:

obj.SetAt(ObjectPath{"val", "test"}, eval.NewValue(cty.StringVal("test"), origin))

Of which creates the object below:

{
    val = {
        test = "test"
    }
}

func NewObject

func NewObject(origin Info) *Object

NewObject creates a new object for configdir directory.

func (*Object) AsValueMap

func (obj *Object) AsValueMap() map[string]cty.Value

AsValueMap returns a map of string to Hashicorp cty.Value.

func (*Object) DeleteAt

func (obj *Object) DeleteAt(path ObjectPath) error

DeleteAt deletes the value at the specified path.

func (*Object) GetKeyPath

func (obj *Object) GetKeyPath(path ObjectPath) (Value, bool)

GetKeyPath retrieves the value at path.

func (*Object) Info

func (obj *Object) Info() Info

Info provides extra information for the object value.

func (*Object) IsObject

func (obj *Object) IsObject() bool

IsObject returns true for Object values.

func (*Object) MergeFailsIfKeyExists

func (obj *Object) MergeFailsIfKeyExists(path ObjectPath, value Value) error

MergeFailsIfKeyExists merge the value into obj but fails if any key in value exists in obj.

func (*Object) MergeNewKeys

func (obj *Object) MergeNewKeys(path ObjectPath, value Value) error

MergeNewKeys merge the keys from value that doesn't exist in obj.

func (*Object) MergeOverwrite

func (obj *Object) MergeOverwrite(path ObjectPath, value Value) error

MergeOverwrite merges value into obj by overwriting each key.

func (*Object) Set

func (obj *Object) Set(key string, value Value)

Set a key value into object.

func (*Object) SetAt

func (obj *Object) SetAt(path ObjectPath, value Value) error

SetAt sets a value at the specified path key.

func (*Object) SetFrom

func (obj *Object) SetFrom(values map[string]Value) *Object

SetFrom sets the object keys and values from the map.

func (*Object) SetFromCtyValues

func (obj *Object) SetFromCtyValues(values map[string]cty.Value, origin Info) *Object

SetFromCtyValues sets the object from the values map.

func (*Object) String

func (obj *Object) String() string

String representation of the object.

type ObjectPath

type ObjectPath []string

ObjectPath represents a path inside the object.

type Value

type Value interface {
	// Info provides extra information for the value.
	Info() Info

	// IsObject tells if the value is an object.
	IsObject() bool
}

Value is an evaluated value.

func NewValue

func NewValue(val cty.Value, origin Info) Value

NewValue returns a new object Value from a cty.Value. Note: this is not a wrapper as it returns an Object if val is a cty.Object.

Jump to

Keyboard shortcuts

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