rule_engine

package
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowedVersions

func AllowedVersions() []string

func CompareValidationResult

func CompareValidationResult(result1, result2 ValidationResult) bool

func EmptyActionImpl

func EmptyActionImpl(_ map[string]interface{}) error

func NewEnv

func NewEnv() (*cel.Env, error)

func ValidateApiRule

func ValidateApiRule(apiRule *resources.DiagnosisRule, actionImpls map[string]ActionImpl) ([]*Rule, ValidationResult)

ValidateApiRule validates an API rule and returns a list of rules and validation result.

func ValidateRuleSpecStr

func ValidateRuleSpecStr(apiRuleStr string) ([]*Rule, ValidationResult)

Types

type Action

type Action struct {
	Name      string
	RawKwargs map[string]interface{}
	// contains filtered or unexported fields
}

Action represents an action with a name and parameters.

func NewAction

func NewAction(
	name string,
	rawKwargs map[string]interface{},
	impl ActionImpl,
) (*Action, error)

NewAction creates a new action with compiled CEL expressions.

func (*Action) Run

func (a *Action) Run(activation map[string]interface{}, kwargs map[string]interface{}) error

Run executes the action with the given activation context as well as additional kwargs. Note that it's user's responsibility to ensure that the additional kwargs do not conflict with fixed kwargs.

func (*Action) RunDirect

func (a *Action) RunDirect(activation map[string]interface{}) error

RunDirect executes the action with the given activation context.

func (*Action) String

func (a *Action) String() string

type ActionImpl

type ActionImpl = func(map[string]interface{}) error

ActionImpl is the alias for the action implementation function type.

type Condition

type Condition struct {
	Raw string
	// contains filtered or unexported fields
}

Condition represents a CEL condition.

func NewCondition

func NewCondition(expr string) (*Condition, error)

NewCondition creates and validates a new condition.

func (*Condition) Evaluate

func (c *Condition) Evaluate(activation map[string]interface{}) bool

Evaluate evaluates the condition against the given activation.

type ErrorSection

type ErrorSection int

ErrorSection represents the section where validation error occurred.

const (
	ConditionSection ErrorSection = iota + 1
	ActionSection
)

type Rule

type Rule struct {
	Conditions   []Condition
	Actions      []Action
	Scope        map[string]string
	Topics       mapset.Set[string]
	DebounceTime time.Duration
	Metadata     map[string]interface{}
}

Rule represents a rule with conditions and actions.

func NewRule

func NewRule(
	conditions []Condition,
	actions []Action,
	scope map[string]string,
	topics mapset.Set[string],
	debounceTime time.Duration,
	metadata map[string]interface{},
) *Rule

NewRule creates a new Rule instance.

func (*Rule) EvalConditions

func (r *Rule) EvalConditions(activation map[string]interface{}, prevActivationTime *time.Time, ts time.Time) (isActive bool, activationTime *time.Time)

EvalConditions evaluates all conditions of the rule.

type RuleItem

type RuleItem struct {
	Msg     map[string]interface{} `json:"msg"`
	Topic   string                 `json:"topic"`
	Ts      float64                `json:"ts"` // Timestamp in seconds
	MsgType string                 `json:"msgType"`
	Source  string                 `json:"source"` // filepath or topic of the message
}

RuleItem represents an item to be evaluated by the rule engine.

type ValidationError

type ValidationError struct {
	Location          *ValidationErrorLocation          `json:"location,omitempty"`
	UnexpectedVersion *ValidationErrorUnexpectedVersion `json:"unexpectedVersion,omitempty"`
	SyntaxError       *struct{}                         `json:"syntaxError,omitempty"`
	EmptySection      *struct{}                         `json:"emptySection,omitempty"`
}

ValidationError represents a validation error.

func (ValidationError) Equal

func (e ValidationError) Equal(other ValidationError) bool

Equal Add this helper method to make ValidationError comparable.

type ValidationErrorLocation

type ValidationErrorLocation struct {
	Section   ErrorSection `json:"section"`
	ItemIndex int          `json:"itemIndex,omitempty"`
}

ValidationErrorLocation represents where the validation error occurred.

func (ValidationErrorLocation) Equal

Equal Add this helper method for ValidationErrorLocation.

type ValidationErrorUnexpectedVersion

type ValidationErrorUnexpectedVersion struct {
	AllowedVersions []string `json:"allowedVersions"`
}

ValidationErrorUnexpectedVersion represents version validation error.

type ValidationResult

type ValidationResult struct {
	Success bool              `json:"success"`
	Errors  []ValidationError `json:"errors"`
}

ValidationResult represents the result of validation.

Jump to

Keyboard shortcuts

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