resonator

package
v0.2.34 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Branch

type Branch struct {
	Path string `json:"path,omitempty"`
}

type BranchValue

type BranchValue struct {
	Path  string    `json:"path,omitempty"`  // .branch.subbranch
	Value ast.Value `json:"value,omitempty"` // ast.Literal{V: 42} today; ast.PathRef / ast.FunctionCall reserved
}

type Condition

type Condition struct {
	Star       bool        `json:"isStar,omitempty"`     // true = matches everything
	Branch     *Branch     `json:"branch,omitempty"`     // .branch.subbranch
	MatchType  MatchType   `json:"matchType,omitempty"`  // ==
	MatchValue interface{} `json:"matchValue,omitempty"` // 5
	// MatchCall is the function-call RHS form: `WHEN .h == &tz("UTC",
	// "hour", 9)`. Exactly one of MatchValue / MatchCall is set. The
	// parser guarantees every argument (recursively) is a literal — no
	// PathRefs — so the call is envelope-independent: a computed
	// constant, not a second path. That restriction is load-bearing:
	// it keeps path-vs-path comparison inexpressible in WHEN, which is
	// what the key-shape authorization pattern relies on.
	MatchCall *ast.FunctionCall `json:"matchCall,omitempty"`
	Adds      *BranchValue      `json:"adds,omitempty"`   // for select, adding branches
	Prunes    *BranchValue      `json:"prunes,omitempty"` // for select, removing branches
}

type Loop added in v0.2.28

type Loop struct {
	// Until is the exit predicate, WHEN's grammar exactly (path-vs-
	// literal only, comma is AND), evaluated after every pass.
	Until *WhenExpr `json:"until,omitempty"`
	// Max is the pass ceiling. 0 means the chassis default (10); the
	// chassis also caps it with --op-loop-max.
	Max int64 `json:"max,omitempty"`
	// Every is the pause between passes. 0 means the chassis default
	// (50ms); the chassis floors it at 2ms. Checked after Until, so
	// the last pass never pays it.
	Every time.Duration `json:"every,omitempty"`
	// Set is applied before every repeat (never before the first
	// pass): resolved against the accumulated view and written onto
	// the op's input and the view. Same value forms as SET. This is
	// how a pass feeds a cursor or counter to the next one.
	Set []BranchValue `json:"set,omitempty"`
}

Loop is the parsed `LOOP [EVERY <duration>] [SET …] UNTIL <predicate> [MAX <n>]` clause. The processor re-executes the rule's EXEC inside its dispatch goroutine, merging each pass locally and handing the scope one payload, until Until holds against the accumulated view or a budget runs out.

type MatchType

type MatchType string
const (
	LT     MatchType = "lt"
	LT_EQ  MatchType = "lteq"
	GT     MatchType = "gt"
	GT_EQ  MatchType = "gteq"
	EQ     MatchType = "eq"
	NOT_EQ MatchType = "ne"
)

type Phrase

type Phrase struct {
	Type     PhraseType
	When     *When                `json:"when,omitempty"`
	Select   *Select              `json:"select,omitempty"`
	SetPre   *Set                 `json:"setPre,omitempty"`  // set before select
	SetPost  *Set                 `json:"setPost,omitempty"` // set after select
	With     map[string]ast.Value `json:"with,omitempty"`
	Priority int64                `json:"priority"`
	Exec     string               `json:"exec,omitempty"`
	Emit     *Set                 `json:"emit,omitempty"`
	Loop     *Loop                `json:"loop,omitempty"`
}

type PhraseType

type PhraseType string
const (
	WHEN     PhraseType = "WHEN"
	SETPRE   PhraseType = "SETPRE"
	SELECT   PhraseType = "SELECT"
	SETPOST  PhraseType = "SETPOST"
	WITH     PhraseType = "WITH"
	PRIORITY PhraseType = "PRIORITY"
	EXEC     PhraseType = "EXEC"
	EMIT     PhraseType = "EMIT"
	LOOP     PhraseType = "LOOP"
)

type Resonator

