lang

package
v0.57.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const ArrKey = "____arr"
View Source
const FilesKey = "____files"
View Source
const FuncKey = "____func"
View Source
const MetaKey = "____meta"

Variables

View Source
var ChildMarkMergeFunc = function.New(&function.Spec{
	Description: `Merges all of the elements from the given maps into a single map, or the attributes from given objects into a single object.`,
	Params:      []function.Parameter{},
	VarParam: &function.Parameter{
		Name:             "maps",
		Type:             cty.DynamicPseudoType,
		AllowDynamicType: true,
		AllowNull:        true,
		AllowMarked:      true,
	},
	Type: func(args []cty.Value) (cty.Type, error) {

		if len(args) == 0 {
			return cty.EmptyObject, nil
		}

		attrs := map[string]cty.Type{}

		first := cty.NilType
		matching := true
		attrsKnown := true
		for i, arg := range args {
			ty := arg.Type()

			if ty.Equals(cty.DynamicPseudoType) {
				return cty.DynamicPseudoType, nil
			}

			if !ty.IsMapType() && !ty.IsObjectType() {
				return cty.NilType, fmt.Errorf("arguments must be maps or objects, got %#v", ty.FriendlyName())
			}

			arg, _ = arg.Unmark()

			switch {
			case ty.IsObjectType() && !arg.IsNull():
				for attr, aty := range ty.AttributeTypes() {
					attrs[attr] = aty
				}
			case ty.IsMapType():
				switch {
				case arg.IsNull():

				case arg.IsKnown():
					ety := arg.Type().ElementType()
					for it := arg.ElementIterator(); it.Next(); {
						attr, _ := it.Element()
						attrs[attr.AsString()] = ety
					}
				default:

					attrsKnown = false
				}
			}

			if i == 0 {
				first = arg.Type()
				continue
			}

			if !ty.Equals(first) && matching {
				matching = false
			}
		}

		if matching {
			return first, nil
		}

		if !attrsKnown {
			return cty.DynamicPseudoType, nil
		}

		return cty.Object(attrs), nil
	},

	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		outputMap := make(map[string]cty.Value)
		var markses []cty.ValueMarks // remember any marked maps/objects we find

		for _, arg := range args {
			if arg.IsNull() {
				continue
			}
			arg, argMarks := arg.Unmark()

			for it := arg.ElementIterator(); it.Next(); {
				k, v := it.Element()
				outputMap[k.AsString()] = v.WithMarks(argMarks)
			}
		}

		switch {
		case retType.IsMapType():
			if len(outputMap) == 0 {
				return cty.MapValEmpty(retType.ElementType()).WithMarks(markses...), nil
			}
			return cty.MapVal(outputMap).WithMarks(markses...), nil
		case retType.IsObjectType(), retType.Equals(cty.DynamicPseudoType):
			return cty.ObjectVal(outputMap).WithMarks(markses...), nil
		default:
			panic(fmt.Sprintf("unexpected return type: %#v", retType))
		}
	},
})

ChildMarkMergeFunc is a copy of the MergeFunc from cty stdlib, but with the ability to merge marked parents into their children. This is necessary for the `merge` function to work as expected and sort values based on location in the source file.

Functions

func CheckForAnyIncompletedBlock added in v0.54.0

func CheckForAnyIncompletedBlock(ectx []*SudoContext) error

func EncodeExpression

func EncodeExpression(e hclsyntax.Expression, ectx *hcl.EvalContext) (any, hcl.Diagnostics, error)

func EvaluateAttr

func EvaluateAttr(ctx context.Context, attr *hclsyntax.Attribute, parentctx *SudoContext, extra ...*hcl.EvalContext) hcl.Diagnostics

func ExtractUserFuncs

func ExtractUserFuncs(ctx context.Context, ibdy hcl.Body, parent *hcl.EvalContext) (map[string]function.Function, hcl.Diagnostics)

func ExtractVariables

func ExtractVariables(ctx context.Context, bdy *hclsyntax.Body, parentctx *SudoContext) hcl.Diagnostics

func InstanceLocationStringToHCLRange

func InstanceLocationStringToHCLRange(instLoc string, msg string, cnt hclsyntax.Expression, ectx *hcl.EvalContext, sudo *SudoContext, file *BodyBuilder) (hcl.Expression, hcl.Diagnostics)

func LoadValidationErrors

