Documentation
¶
Overview ¶
Package expr compiles and evaluates the expressions of a Procedure, the definition of a durable run.
The expressions are CEL (https://cel.dev). A Procedure holds them in two forms:
- Conditions are bare CEL expressions that evaluate to a bool: the waitUntil of a step, which a run waits for, and the when of a transition, which decides where a run goes once a state's steps are done.
- Templates are the args of a call or of a waitUntil step's observe: any JSON value in which a string may hold ${ <CEL> }. A string that is exactly one ${ … } becomes the expression's value, of whatever type; a ${ … } inside longer text is replaced by the value's text. $${ writes a literal ${. Object keys are kept as they are.
Names ¶
An expression reads "subject", what the run is about, "run", the run itself, and the id of every step of the procedure, which holds that step's answer. Each is dynamically typed: its fields are checked when the expression runs, not when it is compiled. A name the run has no value for yet, such as a step that hasn't run, is null; reading a field of it is an error, so has(), the optional field syntax (step.?field.orValue(…)) or a comparison with null guard reads that may come before the value does.
Values come from JSON, so every number is a double. Comparisons between numbers of different types work (count == 2, count < 3.5), arithmetic doesn't: CEL adds and multiplies only numbers of one type, so count + 1 is an error where count + 1.0 and int(count) + 1 are not. A template value is returned as plain Go: nil, bool, int64, uint64, float64, string, []any and map[string]any.
Limits ¶
Besides the CEL standard library an expression may use the string extensions and optional types. Each evaluation has a cost limit and a time limit, so a large comprehension fails instead of stalling the run.
Durations ¶
ParseDuration reads a step's deadline: a Go duration that may also use d for days, such as 30m, 1h or 1d12h.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnknown = errors.New("an observation has not answered")
ErrUnknown means a source has yet to answer.
Functions ¶
func DeferredError ¶
DeferredError lets CEL propagate a let binding's error only if it is read.
func ParseDuration ¶
ParseDuration parses a Go duration that may also use d (24h) as a unit: "1d", "1d12h", "30m", "1h", "1.5h". A duration that is negative or zero is an error.
func UnknownNames ¶
UnknownNames returns the sorted root variable names carried by an unknown evaluation error, or nil when err carries none.
Types ¶
type Condition ¶
type Condition struct {
// contains filtered or unexported fields
}
Condition is a compiled bare CEL expression that must be bool: a step's waitUntil or a transition's when.
func (*Condition) Holds ¶
Holds evaluates the condition. Variables activation has no value for are null.
ErrUnknown identifies a condition dependent on an unanswered source. An invalid field lookup or type mismatch returns its evaluation error.
type Env ¶
type Env struct {
// contains filtered or unexported fields
}
Env compiles and evaluates a procedure's expressions.
func NewEnv ¶
NewEnv returns the environment in which expressions read vars, each of them dynamically typed.
func (*Env) Condition ¶
Condition compiles src and checks that it can evaluate to a bool: its type is bool, or dyn, which is checked when it runs.
Example ¶
package main
import (
"fmt"
"github.com/giantswarm/muster/v5/internal/durable/expr"
)
func main() {
env, err := expr.NewEnv(expr.Vars{"subject", "run", "check"})
if err != nil {
panic(err)
}
ready, err := env.Condition("check.status == 'done' && check.replicas >= 3")
if err != nil {
panic(err)
}
// Values come from JSON: replicas is a double.
holds, err := ready.Holds(map[string]any{
"check": map[string]any{"status": "done", "replicas": float64(3)},
})
fmt.Println(holds, err)
// Before the step has answered, check is null and the condition is
// unknown.
holds, err = ready.Holds(nil)
fmt.Println(holds, err)
}
Output: true <nil> false condition "check.status == 'done' && check.replicas >= 3": no such key: status
func (*Env) Template ¶
Template parses value, decoded JSON (map[string]any, []any, string, float64, bool or nil), and compiles every ${ … } in it.
A string that is exactly one ${ <expression> } is replaced by the expression's value, of any type. In a longer string each ${ … } is replaced by its value's text: a string as it is, null as nothing, anything else as JSON. $${ is a literal ${. Strings without ${ and object keys are kept as they are.
Example ¶
package main
import (
"fmt"
"github.com/giantswarm/muster/v5/internal/durable/expr"
)
func main() {
env, err := expr.NewEnv(expr.Vars{"subject", "run"})
if err != nil {
panic(err)
}
// size comes from JSON, so it is a double: arithmetic with it takes
// doubles (2.0) or converts it (int(subject.size)).
args, err := env.Template(map[string]any{
"cluster": "${ subject.name }",
"replicas": "${ int(subject.size) * 2 }",
"reason": "scale ${ subject.name } to ${ subject.size * 2.0 }",
"literal": "$${ kept }",
})
if err != nil {
panic(err)
}
out, err := args.Render(map[string]any{
"subject": map[string]any{"name": "alpha", "size": float64(2)},
})
fmt.Println(out, err)
}
Output: map[cluster:alpha literal:${ kept } reason:scale alpha to 4 replicas:4] <nil>
type Signals ¶
Signals holds the JSON values received so far. A signal that has not yet arrived is unknown in an expression, including after other signals arrive.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a compiled argument tree: any JSON value in which strings may hold ${ <CEL> }.
type UnknownError ¶
type UnknownError struct {
Names []string
}
UnknownError wraps ErrUnknown with the root variable names that produced CEL's unknown result.
func (*UnknownError) Error ¶
func (e *UnknownError) Error() string
func (*UnknownError) Unwrap ¶
func (e *UnknownError) Unwrap() error