funcs

package
v0.17.2 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

c matches character c (c != '\\', '-', ']') '\\' c matches character c lo '-' hi matches character c for lo <= c <= hi

Index

Constants

This section is empty.

Variables

View Source
var AllTrueFunc = function.New(&function.Spec{
	VarParam: &function.Parameter{
		Name: "clauses",
		Type: cty.Bool,
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		return AllTrue(args)
	},
})

alltrue() takes a variable number of bool arguments and returns true if all of them are true.

View Source
var AnyTrueFunc = function.New(&function.Spec{
	VarParam: &function.Parameter{
		Name: "clauses",
		Type: cty.Bool,
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		if len(args) == 0 {
			return cty.False, nil
		}

		for _, clause := range args {
			if clause.True() {
				return cty.True, nil
			}
		}

		return cty.False, nil
	},
})

anytrue() takes a variable number of bool arguments and returns true if any of them are true.

View Source
var EnvFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "envVarName",
			Type: cty.String,
		},
		{
			Name: "defaultValue",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		envVarName := args[0]
		defaultValue := args[1]
		return Env(envVarName, defaultValue)
	},
})

EnvFunc is a cty.Function that returns an env var or the default value if it doesn't exist

View Source
var ExclusiveGlobFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "patterns",
			Type: cty.DynamicPseudoType,
		},
		{
			Name: "values",
			Type: cty.DynamicPseudoType,
		},
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		values := args[0]
		patterns := args[1]
		return XGlob(values, patterns)
	},
})

A cty.Function that matches a string or list of strings against a glob pattern or list of glob patterns. Returns true if _all_ values match at least one pattern.

View Source
var GlobFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "patterns",
			Type: cty.DynamicPseudoType,
		},
		{
			Name: "values",
			Type: cty.DynamicPseudoType,
		},
	},
	Type: function.StaticReturnType(cty.Bool),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		values := args[0]
		patterns := args[1]
		return Glob(values, patterns)
	},
})

GlobFunc is a cty.Function that matches a string or list of strings against a glob pattern or list of glob patterns. Returns true if _any_ values match at least one pattern.

View Source
var StatelessFunctions = map[string]function.Function{
	"abs":             stdlib.AbsoluteFunc,
	"alltrue":         AllTrueFunc,
	"anytrue":         AnyTrueFunc,
	"versiontmpl":     VersionTemplateFunc,
	"can":             tryfunc.CanFunc,
	"ceil":            stdlib.CeilFunc,
	"chomp":           stdlib.ChompFunc,
	"coalesce":        stdlib.CoalesceFunc,
	"compact":         stdlib.CompactFunc,
	"concat":          stdlib.ConcatFunc,
	"csv":             stdlib.CSVDecodeFunc,
	"env":             EnvFunc,
	"flatten":         stdlib.FlattenFunc,
	"floor":           stdlib.FloorFunc,
	"format":          stdlib.FormatFunc,
	"formatdate":      stdlib.FormatDateFunc,
	"glob":            GlobFunc,
	"indent":          stdlib.IndentFunc,
	"index":           stdlib.IndexFunc,
	"int":             stdlib.IntFunc,
	"join":            stdlib.JoinFunc,
	"jsondecode":      stdlib.JSONDecodeFunc,
	"jsonencode":      stdlib.JSONEncodeFunc,
	"keys":            stdlib.KeysFunc,
	"length":          stdlib.LengthFunc,
	"lookup":          stdlib.LookupFunc,
	"lower":           stdlib.LowerFunc,
	"max":             stdlib.MaxFunc,
	"merge":           stdlib.MergeFunc,
	"min":             stdlib.MinFunc,
	"range":           stdlib.RangeFunc,
	"regex":           stdlib.RegexAllFunc,
	"regexreplace":    stdlib.RegexReplaceFunc,
	"replace":         stdlib.ReplaceFunc,
	"reverse":         stdlib.ReverseFunc,
	"setintersection": stdlib.SetIntersectionFunc,
	"setproduct":      stdlib.SetProductFunc,
	"setunion":        stdlib.SetUnionFunc,
	"slice":           stdlib.SliceFunc,
	"sort":            stdlib.SortFunc,
	"split":           stdlib.SplitFunc,
	"strlen":          stdlib.StrlenFunc,
	"substr":          stdlib.SubstrFunc,
	"timeadd":         stdlib.TimeAddFunc,
	"title":           stdlib.TitleFunc,
	"tobool":          stdlib.MakeToFunc(cty.Bool),
	"tolist":          stdlib.MakeToFunc(cty.List(cty.DynamicPseudoType)),
	"tomap":           stdlib.MakeToFunc(cty.Map(cty.DynamicPseudoType)),
	"tonumber":        stdlib.MakeToFunc(cty.Number),
	"toset":           stdlib.MakeToFunc(cty.Set(cty.DynamicPseudoType)),
	"tostring":        stdlib.MakeToFunc(cty.String),
	"trim":            stdlib.TrimFunc,
	"trimprefix":      stdlib.TrimPrefixFunc,
	"trimspace":       stdlib.TrimSpaceFunc,
	"trimsuffix":      stdlib.TrimSuffixFunc,
	"try":             tryfunc.TryFunc,
	"upper":           stdlib.UpperFunc,
	"values":          stdlib.ValuesFunc,
	"xglob":           ExclusiveGlobFunc,
	"zipmap":          stdlib.ZipmapFunc,
}