func LoadValidationErrors(ctx context.Context, cnt hclsyntax.Expression, ectx *hcl.EvalContext, errv error, bdy *BodyBuilder, sudo *SudoContext) (hcl.Diagnostics, error)

func NewArrayItemAttribute added in v0.53.0

func NewArrayItemAttribute(index int, expr hclsyntax.Expression) *hclsyntax.Attribute

func NewContextFromFile

func NewContextFromFile(ctx context.Context, fle []byte, name string) (*hcl.File, *SudoContext, *BodyBuilder, hcl.Diagnostics, error)

func NewContextFromFiles

func NewContextFromFiles(ctx context.Context, fle map[string][]byte) (*hcl.File, *SudoContext, *BodyBuilder, hcl.Diagnostics, error)

func NewContextualizedFunctionMap

func NewContextualizedFunctionMap(ectx *SudoContext, file string) map[string]function.Function

func NewDynamicContextualizedFunctionMap

func NewDynamicContextualizedFunctionMap(ectx *SudoContext) map[string]function.Function

func NewForCollectionAttribute added in v0.53.0

func NewForCollectionAttribute(expr hclsyntax.Expression) *hclsyntax.Attribute

func NewFuncArgAttribute added in v0.53.0

func NewFuncArgAttribute(index int, expr hclsyntax.Expression) *hclsyntax.Attribute

func NewFunctionMap

func NewFunctionMap() map[string]function.Function

func NewGenBlockEvaluation

func NewGenBlockEvaluation(ctx context.Context, sctx *SudoContext, file *BodyBuilder) (res map[string]*FileBlockEvaluation, diags hcl.Diagnostics, err error)

func NewObjectItemAttribute added in v0.53.0

func NewObjectItemAttribute(key string, nameRange hcl.Range, expr hclsyntax.Expression) *hclsyntax.Attribute

func NewRefFunctionFromPath

func NewRefFunctionFromPath(ctx context.Context, start string) function.Function

func NewUnknownBlockEvaluation

func NewUnknownBlockEvaluation(ctx context.Context, parentctx *SudoContext, block *hclsyntax.Block) (diags hcl.Diagnostics)

func ProccessBulk

func ProccessBulk(ctx context.Context, fs afero.Fs, files []string) (map[string]*FileBlockEvaluation, hcl.Diagnostics, error)

func Process

func Process(ctx context.Context, fs afero.Fs, file string) (map[string]*FileBlockEvaluation, hcl.Diagnostics, error)

func UnmarkToSortedArray added in v0.53.0

func UnmarkToSortedArray(me cty.Value) (any, error)

Types

type AnyBlockEvaluation

type AnyBlockEvaluation struct {
	Name    string
	Content map[string]cty.Value
}

type AttrMeta added in v0.53.0

type AttrMeta struct {
	HCL   *hclsyntax.Attribute
	Value cty.Value
}

func (*AttrMeta) Range added in v0.53.0

func (me *AttrMeta) Range() hcl.Range

func (*AttrMeta) Variables added in v0.53.0

func (*AttrMeta) Variables() map[string]cty.Value

Variables implements Meta.

type AugmentedForValueExpr added in v0.53.0

type AugmentedForValueExpr struct {
	hclsyntax.Expression
	Sudo    *SudoContext
	ForExpr *hclsyntax.ForExpr
	Ctx     context.Context
}

func (*AugmentedForValueExpr) Range added in v0.53.0

func (me *AugmentedForValueExpr) Range() hcl.Range

Range implements hclsyntax.Expression.

func (*AugmentedForValueExpr) StartRange added in v0.53.0

func (me *AugmentedForValueExpr) StartRange() hcl.Range

StartRange implements hclsyntax.Expression.

func (*AugmentedForValueExpr) Value added in v0.53.0

func (me *AugmentedForValueExpr) Value(ectx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)

Value implements hclsyntax.Expression.

func (*AugmentedForValueExpr) Variables added in v0.53.0

func (me *AugmentedForValueExpr) Variables() []hcl.Traversal

Variables implements hclsyntax.Expression.

type BasicBlockMeta added in v0.53.0

type BasicBlockMeta struct {
	HCL *hclsyntax.Block
}

func (*BasicBlockMeta) Block added in v0.53.0

func (me *BasicBlockMeta) Block() *hclsyntax.Block

func (*BasicBlockMeta) Range added in v0.53.0

func (me *BasicBlockMeta) Range() hcl.Range

func (*BasicBlockMeta) Variables added in v0.53.0

func (me *BasicBlockMeta) Variables() map[string]cty.Value

