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
- type ArgType
- type Block
- func (b *Block) Evaluate(ctx context.Context, ruleName string, inputs map[string]any) (bool, error)
- func (b *Block) EvaluateAll(ctx context.Context, inputs map[string]any) (*Result, error)
- func (b *Block) EvaluateAllFrom(ctx context.Context, input any) (*Result, error)
- func (b *Block) EvaluateFrom(ctx context.Context, ruleName string, input any) (bool, error)
- func (b *Block) Init(_ context.Context) error
- func (b *Block) Name() string
- func (b *Block) Shutdown(_ context.Context) error
- type Option
- type Result
- type RuleDefinition
- type Schema
Constants ¶
const ( String = gdeengine.StringType Int = gdeengine.IntType Bool = gdeengine.BoolType Float = gdeengine.FloatType )
Supported ArgType constants.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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"})
type Option ¶
type Option func(*blockConfig)
Option configures a Decision Block.
func WithRule ¶
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) Failed ¶
Failed reports whether the named rule evaluated to false (not an error). Returns true for unknown names.
func (*Result) FailedNames ¶
FailedNames returns the names of all rules that evaluated to false or errored.
func (*Result) Passed ¶
Passed reports whether the named rule evaluated to true. Returns false if the rule is unknown or errored.
func (*Result) PassedNames ¶
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.