StatelessFunctions can be instantiated once

View Source
var VersionTemplateFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "template",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		templateVal := args[0]
		template := templateVal.AsString()

		version, err := TemplateVersion(template)

		return cty.StringVal(version), err
	},
})

VersionTemplateFunc generates a calver or pet version according to a template

Functions

func AllTrue

func AllTrue(values []cty.Value) (cty.Value, error)

func AnyTrue

func AnyTrue(values []cty.Value) (cty.Value, error)

func Env

func Env(envVarName, defaultValue cty.Value) (cty.Value, error)

func File

func File(directory, filename string, files map[string][]byte) (string, error)

File returns the content of a file from the HopsFiles struct.

Default file path is the directory that is passed in.

func FileFunc

func FileFunc(files map[string][]byte, hopsDirectory string) function.Function

FileFunc is a stateful cty function that returns the contents of a file relative to the current .hops file. The data of the file is determined at startup time.

It is stateful because it requires the HopsFiles struct and the hopsDirectory to be passed in.

func Glob

func Glob(values, patterns cty.Value) (cty.Value, error)

func StatefulFunctions

func StatefulFunctions(files map[string][]byte, hopsDirectory string) map[string]function.Function

StatefulFunctions returns a map of all stateful functions that are scoped to a block (and therefore a single .hops file).

These must be added to the eval context for each block as the contents change. This is a workaround to the fact that cty does not allow passing additional context information beyond the parameters of the function. And specifically does not allow passing the eval context.

func Template

func Template(directory, filename string, files map[string][]byte, variables map[string]any) (string, error)

Template returns the evaluated template content of a file from the HopsFiles struct and the passed in variables. Handles special case where "autoescape" is desired.

Default file path is the directory that is passed in. If "autoescape" is true, then the template is wrapped in autoescape tags which protects against dangerous HTML inputs in variables.

func TemplateFunc

func TemplateFunc(files map[string][]byte, hopsDirectory string) function.Function

TemplateFunc is a stateful cty function that evaluates a file and variables using the pongo2 library, which matches the Django template language. It returns the results as a string. It finds the file relative to the current .hops file. The data of the file is determined at startup time.

It is stateful because it requires the HopsFiles struct and the hopsDirectory to be passed in.

func TemplateVersion

func TemplateVersion(template string) (string, error)

func XGlob

func XGlob(values, patterns cty.Value) (cty.Value, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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