type BlockLabelMeta added in v0.53.0

type BlockLabelMeta struct {
	HCL hcl.Range
}

func (*BlockLabelMeta) Range added in v0.53.0

func (me *BlockLabelMeta) Range() hcl.Range

func (*BlockLabelMeta) Variables added in v0.53.0

func (me *BlockLabelMeta) Variables() map[string]cty.Value

type BlockMeta added in v0.53.0

type BlockMeta interface {
	Meta
	Block() *hclsyntax.Block
}

type BodyBuilder

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

func (*BodyBuilder) GetAllBlocksOfType

func (me *BodyBuilder) GetAllBlocksOfType(name string) []*hclsyntax.Block

func (*BodyBuilder) NewRoot

func (me *BodyBuilder) NewRoot() *hclsyntax.Body

func (*BodyBuilder) NewRootForFile

func (me *BodyBuilder) NewRootForFile(file string) (*hclsyntax.Body, error)

type FileBlockEvaluation

type FileBlockEvaluation struct {
	Name          string
	Schema        string
	Path          string
	OrderedOutput yaml.MapSlice
	RawOutput     any
	Source        string
}

func (*FileBlockEvaluation) Encode

func (me *FileBlockEvaluation) Encode() ([]byte, error)

func (*FileBlockEvaluation) WriteToFile

func (me *FileBlockEvaluation) WriteToFile(ctx context.Context, fs afero.Fs) error

func (*FileBlockEvaluation) WriteToReader

func (me *FileBlockEvaluation) WriteToReader(ctx context.Context) (io.Reader, error)

type GenBlockMeta added in v0.53.0

type GenBlockMeta struct {
	BasicBlockMeta
	RootRelPath string
}

func (*GenBlockMeta) Block added in v0.53.0

func (me *GenBlockMeta) Block() *hclsyntax.Block

func (*GenBlockMeta) Range added in v0.53.0

func (me *GenBlockMeta) Range() hcl.Range

func (*GenBlockMeta) Variables added in v0.53.0

func (me *GenBlockMeta) Variables() map[string]cty.Value

type IncomleteBlockMeta added in v0.54.0

type IncomleteBlockMeta struct {
	HCL *hclsyntax.Block
}

func (*IncomleteBlockMeta) Block added in v0.54.0

func (me *IncomleteBlockMeta) Block() *hclsyntax.Block

func (*IncomleteBlockMeta) Range added in v0.54.0

func (me *IncomleteBlockMeta) Range() hcl.Range

func (*IncomleteBlockMeta) Variables added in v0.54.0

func (me *IncomleteBlockMeta) Variables() map[string]cty.Value

type Meta added in v0.53.0

type Meta interface {
	// Range() hcl.Range
	// Value() cty.Value
	Range() hcl.Range
	Variables() map[string]cty.Value
}

type PreCalcExpr added in v0.53.0

type PreCalcExpr struct {
	hclsyntax.Expression
	Val cty.Value
}

func (*PreCalcExpr) Range added in v0.53.0

func (me *PreCalcExpr) Range() hcl.Range

Range implements hclsyntax.Expression.

func (*PreCalcExpr) StartRange added in v0.53.0

func (me *PreCalcExpr) StartRange() hcl.Range

StartRange implements hclsyntax.Expression.

func (*PreCalcExpr) Value added in v0.53.0

func (me *PreCalcExpr) Value(ectx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)

Value implements hclsyntax.Expression.

func (*PreCalcExpr) Variables added in v0.53.0

func (me *PreCalcExpr) Variables() []hcl.Traversal

Variables implements hclsyntax.Expression.

type RemappableSudoContextArray added in v0.53.0

type RemappableSudoContextArray []*SudoContext

type SimpleNameMeta added in v0.53.0

type SimpleNameMeta struct {
	NameRange hcl.Range
}

func NewSimpleNameMeta added in v0.53.0

func NewSimpleNameMeta(parent hcl.Range) *SimpleNameMeta

func (*SimpleNameMeta) Range added in v0.53.0

func (me *SimpleNameMeta) Range() hcl.Range

func (*SimpleNameMeta) Variables added in v0.53.0

func (*SimpleNameMeta) Variables() map[string]cty.Value

Variables implements Meta.

type Sorter added in v0.53.0

type Sorter struct {
	Range   []hcl.Range
	Key     string
	Value   cty.Value
	Ignored bool
}

func (*Sorter) Array added in v0.53.0