type Resonator struct {
	When     *When                `json:"when,omitempty"`
	Select   *Select              `json:"select,omitempty"`
	SetPre   *Set                 `json:"setPre,omitempty"`  // set before select
	SetPost  *Set                 `json:"setPost,omitempty"` // set after select
	With     map[string]ast.Value `json:"with,omitempty"`
	Priority int64                `json:"priority"`
	Exec     string               `json:"exec,omitempty"`
	// Emit overlays values onto THIS rule's response after EXEC,
	// before the per-scope merge. Overwrite semantics (not the
	// set-if-absent behavior of SET POST). Lets a rule contribute
	// literal fields to the merged scope output without needing a
	// real HTTP/handler call: pair with EXEC for "enrich the
	// response", or write EMIT alone for a "synthetic emitter".
	Emit *Set `json:"emit,omitempty"`
	// Loop is the LOOP clause: re-run this rule's EXEC inside one
	// dispatch until Until holds. nil means the op runs once.
	Loop *Loop `json:"loop,omitempty"`
}

Resonator is the parsed form of one txcl rule. Built by the parser, consumed by the processor at match-and-dispatch time.

func New

func New() *Resonator

func (Resonator) WhenMatches

func (res Resonator) WhenMatches(input string) bool

WhenMatches returns true if the WHEN clause matches the input envelope. Semantics preserved across the grammar v2 upgrade:

  • empty input never matches (caller contract)
  • nil/empty When matches everything (star)
  • legacy When{Conditions: [...]} is promoted to an Expr tree on the fly; any Star condition in the list short-circuits to match
  • new When{Expr: ...} is walked directly

type Select

type Select struct {
	Star        bool               `json:"star,omitempty"`
	Assignments []SelectAssignment `json:"assignments,omitempty"`
}

Select projects the rule's dispatched input. With one or more assignments, the input the EXEC receives becomes ONLY the assigned destinations plus standard envelope identity (`_ts`, and the `_txc.op`/`_txc.step` runtime stamp for handler transports). Assignments are also overlaid onto the rule's response via a synthetic EMIT, so the writes persist into the merged scope envelope (additive) even when the rule has no EXEC. Star is the explicit "everything" spelling (`SELECT *`): it parses to zero assignments and the projection is a no-op. Distinct from SET (literal RHS only, op-scoped, requires EXEC to commit) and EMIT (literal RHS only, post-EXEC overlay).

type SelectAssignment

type SelectAssignment struct {
	From       string    `json:"from"`
	To         string    `json:"to"`
	Default    ast.Value `json:"default,omitempty"`
	HasDefault bool      `json:"has_default,omitempty"`
}

SelectAssignment is one `<source> AS <dest> [DEFAULT <literal>]` clause inside a SELECT. From and To are envelope paths (with the leading `.` or `@` sugar accepted by the parser, normalized for gjson/sjson at evaluation time). Default is the literal used when From resolves to a missing path or the empty string.

type Set

type Set struct {
	Overrides []BranchValue `json:"overrides,omitempty"`
}

type When

type When struct {
	// Expr is the canonical tree shape produced by the parser. New rules
	// always populate Expr.
	Expr *WhenExpr `json:"expr,omitempty"`
	// Conditions is the legacy flat-AND shape. Programmatic callers (and
	// any in-flight JSON written before the grammar v2 upgrade) populate
	// this; WhenMatches promotes it to an Expr at evaluation time.
	Conditions []Condition `json:"conditions,omitempty"`
}

type WhenExpr

type WhenExpr struct {
	And     []WhenExpr `json:"and,omitempty"`
	Or      []WhenExpr `json:"or,omitempty"`
	Not     *WhenExpr  `json:"not,omitempty"`
	Leaf    Condition  `json:"leaf,omitempty"`
	HasLeaf bool       `json:"has_leaf,omitempty"`
	Star    bool       `json:"star,omitempty"`
}

WhenExpr is the boolean expression tree for a WHEN clause. Exactly one of {And, Or, Not, HasLeaf, Star} is set on each node.

And/Or are n-ary (slices of values), Not is unary and uses a pointer for self-recursion (Go can't embed a value-type into itself). Leaf is held by value with HasLeaf as the discriminator so the tree contains no leaf-level pointers and there's no aliasing back into any source slice.

func (*WhenExpr) Matches added in v0.2.28

func (e *WhenExpr) Matches(input string) bool

Matches evaluates a standalone expression tree against the input envelope. Unlike WhenMatches it carries none of the WHEN-clause conveniences (empty input never matches, nil When matches all): the caller owns those decisions. Used for LOOP … UNTIL, where the processor evaluates the predicate against the view it accumulated across passes.

Jump to

Keyboard shortcuts

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