dispatch

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package dispatch routes evaluation results to named handler functions. Handlers are registered immutably on a Dispatcher, then composed into [Plan]s that specify which handlers run and under what conditions.

rules: github.com/rhyselsmore/anyexpr/rules

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDuplicateHandler is returned when two handlers share the
	// same name.
	ErrDuplicateHandler = errors.New("dispatch: duplicate handler")

	// ErrUnknownHandler is returned when a plan references a handler
	// name not registered on the dispatcher.
	ErrUnknownHandler = errors.New("dispatch: unknown handler")
)

Functions

This section is empty.

Types

type Dispatched

type Dispatched struct {
	// Handler is the name of the handler.
	Handler string

	// MatchedExpr is the When expression that triggered this handler.
	MatchedExpr string

	// Duration is how long the handler took to execute.
	Duration time.Duration

	// Error is non-nil if the handler returned an error or panicked.
	Error error

	// Panicked is true if the handler panicked (Error will contain
	// the recovered value).
	Panicked bool
}

Dispatched records a single handler invocation.

type Dispatcher

type Dispatcher[E any, A any] struct {
	// contains filtered or unexported fields
}

Dispatcher is an immutable registry of named handlers. Build plans from it to control which handlers run and when.

  • E is the environment type (e.g. Email).
  • A is the actions struct (e.g. EmailActions).

func New

func New[E any, A any](opts ...DispatcherOpt[E, A]) (*Dispatcher[E, A], error)

New creates an immutable Dispatcher with the given handlers.

func (*Dispatcher[E, A]) Describe

func (d *Dispatcher[E, A]) Describe() []HandlerInfo

Describe returns metadata for all registered handlers, in registration order. Useful for introspection — an agent can discover what handlers are available.

func (*Dispatcher[E, A]) Plan

func (d *Dispatcher[E, A]) Plan(opts ...PlanOpt[E, A]) (*Plan[E, A], error)

Plan builds an immutable plan from the dispatcher's registered handlers. Handler names are validated, When expressions are compiled.

type DispatcherOpt

type DispatcherOpt[E any, A any] func(*dispatcherConfig[E, A]) error

DispatcherOpt configures a Dispatcher.

func Handle

func Handle[E any, A any](name string, fn HandlerFunc[E, A], opts ...HandleOpt) DispatcherOpt[E, A]

Handle registers a named handler function with optional description.

func WithLogger

func WithLogger[E any, A any](l *slog.Logger) DispatcherOpt[E, A]

WithLogger sets a structured logger for dispatch events.

type HandleOpt

type HandleOpt func(*handleConfig)

HandleOpt configures a handler registration.

func WithDescription

func WithDescription(desc string) HandleOpt

WithDescription sets a human-readable description for a handler. Surfaced via Dispatcher.Describe for introspection.

type HandlerFunc

type HandlerFunc[E any, A any] func(ctx context.Context, eval *rules.Evaluation[E, A]) error

HandlerFunc is the function signature for dispatch handlers.

type HandlerInfo

type HandlerInfo struct {
	// Name is the handler's registered name.
	Name string
	// Description is the human-readable description, if provided.
	Description string
}

HandlerInfo describes a registered handler.

type Plan

type Plan[E any, A any] struct {
	// contains filtered or unexported fields
}

Plan is an immutable, compiled dispatch plan. Created from a Dispatcher via Plan(). Safe for concurrent use.

func (*Plan[E, A]) Describe

func (p *Plan[E, A]) Describe() []PlanInfo

Describe returns metadata for each handler in the plan, in execution order.

func (*Plan[E, A]) Execute

func (p *Plan[E, A]) Execute(ctx context.Context, eval *rules.Evaluation[E, A]) *Result[E, A]

Execute runs the plan against an evaluation result.

func (*Plan[E, A]) Name

func (p *Plan[E, A]) Name() string

Name returns the plan's name.

type PlanInfo

type PlanInfo struct {
	// Handler is the handler name.
	Handler string
	// Whens lists the gating expressions for this handler. Empty
	// means always runs.
	Whens []string
}

PlanInfo describes a handler entry within a plan.

type PlanOpt

type PlanOpt[E any, A any] func(*planConfig[E, A]) error

PlanOpt configures a Plan.

func Gate

func Gate[E any, A any](expr string) PlanOpt[E, A]

Gate sets a top-level expression that must pass before any handlers execute. If the gate expression returns false, no handlers run.

func Run

func Run[E any, A any](name string, opts ...RunOpt[E, A]) PlanOpt[E, A]

Run includes a named handler in the plan with optional When expressions.

func WithName

func WithName[E any, A any](name string) PlanOpt[E, A]

WithName sets a name for the plan. Used in logging and Debug output.

func WithStrategy

func WithStrategy[E any, A any](s Strategy) PlanOpt[E, A]

WithStrategy sets the execution strategy for the plan. Default is AllContinue.

type Result

type Result[E any, A any] struct {
	// Plan is the name of the plan that was executed.
	Plan string

	// Evaluation is the original evaluation that was dispatched.
	Evaluation *rules.Evaluation[E, A]

	// Dispatched lists every handler that was invoked, in execution
	// order, with timing and error information.
	Dispatched []Dispatched

	// Duration is the total dispatch time (all handlers).
	Duration time.Duration

	// Gated is true if a gate expression was evaluated.
	Gated bool

	// GateExpr is the gate expression string, if Gated is true.
	GateExpr string

	// GatePassed is true if the gate expression passed. If false,
	// no handlers were invoked.
	GatePassed bool
}

Result is the outcome of a dispatch run.

func (*Result[E, A]) Debug

func (r *Result[E, A]) Debug() string

Debug returns a human-readable summary of the dispatch result.

func (*Result[E, A]) Errors

func (r *Result[E, A]) Errors() []error

Errors returns all non-nil errors from dispatched handlers.

func (*Result[E, A]) OK

func (r *Result[E, A]) OK() bool

OK returns true if no handler errors occurred.

type RunOpt

type RunOpt[E any, A any] func(*runConfig[E, A])

RunOpt configures a handler entry within a plan.

func When

func When[E any, A any](expr string) RunOpt[E, A]

When adds a gating expression to a handler within a plan. The expression is evaluated against Evaluation[E, A]. The handler runs if any of its When expressions match. No When means always run.

type Strategy

type Strategy int

Strategy controls how the dispatcher executes matching handlers.

const (
	// AllContinue runs all matching handlers. Errors are collected
	// and returned together. All handlers run regardless of failures.
	AllContinue Strategy = iota

	// AllHaltOnError runs matching handlers in registration order.
	// Stops on the first error.
	AllHaltOnError

	// FirstMatch runs only the first matching handler and stops.
	FirstMatch
)

func (Strategy) String

func (s Strategy) String() string

Jump to

Keyboard shortcuts

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