func (me *Sorter) Array() []hcl.Range

type SudoContext

type SudoContext struct {
	ParentKey string
	Parent    *SudoContext
	Map       map[string]*SudoContext
	Value     *cty.Value

	UserFuncs        map[string]function.Function
	Meta             Meta
	TmpFileLevelVars map[string]cty.Value
	// contains filtered or unexported fields
}

func CheckForCompletedBlock added in v0.54.0

func CheckForCompletedBlock(ectx *SudoContext, file string) (*SudoContext, error)

func (*SudoContext) ApplyBody

func (parent *SudoContext) ApplyBody(ctx context.Context, body *hclsyntax.Body) hcl.Diagnostics

func (*SudoContext) ApplyKeyVal

func (me *SudoContext) ApplyKeyVal(key string, val cty.Value, r hcl.Range) hcl.Diagnostics

func (*SudoContext) ApplyValue

func (me *SudoContext) ApplyValue(met cty.Value)

func (*SudoContext) BlocksOfType added in v0.53.0

func (me *SudoContext) BlocksOfType(name string) ([]*SudoContext, error)

func (*SudoContext) BuildStaticEvalContext

func (wc *SudoContext) BuildStaticEvalContext() *hcl.EvalContext

func (*SudoContext) BuildStaticEvalContextWithFileData

func (wc *SudoContext) BuildStaticEvalContextWithFileData(file string) *hcl.EvalContext

func (*SudoContext) BuildStaticEvalVars

func (me *SudoContext) BuildStaticEvalVars() map[string]cty.Value

func (*SudoContext) BuildStaticVarsList

func (me *SudoContext) BuildStaticVarsList() []cty.Value

func (*SudoContext) Functions

func (me *SudoContext) Functions() map[string]function.Function

func (*SudoContext) GetAllFileLevelBlocksOfType added in v0.53.0

func (me *SudoContext) GetAllFileLevelBlocksOfType(name string) ([]*SudoContext, error)

func (*SudoContext) GetAllTemporaryFileLevelVars added in v0.53.0

func (wc *SudoContext) GetAllTemporaryFileLevelVars() map[string]cty.Value

func (*SudoContext) IdentifyChild added in v0.54.0

func (me *SudoContext) IdentifyChild(strs ...string) *SudoContext

func (*SudoContext) List added in v0.53.0

func (me *SudoContext) List() []*SudoContext

func (*SudoContext) NewBlockChild added in v0.55.0

func (me *SudoContext) NewBlockChild(typ *hclsyntax.Block) (*SudoContext, error)

func (*SudoContext) NewBlockLabelChild added in v0.55.0

func (me *SudoContext) NewBlockLabelChild(label string, rnge hcl.Range) (*SudoContext, error)

func (*SudoContext) NewChild

func (me *SudoContext) NewChild(key string, rnge hcl.Range) *SudoContext

func (*SudoContext) NewNonBlockChild added in v0.55.0

func (me *SudoContext) NewNonBlockChild(key string, rnge hcl.Range) (*SudoContext, error)

func (*SudoContext) Root

func (wc *SudoContext) Root() *SudoContext

func (*SudoContext) ToValue

func (me *SudoContext) ToValue() cty.Value

func (*SudoContext) ToValueWithExtraContext added in v0.53.0

func (me *SudoContext) ToValueWithExtraContext() cty.Value

func (*SudoContext) ToYAML added in v0.53.0

func (me *SudoContext) ToYAML() (yaml.MapSlice, error)

type ValidationBlock

type ValidationBlock interface {
	HasValidationErrors() bool
	Encode() ([]byte, error)
}

type WrappedExpression added in v0.53.0

type WrappedExpression struct {
	hclsyntax.Node
	Expr hclsyntax.Expression
	Sudo *SudoContext
}

func (*WrappedExpression) Range added in v0.53.0

func (me *WrappedExpression) Range() hcl.Range

Range implements hclsyntax.Expression.

func (*WrappedExpression) StartRange added in v0.53.0

func (me *WrappedExpression) StartRange() hcl.Range

StartRange implements hclsyntax.Expression.

func (*WrappedExpression) Value added in v0.53.0

func (me *WrappedExpression) Value(ectx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)

Value implements hclsyntax.Expression.

func (*WrappedExpression) Variables added in v0.53.0

func (me *WrappedExpression) Variables() []hcl.Traversal

Variables implements hclsyntax.Expression.

Jump to

Keyboard shortcuts

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