script

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EachValue added in v0.0.2

func EachValue(value any) ([]any, error)

EachValue converts a plain Go value into a slice of items for iteration by the workflow engine's `each` blocks. Scripting engines should use this helper when implementing Value.Items for values that are already Go-native.

Slices and arrays become a []any of their elements. Maps with string keys become a list of {"key", "value"} pairs ordered by sorted key so iteration is deterministic. Scalars become single-element slices. Unknown types return an error.

func IsTruthyValue added in v0.0.2

func IsTruthyValue(value any) bool

IsTruthyValue returns whether a plain Go value should be treated as truthy in workflow conditions. Scripting engines should use this helper when implementing Value.IsTruthy for values that are already Go-native.

The rules are intentionally pragmatic for workflow authoring:

  • bool: itself
  • numeric: non-zero is truthy
  • string: non-empty and not "false" (case-insensitive)
  • slices/maps (any element or key type): non-empty is truthy
  • nil: false
  • anything else: non-nil is truthy

Types

type Compiler

type Compiler interface {
	Compile(ctx context.Context, code string) (Script, error)
}

Compiler is an interface used to compile source code into a Script.

type Script

type Script interface {
	Evaluate(ctx context.Context, globals map[string]any) (Value, error)
}

Script represents a compiled script that can be evaluated.

type Template

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

Template is a parsed ${...} template. Templates are the only interpolation syntax in the workflow engine. A template may be:

  1. Literal — contains no ${...} tokens. Eval returns the raw string.
  2. Single-expression — the whole trimmed raw string is exactly one ${expr}. Eval returns the typed value produced by the script engine (preserving number, bool, array, map, etc.).
  3. Interpolated — the raw string mixes literal text with one or more ${expr} tokens. Eval returns a concatenated string, with each expression value stringified via Value.String().

This is "contextual type inference": callers wishing to pass a typed value (number, bool, slice) through a parameter write the whole value as a single ${...} token; callers building a string (URLs, messages, topics) interpolate tokens inside surrounding text and get a string back.

func NewTemplate

func NewTemplate(engine Compiler, raw string) (*Template, error)

NewTemplate parses raw as a ${...} template and compiles every expression it contains against engine. Returns an error if any expression is syntactically malformed (unclosed brace) or fails to compile.

func (*Template) Eval

func (e *Template) Eval(ctx context.Context, globals map[string]any) (any, error)

Eval evaluates the template. For literal templates the raw string is returned as-is. For single-expression templates the script's typed value is returned. For interpolated templates the result is the concatenated string with each expression stringified.

func (*Template) EvalString added in v0.0.2

func (e *Template) EvalString(ctx context.Context, globals map[string]any) (string, error)

EvalString evaluates the template and always returns a string. For single-expression templates this stringifies the typed value; use Eval instead when preserving the underlying type matters.

type Value

type Value interface {

	// Value returns the Go value for this value as an any
	Value() any

	// Items returns the items for this value as an array of any
	Items() ([]any, error)

	// String returns the string representation of this value
	String() string

	// IsTruthy returns true if this value is truthy
	IsTruthy() bool
}

Value represents the result of a script evaluation.

Jump to

Keyboard shortcuts

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