Documentation
¶
Overview ¶
Package expr evaluates the expressions and {{ }} templates of a workflow.
Index ¶
- Variables
- func CallsTemplate(v any) bool
- func CheckTypes(input string, names map[string]Shape, wantBool bool) error
- func HasTemplate(s string) bool
- func Hints(input string, err error) []string
- func MethodCallHints(input string) []string
- func NameFor(name string) string
- func Names(input string) []string
- func Parse(input string) error
- func Reads(input, name string) [][]string
- func ReadsByVariableKey(input, name string) [][]string
- func ReadsNothing(input string) bool
- func Refs(v any, name string) (keys []string, dynamic bool)
- func ReplaceTemplates(s, repl string) string
- func TemplateExprs(s string) []string
- type Clause
- type Expr
- func (e *Expr) Eval(input string, env any) (any, error)
- func (e *Expr) EvalTemplate(input string, env any) (string, error)
- func (e *Expr) EvalTemplateMap(input map[string]any, env any) (map[string]any, error)
- func (e *Expr) EvalTemplateWithTypePreservation(input string, env any) (any, error)
- func (e *Expr) FalseClause(input string, env any) (Clause, bool)
- func (e *Expr) Options(env any) []ex.Option
- type FieldError
- type HintError
- type Operand
- type Shape
- type TemplateError
Constants ¶
This section is empty.
Variables ¶
var ErrNotBool = errors.New("not true or false")
ErrNotBool is the error of an expression that is to come to true or false, and that is known to come to a value of another kind.
Functions ¶
func CallsTemplate ¶
CallsTemplate reports whether a template in v calls template, whose argument is evaluated as templates once it runs, so that what it reads cannot be known before then. v is as Refs takes it.
func CheckTypes ¶
CheckTypes returns what is sure to go wrong with input, an expression, by the kinds of the values it reads, which names gives the shapes of: an operator given values it does not take, as "id-" + 1 is, which fails, and a comparison of values of kinds that are never equal, as res.code == "200 OK" is where res.code is a number, which is never true. With wantBool, an expression whose value is known not to be true or false, as a test's must be, is an error too, which wraps ErrNotBool.
A value whose kind is not known, as what is read from a key no shape gives and what a function of Probe returns, is taken to be of the kind that would be right, so that nothing is told that may not go wrong. A part the checker refuses that may not go wrong, such as an index of a string, is set aside as such a value, and the rest is checked. An expression that does not parse, which Parse tells of, has nothing told here.
The error says what is wrong with the expression as written, and how it is likely meant, as a *HintError.
func HasTemplate ¶
HasTemplate reports whether s holds a {{ }} template, as the evaluation of s finds one: a "{{" that is never closed, or a "}}" alone, is text.
func Hints ¶
Hints returns how input, an expression that fails with err, is likely meant to be written where it is written as another language would write it: a method expressions do not have, such as res.body.contains("x") for res.body contains "x", .length for len(), === for ==, or null for nil. Some of these, such as .length or null, are not wrong in themselves, a map may have a key named length, so they are told only of an expression that fails, and .length only where err says it fails.
func MethodCallHints ¶
MethodCallHints returns how input, an expression, is meant to be written where it calls a method expressions do not have, such as res.body.contains("x"), which fails whatever it is called on: no value an expression reads has methods of these names. It returns nothing for an expression that does not parse.
func NameFor ¶
NameFor returns what name, which expressions do not have but another language does, is written as in expressions, such as nil for null, or an empty string.
func Names ¶
Names returns the names that input, an expression, reads from what it is evaluated against, in the order they first come: vars and outputs in vars.id == outputs.login.id. A function it calls, a name let declares and $env, which is what it is evaluated against as a whole, are not among them. An expression that does not parse reads nothing here.
func Parse ¶
Parse reports whether input is an expression that parses, without evaluating it, so that a workflow can be checked before it runs. Its error tells how input is likely meant to be written where it is written as another language would write it, as a *HintError.
func Reads ¶
Reads returns the constant keys that input, an expression, reads from the object name, as chains: outputs.auth.token is read as [auth token], and as [auth] too. A key read in a way that cannot be known before it runs, such as name[k], is not returned. An expression that does not parse reads nothing here.
func ReadsByVariableKey ¶
ReadsByVariableKey returns where input, an expression, reads the object name by a key that cannot be known before it runs, as the chains of the constant keys read before that key: outputs[vars.id].token is read as [], and outputs.auth[k] as [auth]. An expression that does not parse reads nothing here.
func ReadsNothing ¶
ReadsNothing reports whether input, an expression that parses, reads no variable, so that it evaluates the same whatever it is evaluated against, as true or 1 == 1 does. A function it calls, such as now(), is not a variable.
func Refs ¶
Refs returns the names that the templates in v read from the object name, as name.key or name['key']. v is a string, or a map or array of them at any depth, as a value under vars is. dynamic is true when a template reads the object in a way whose key cannot be known before it runs, such as name[k] or the object as a whole, so that it may read any key. keys come in the order they are first read, a map's values in the order of its sorted keys.
An expression that does not parse reads nothing here; evaluating it reports the error.
func ReplaceTemplates ¶
ReplaceTemplates returns s with repl in place of each {{ }} template in it, for what is written around the templates to be read without them.
func TemplateExprs ¶
TemplateExprs returns the expressions of the {{ }} templates in s, in the order they come.
Types ¶
type Clause ¶
type Clause struct {
// Expr is the condition, as the parser writes it back: res.code == 200.
Expr string
// Operands are what the condition compares, with what each evaluated
// to. A literal, such as the 200 above, is left out, as it says what it
// is.
Operands []Operand
}
Clause is one of the conditions an expression joins with &&, as each line of a test usually is.
type Expr ¶
type Expr struct {
// BeforeTemplate, when it is set, is called with the text template() is
// about to expand, at any depth. An error it returns stops the
// evaluation, and is found in the error returned with errors.As.
BeforeTemplate func(text string) error
// contains filtered or unexported fields
}
Expr evaluates expressions and templates against an environment such as a step's context.
func (*Expr) Eval ¶
Eval evaluates input, an expression, against env and returns its value. When input cannot be evaluated, the value is nil.
func (*Expr) EvalTemplate ¶
EvalTemplate replaces each {{ }} template in input with the value of its expression. A template that does not compile or fails to run is an error naming the template, and no string is returned: a value with the error written into it would otherwise be sent on, as a URL or a header, as if it were right.
func (*Expr) EvalTemplateMap ¶
EvalTemplateMap evaluates every template in the keys and values of input, at any depth, keeping the type of a value that is a single template and evaluating a key as text. The errors of all the keys and values that could not be evaluated are joined, each as a *FieldError naming the value by its keys as written. A key that comes to the same key as another is an error too. The map is returned with the errors, so that a caller can still see what could be evaluated, such as credentials to hide from the errors: it holds nil where a value could not be evaluated, and leaves out a key that could not be, or that came to a key taken.
func (*Expr) EvalTemplateWithTypePreservation ¶
func (*Expr) FalseClause ¶
FalseClause returns the first of the conditions input joins with && that is not true against env, so that a test of several conditions that failed can say which one did. An expression that joins none is one condition.
The conditions are evaluated again, each on its own, after the expression was found false as a whole. One that calls a function that returns another value each time, such as random_int, could then be told as true, or with a value it did not have, so nothing is told of an expression that calls one.
It reports false when no condition can be told: input does not parse, calls such a function, or a condition cannot be evaluated on its own.
func (*Expr) Options ¶
Options builds the expr options used to compile every workflow expression.
Two guards that used to live here have been removed because neither did anything. ex.DisableBuiltin had no effect on all / any / one / filter / map / count: expr's parser recognises those as predicate builtins before it looks at the disabled set (parser.go, the `predicates` table), so the calls were dead and the functions have always been reachable. A per-key blocklist that filtered the environment was equally inert, because the filtered copy only ever reached the type checker while ex.Run receives the caller's env, and enforcing it would have broken the documented way of reading credentials into `vars`.
What still bounds an expression is the length check in checkLength, the evaluation timeout in executeWithTimeout, and the argument limits on the functions registered below.
type FieldError ¶
type FieldError struct {
Path string
// EvaluatedPath is Path with the keys that hold templates as they were
// evaluated, which is what tells a value such as a header named by a
// template apart. It is empty where it is not known.
EvaluatedPath string
Err error
}
FieldError is an error in the value under a key of a map evaluated by EvalTemplateMap. Path names the value from the top of that map, as in headers.authorization or items[0].name.
func (*FieldError) Error ¶
func (e *FieldError) Error() string
func (*FieldError) Unwrap ¶
func (e *FieldError) Unwrap() error
type HintError ¶
HintError is an error of an expression written as another language, such as JavaScript or Python, would write it, with how expressions write it.
type Shape ¶
Shape is what is known of a value before an expression reads it: its kind, when that is known, and for a map, the shapes of the keys that are known. Kind is number, string, bool, map or list; a value of any other kind or none, and a key of a map that is not among its fields, may hold anything. A shape with fields and no kind is of a map. Elem is, for a list, the shape of each of its entries, or nil when they may hold anything.
type TemplateError ¶
type TemplateError struct {
Template string // The template as written, braces included
Err error // Why it could not be evaluated
}
TemplateError is a {{ }} template that could not be evaluated.
func (*TemplateError) Error ¶
func (e *TemplateError) Error() string
func (*TemplateError) Unwrap ¶
func (e *TemplateError) Unwrap() error