expr

package
v0.0.0-...-9205218 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 9 Imported by: ? 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 implements OneGov's Expression V2 language and transition conditions, including the parts of the real engine that surprise people: references are pasted into the text before parsing, and how they are pasted is configurable (see Policy) until replay against real instances settles it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Refs

func Refs(src string) []string

Refs lists the references in a source, braced ($.{x}) or bare ($.x), in order of appearance, without duplicates.

Types

type ConditionResult

type ConditionResult struct {
	Pass bool
	// Unresolved lists references in the condition that had no value. The
	// condition still evaluates (they count as blank), but a transition
	// that depends on a value nobody produced is worth a look.
	Unresolved []string
	Err        error
}

ConditionResult is an evaluated transition condition.

func EvalCondition

func EvalCondition(src string, ctx Context, p Policy) ConditionResult

EvalCondition evaluates a transition condition. An empty condition passes.

type Context

type Context interface {
	Lookup(path string) (Value, bool)
}

Context resolves references. Paths are dotted, without the "$." prefix: "applicantID", "n9.output.total", "n20.form-slug.field".

type EvalError

type EvalError struct {
	Pos int
	Msg string
}

EvalError is a runtime failure: wrong types, an unknown function, a division by zero.

func (*EvalError) Error

func (e *EvalError) Error() string

type Hazard

type Hazard struct {
	Code    string
	Ref     string // the reference involved, if any
	Message string
	Fails   bool // true if it fails the instance, false if it is silently wrong
}

Hazard is a pattern in an expression's source that is known to break, or to quietly give the wrong answer, in the live engine.

func Hazards

func Hazards(src string) []Hazard

Hazards checks an Expression V2 source before substitution. Every rule here comes from a real failure written up in the workflow-expressions pitfalls checklist.

type Kind

type Kind int
const (
	Null Kind = iota
	Number
	String
	Bool
	List
	Map
)

func (Kind) String

func (k Kind) String() string

type MapContext

type MapContext map[string]any

MapContext is a Context over a plain map, for tests and fixtures.

func (MapContext) Lookup

func (m MapContext) Lookup(path string) (Value, bool)

type Missing

type Missing int

Missing says what happens to a reference with no value.

const (
	// KeepLiteral leaves $.{path} in the text. This is Expression V2
	// (ctrl 22): the literal reaches the parser and the instance fails.
	KeepLiteral Missing = iota
	// Blank replaces it with nothing. Every other control does this
	// (replace_unmatched = ""), silently.
	Blank
)

type Mode

type Mode int

Mode says which language is being parsed.

const (
	// ExpressionMode is Expression V2 after substitution: no references may
	// remain, and a leftover $.{...} is a syntax error.
	ExpressionMode Mode = iota
	// ConditionMode is a transition condition: bare $.n12.state references
	// are allowed and AND / OR work as well as && / ||.
	ConditionMode
)

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node is a parsed expression.

func Parse

func Parse(src string, mode Mode) (Node, error)

Parse parses src. It does not substitute references; see Substitute.

type Policy

type Policy struct {
	// Quote wraps string values pasted into Expression V2. The live engine
	// uses a double quote; test_workflow_expression uses a single quote,
	// which is why the sandbox hides nested-quote failures.
	// (workflow-expressions SKILL.md, core rule 8)
	Quote byte

	// QuoteNull: whether a null value is pasted as "" or as nothing.
	// Confirmed on a recorded Recorded instance: with dob2 null,
	// ifCondition( $.{dob2} == ”, ”, ... ) produced ” and did not
	// fail, which only parses if the null arrived as "". (The docs' "bare
	// empty substitution is a parse error" does not apply to nulls.)
	QuoteNull bool

	// Templates: an expression that is one double-quoted string with
	// references inside is filled in as plain text, not quoted values. Core
	// rule 1 recommends exactly that pattern for text outputs, and it could
	// not parse otherwise.
	Templates bool

	// LooseEquality for Expression V2 ==. The function reference says both
	// operands must be the same type, so 5 == '5' is false.
	LooseEquality bool

	// ConditionLooseEquality for transition conditions. Conditions compare
	// counts as $.n3.count != "0" while the context stores the number 0;
	// those workflows run correctly in production, so the engine must treat
	// them as equal.
	ConditionLooseEquality bool

	// Location for currentDate and friends. Maldives time unless set.
	Location *time.Location
	// Now is the clock. time.Now unless set; fix it in tests and fixtures.
	Now func() time.Time
}

Policy holds the engine behaviours that the documentation describes but that have not yet been confirmed against recorded instances. Each field names the source it comes from. Replay against real runs (milestone 4) will either confirm the defaults or change them.

func Live

func Live() Policy

Live is the best current model of the production engine.

func Sandbox

func Sandbox() Policy

Sandbox models test_workflow_expression, which quotes with single quotes.

type Result

type Result struct {
	Value       Value
	Substituted string // the text the parser actually saw
	Subs        []Sub
	Err         error
}

Result is one evaluated Expression V2 output.

func EvalExpression

func EvalExpression(src, returnType string, ctx Context, p Policy) Result

EvalExpression runs one Expression V2 output: substitute, parse, evaluate, then convert to the declared return type.

type Sub

type Sub struct {
	Ref      string // path inside $.{...}
	Found    bool
	Value    Value
	Rendered string // what went into the text
}

Sub records one reference replacement, for traces.

func Interpolate

func Interpolate(text string, ctx Context) (string, []Sub)

Interpolate fills references in plain text (task titles, SMS bodies, DataHub values): no quoting, and a missing value becomes blank, as on every control other than Expression V2.

func Substitute

func Substitute(src string, ctx Context, p Policy, missing Missing) (string, []Sub)

Substitute pastes reference values into src the way the engine does, before anything is parsed.

type SyntaxError

type SyntaxError struct {
	Pos int
	Msg string
}

SyntaxError is a parse failure. In the live engine this fails the instance.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Value

type Value struct {
	Kind Kind
	Num  float64
	Str  string
	Bool bool
	List []Value
	Map  map[string]Value
}

Value is a value in an expression or in the workflow context.

func Boolean

func Boolean(b bool) Value

func Eval

func Eval(src string, mode Mode, ctx Context, policy Policy) (Value, error)

Eval parses and evaluates a source that needs no substitution.

func FromAny

func FromAny(x any) Value

FromAny converts decoded JSON (or plain Go values in tests) to a Value.

func NullValue

func NullValue() Value

func Num

func Num(f float64) Value

func Str

func Str(s string) Value

func (Value) Get

func (v Value) Get(path []string) (Value, bool)

Get walks a dotted path ("output.0.name") into lists and maps.

func (Value) IsNull

func (v Value) IsNull() bool

func (Value) String

func (v Value) String() string

func (Value) Text

func (v Value) Text() string

Text renders a value the way it appears when pasted into an expression or shown in a field, without quotes.

Jump to

Keyboard shortcuts

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