decision

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package decision provides a CEL-based rule evaluation block for go-code-blocks.

It integrates github.com/raywall/go-decision-engine to let you define typed business rules using Google's Common Expression Language (CEL), then evaluate them against runtime data to drive routing decisions across other blocks.

Typical usage

router := decision.New("customer-router",
    decision.WithRule("is-pj", `customer_type == "PJ"`, decision.Schema{
        "customer_type": decision.String,
    }),
    decision.WithRule("is-pf", `customer_type == "PF"`, decision.Schema{
        "customer_type": decision.String,
    }),
)

// Register alongside other blocks
app.MustRegister(router)
app.InitAll(ctx)

// Evaluate at request time
result, err := router.EvaluateAll(ctx, map[string]any{
    "customer_type": "PJ",
})

switch {
case result.Passed("is-pj"):
    data, _ := dynamoBlock.GetItem(ctx, id, nil)
case result.Passed("is-pf"):
    return ErrPersonFisicaNotSupported
}

Index

Constants

Supported ArgType constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgType

type ArgType = gdeengine.ArgType

ArgType is the CEL-compatible type for a rule input variable.

type Block

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

Block is a CEL-based decision block. It holds a set of named rules compiled during Init, then evaluated on demand at request time.

func New

func New(name string, opts ...Option) *Block

New creates a new Decision Block.

router := decision.New("customer-router",
    decision.WithRule("is-pj", `customer_type == "PJ"`, decision.Schema{
        "customer_type": decision.String,
    }),
)

func (*Block) Evaluate

func (b *Block) Evaluate(ctx context.Context, ruleName string, inputs map[string]any) (bool, error)

Evaluate runs a single named rule against the provided flat map. Returns the bool result of the CEL expression.

Use this when you need to check a specific rule without running the full set.

ok, err := router.Evaluate(ctx, "is-pj", map[string]any{"customer_type": "PJ"})

func (*Block) EvaluateAll

func (b *Block) EvaluateAll(ctx context.Context, inputs map[string]any) (*Result, error)

EvaluateAll runs all registered rules concurrently against the provided flat map and returns a Result that can be queried by rule name.

result, err := router.EvaluateAll(ctx, map[string]any{"customer_type": "PJ"})
if result.Passed("is-pj") { ... }

Note: each rule is evaluated against the full inputs map; only the fields declared in its Schema are validated and used by the CEL expression. Fields unknown to a rule are silently ignored by that rule.

func (*Block) EvaluateAllFrom

func (b *Block) EvaluateAllFrom(ctx context.Context, input any) (*Result, error)

EvaluateAllFrom runs every rule concurrently against a struct annotated with `decision:` tags, using the engine's native fan-out implementation.

type Customer struct {
    Type    string  `decision:"customer_type"`
    Revenue float64 `decision:"revenue"`
}
result, err := router.EvaluateAllFrom(ctx, Customer{Type: "PJ", Revenue: 5000})
if result.Passed("high-value-pj") { ... }

func (*Block) EvaluateFrom

func (b *Block) EvaluateFrom(ctx context.Context, ruleName string, input any) (bool, error)

EvaluateFrom runs a single named rule against a struct annotated with `decision:` struct tags. Only tagged fields are extracted and validated.

type Customer struct {
    Type string `decision:"customer_type"`
    ID   string `decision:"-"`
}
ok, err := router.EvaluateFrom(ctx, "is-pj", Customer{Type: "PJ"})

func (*Block) Init

func (b *Block) Init(_ context.Context) error

Init implements core.Block. It creates the shared rule cache and compiles every registered rule. Compilation errors are surfaced immediately so misconfigured expressions are caught at startup, not at request time.

func (*Block) Name

func (b *Block) Name() string

Name implements core.Block.

func (*Block) Shutdown

func (b *Block) Shutdown(_ context.Context) error

Shutdown implements core.Block.

type Option

type Option func(*blockConfig)

Option configures a Decision Block.

func WithRule

func WithRule(name, expression string, schema Schema) Option

WithRule adds a named CEL rule to the block.

name — unique identifier used to query the result (e.g. "is-pj") expression — CEL expression that must evaluate to bool (e.g. `customer_type == "PJ"`) schema — maps each variable referenced in the expression to its ArgType

Rules are compiled once during Init and cached for the lifetime of the block. Multiple calls to WithRule register multiple rules; they are all evaluated concurrently when EvaluateAll is called.

decision.WithRule("is-pj", `customer_type == "PJ"`, decision.Schema{
    "customer_type": decision.String,
})

type Result

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

Result is returned by Evaluate and EvaluateAll. It provides a clean API for inspecting which rules passed or failed.

func (*Result) All

func (r *Result) All() bool

All reports whether every rule passed.

func (*Result) Any

func (r *Result) Any() bool

Any reports whether at least one rule passed.

func (*Result) Err

func (r *Result) Err(name string) error

Err returns the evaluation error for the named rule, or nil.

func (*Result) Failed

func (r *Result) Failed(name string) bool

Failed reports whether the named rule evaluated to false (not an error). Returns true for unknown names.

func (*Result) FailedNames

func (r *Result) FailedNames() []string

FailedNames returns the names of all rules that evaluated to false or errored.

func (*Result) None

func (r *Result) None() bool

None reports whether no rule passed.

func (*Result) Passed

func (r *Result) Passed(name string) bool

Passed reports whether the named rule evaluated to true. Returns false if the rule is unknown or errored.

func (*Result) PassedNames

func (r *Result) PassedNames() []string

PassedNames returns the names of all rules that evaluated to true.

type RuleDefinition

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

RuleDefinition holds the static declaration of a rule before compilation.

type Schema

type Schema = map[string]ArgType

Schema maps variable names to their CEL type. It is the argument type descriptor for a DecisionRule.

Jump to

Keyboard shortcuts

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