Skip to main content

Module expression

Module expression 

Source
Expand description

A minimal boolean expression language for approval rules.

An Expression is parsed once, when the workflow is built, and evaluated against a JSON context when an approval gate opens. The language is small on purpose: dotted paths, literals, comparisons, &&, ||, ! and parentheses.

§Grammar

expr    := or
or      := and ("||" and)*
and     := unary ("&&" unary)*
unary   := "!" unary | primary
primary := "(" expr ")" | operand (cmp operand)?
cmp     := == | != | > | >= | < | <=
operand := path | literal
path    := ident ( "." ident | "[" string "]" | "[" integer "]" )*
ident   := [A-Za-z_][A-Za-z0-9_-]*
literal := number | "str" | 'str' | true | false | null

The root identifier of every path must be one of EXPRESSION_ROOTS: output, payload, labels, metadata or steps.

§Evaluation semantics

Evaluation is total: it never fails.

  • A path that does not resolve (missing key, out-of-range index, indexing into a scalar) is null.
  • == and != use JSON equality, with numbers compared as f64. When one side is a number and the other a string that parses as a number, they are compared numerically, because labels are always strings (labels.priority > 3 works with priority = "5"). != is the negation of ==.
  • >, >=, < and <= compare numbers numerically (with the same coercion) and strings lexicographically. Any other pair of types is false.
  • A bare operand is tested for truthiness: null, false, 0, "", [] and {} are false, everything else is true.

§Examples

use ironflow_engine::expression::Expression;
use serde_json::json;

let expr = Expression::parse("payload.amount > 10000 && labels.env == 'production'")?;
let ctx = json!({
    "payload": {"amount": 15000},
    "labels": {"env": "production"},
});
assert!(expr.evaluate(&ctx));

Structs§

Expression
A parsed boolean expression over a JSON context.

Enums§

ExpressionError
Error returned when an expression fails to parse.

Constants§

EXPRESSION_ROOTS
Root identifiers a path may start with.
MAX_EXPRESSION_DEPTH
Maximum nesting depth of parentheses and ! operators.
MAX_EXPRESSION_LEN
Maximum length of an expression source, in bytes.