lang

package
v0.71.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 30 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"
View Source
const UserFuncBlockType = "func"

Variables

View Source
var AdapedConcatFuncSpec = &function.Spec{
	Description: `Concatenates together all of the given lists or tuples into a single sequence, preserving the input order.`,
	Params:      []function.Parameter{},
	VarParam: &function.Parameter{
		Name:        "seqs",
		Type:        cty.DynamicPseudoType,
		AllowMarked: true,
	},
	Type: func(args []cty.Value) (ret cty.Type, err error) {
		if len(args) == 0 {
			return cty.NilType, fmt.Errorf("at least one argument is required")
		}

		etys := make([]cty.Type, 0, len(args))
		for i, val := range args {

			val, _ := val.UnmarkDeep()

			ety := val.Type()
			switch {
			case ety.IsTupleType():
				etys = append(etys, ety.TupleElementTypes()...)
			default:
				return cty.NilType, function.NewArgErrorf(
					i, "all arguments must be lists or tuples; got %s",
					ety.FriendlyName(),
				)
			}
		}
		return cty.Tuple(etys), nil
	},

	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		switch {
		case retType.IsTupleType():

			vals := make([]cty.Value, 0, len(args))

			for _, seq := range args {
				id := xid.New().String()

				seq, seqMarks := seq.Unmark()

				it := seq.ElementIterator()
				for it.Next() {
					_, v := it.Element()
					v, umrk := v.Unmark()
					vals = append(vals, v.WithMarks(seqMarks).Mark(&mergeMarker{umrk, id}))
				}
			}

			return cty.TupleVal(vals), nil
		default:

			panic("unsupported return type")
		}
	},
}

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

View Source
var AdaptedMergeFuncSpec = &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
			}

			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)

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

			for it := arg.ElementIterator(); it.Next(); {
				k, v := it.Element()
				v, umrk := v.Unmark()
				outputMap[k.AsString()] = v.WithMarks(argMarks).Mark(&mergeMarker{umrk, xid})
			}
		}

		switch {
		case retType.IsObjectType(), retType.Equals(cty.DynamicPseudoType):
			return cty.ObjectVal(outputMap), nil
		default:
			panic(fmt.Sprintf("unexpected return type: %#v", retType))
		}
	},
}

AdaptedMergeFuncSpec is a copy of the MergeFunc spec 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.

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

		if len(args) == 0 {
			return cty.NilType, errors.New("at least one argument is required")
		}

		first := args[0].Type()
		if first.IsListType() || first.IsTupleType() {
			return AdapedConcatFuncSpec.Type(args)
		}

		return AdaptedMergeFuncSpec.Type(args)
	},

	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		if retType.IsListType() || retType.IsTupleType() {
			return AdapedConcatFuncSpec.Impl(args, retType)
		}
		return AdaptedMergeFuncSpec.Impl(args, retType)
	},
})

CombinedAdaptedMergeConcatFunc is a dynamic function that can merge maps or arrays it looks at the first argument to determine if it should merge maps or arrays and then delegates to the appropriate function. The AdaptedMergeFuncSpec and AdapedConcatFuncSpec are copies of the MergeFunc and ConcatFunc specs from cty stdlib, but with the ability to merge marked parents into their children.

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 *SudoContext) (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 LoadGlobalEnvVars added in v0.71.0

func LoadGlobalEnvVars(fs afero.Fs, opts *LoadGlobalEnvVarOpts) (map[string]string, error)

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, env map[string]string) (*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 NewSorterList added in v0.60.0

func NewSorterList(val cty.Value) (*Sorter, []*Sorter)

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, enhance SortEnhancer) (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) Enhance added in v0.60.0

func (*BasicBlockMeta) Enhance(v cty.Value, ok []any) (cty.Value, error)

Enhance implements BlockMeta.

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
	Enhance(v cty.Value, ok []any) (cty.Value, error)
}

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) Enhance added in v0.60.0

func (me *GenBlockMeta) Enhance(val cty.Value, ok []any) (cty.Value, error)

Enhance implements SortEnhancer.

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) Enhance added in v0.60.0

func (me *IncomleteBlockMeta) Enhance(v cty.Value, ok []any) (cty.Value, error)

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

type LoadGlobalEnvVarOpts struct {
	GoModFileName  string
	DotEnvFileName string
}

type Meta added in v0.53.0

type Meta interface {
	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 SortEnhancer added in v0.60.0

type SortEnhancer interface {
	Enhance(cty.Value, []any) (cty.Value, error)
}

type Sorter added in v0.53.0

type Sorter struct {
	OGValue    cty.Value
	Range      []hcl.Range
	MergeRange []mergeRange
	Key        string
	Value      cty.Value
	Ignored    bool
	KnownMarks []any
	// contains filtered or unexported fields
}

func NewSorter added in v0.60.0

func NewSorter(key string, og cty.Value) *Sorter

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

func FilterSudoContextWithRegex(sctx []*SudoContext, keys []string, reg cty.Value) ([]*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) GetAllUserFuncs added in v0.62.0

func (wc *SudoContext) GetAllUserFuncs() map[string]function.Function

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) Match added in v0.67.0

func (me *SudoContext) Match(keys []string, val cty.Value) (bool, hcl.Diagnostics)

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) ParentBlockMeta added in v0.60.0

func (wc *SudoContext) ParentBlockMeta() BlockMeta

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