eval

package
v0.2.18 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: Apache-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 added in v0.1.29

func NewContextFrom(ctx *hhcl.EvalContext) *Context

NewContextFrom creates a new evaluator from the hashicorp EvalContext.

func (*Context) Copy added in v0.2.6

func (c *Context) Copy() *Context

Copy the eval context.

func (*Context) DeleteNamespace added in v0.1.15

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 added in v0.1.37

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

GetNamespace will retrieve the value for the given namespace.

func (*Context) HasNamespace added in v0.0.10

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

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

func (*Context) PartialEval added in v0.0.12

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 added in v0.2.16

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 added in v0.2.7

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 added in v0.2.6

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

Unwrap returns the internal hhcl.EvalContext.

type CtyValue added in v0.1.37

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

CtyValue is a wrapper for a raw cty value.

func NewCtyValue added in v0.1.37

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 added in v0.2.4

func (v CtyValue) Info() Info

Info provides extra information for the value.

func (CtyValue) IsObject added in v0.1.37

func (v CtyValue) IsObject() bool

IsObject returns false for CtyValue values.

func (CtyValue) Raw added in v0.1.37

func (v CtyValue) Raw() cty.Value

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

type Info added in v0.2.4

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 added in v0.1.37

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 added in v0.1.37

func NewObject(origin Info) *Object

NewObject creates a new object for configdir directory.

func (*Object) AsValueMap added in v0.1.37

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

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

func (*Object) DeleteAt added in v0.1.37

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

DeleteAt deletes the value at the specified path.

func (*Object) GetKeyPath added in v0.1.37

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

GetKeyPath retrieves the value at path.

func (*Object) Info added in v0.2.4

func (obj *Object) Info() Info

Info provides extra information for the object value.

func (*Object) IsObject added in v0.1.37

func (obj *Object) IsObject() bool

IsObject returns true for Object values.

func (*Object) MergeFailsIfKeyExists added in v0.2.5

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 added in v0.2.5

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

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

func (*Object) MergeOverwrite added in v0.2.5

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

MergeOverwrite merges value into obj by overwriting each key.

func (*Object) Set added in v0.1.37

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

Set a key value into object.

func (*Object) SetAt added in v0.1.37

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

SetAt sets a value at the specified path key.

func (*Object) SetFrom added in v0.1.37

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

SetFrom sets the object keys and values from the map.

func (*Object) SetFromCtyValues added in v0.1.37

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

SetFromCtyValues sets the object from the values map.

func (*Object) String added in v0.1.37

func (obj *Object) String() string

String representation of the object.

type ObjectPath added in v0.1.37

type ObjectPath []string

ObjectPath represents a path inside the object.

type Value added in v0.1.37

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 added in v0.1.37

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