rule

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package rule provides a YAML-driven rule engine that compiles rules, indexes fields for fast lookup, evaluates conditions against data, and applies transformations when conditions match.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompiledRule

type CompiledRule struct {
	Name            string
	Conditions      []*Condition
	FieldSet        map[string]bool // Fields this rule requires
	Transformations []*Transformation
}

CompiledRule represents a pre-compiled rule ready for fast evaluation

type Condition

type Condition struct {
	Field    string `yaml:"field"`
	Operator string `yaml:"operator"`
	Value    any    `yaml:"value"`
}

Condition represents a single condition in a rule

type RuleDefinition

type RuleDefinition struct {
	Rule            string            `yaml:"rule"`
	Conditions      []*Condition      `yaml:"conditions"`
	Transformations []*Transformation `yaml:"transformations,omitempty"`
}

RuleDefinition represents the YAML structure of a rule

type RuleEngine

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

RuleEngine implements port.RuleEnginePort with pre-compilation and field indexing

func NewRuleEngine

func NewRuleEngine(cache port.CachePort, transformer port.TransformerPort) *RuleEngine

NewRuleEngine creates a new rule engine with cache and transformer support

func (*RuleEngine) Compile

func (re *RuleEngine) Compile(ctx context.Context, rules []string) error

Compile takes rule definitions (YAML strings) and pre-compiles them

func (*RuleEngine) Evaluate

func (re *RuleEngine) Evaluate(ctx context.Context, data map[string]any) (map[string]any, []string, error)

Evaluate takes input data, applies matching rules and transformations, returns modified data and rule names

func (*RuleEngine) GetStats

func (re *RuleEngine) GetStats() *port.RuleEngineStats

GetStats returns engine statistics

func (*RuleEngine) SetCacheTTL

func (re *RuleEngine) SetCacheTTL(ttl time.Duration)

SetCacheTTL sets the cache TTL for rule evaluation results.

type Transformation

type Transformation struct {
	Field  string         `yaml:"field"`
	Name   string         `yaml:"transformer"`
	Params map[string]any `yaml:"params,omitempty"`
}

Transformation represents a transformation to apply when rule matches

Jump to

Keyboard shortcuts

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