expr

package
v5.67.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
var ErrUnknown = errors.New("an observation has not answered")

ErrUnknown means a source has yet to answer.

Functions

func DeferredError

func DeferredError(err error) any

DeferredError lets CEL propagate a let binding's error only if it is read.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

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 Unknown

func Unknown(names ...string) any

Unknown marks one or more declared names whose values are unknown in CEL.

func UnknownNames

func UnknownNames(err error) []string

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

func (c *Condition) Holds(activation map[string]any) (bool, error)

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.

func (*Condition) Reads

func (c *Condition) Reads() []string

Reads lists the free variables in the checked condition.

func (*Condition) String

func (c *Condition) String() string

String returns the condition's source.

type Env

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

Env compiles and evaluates a procedure's expressions.

func NewEnv

func NewEnv(vars Vars) (*Env, error)

NewEnv returns the environment in which expressions read vars, each of them dynamically typed.

func (*Env) Condition

func (e *Env) Condition(src string) (*Condition, error)

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

func (e *Env) Template(value any) (*Template, error)

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>

func (*Env) Value

func (e *Env) Value(src string) (*Template, error)

Value compiles bare CEL with an arbitrary JSON result.

func (*Env) With

func (e *Env) With(names ...string) (*Env, error)

With returns a new environment including local bindings.

type PathError

type PathError struct {
	Path string
	Err  error
}

PathError identifies the JSON field whose expression failed to compile.

func (*PathError) Error

func (e *PathError) Error() string

func (*PathError) Unwrap

func (e *PathError) Unwrap() error

type Signals

type Signals map[string]any

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> }.

func (*Template) Reads

func (t *Template) Reads() []string

Reads lists the free variables in this template's checked expressions.

func (*Template) Render

func (t *Template) Render(activation map[string]any) (any, error)

Render evaluates every expression against activation, in which a variable without a value is null, and returns the rendered tree in fresh maps and slices. The error of an expression names it.

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

type Vars

type Vars []string

Vars are the names an expression may read: "subject", "run", and the id of every step of the procedure.

Jump to

Keyboard shortcuts

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