predicates

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package predicates provides a pluggable condition registry: typed predicates that evaluate a Source (caller-supplied context) and compose with And/Or/Not. Predicates can be constructed from data (Spec → tree) for config-driven feature flagging, A/B segmentation, policy rules, ability gating, loot drop conditions, alert rules.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownType = errors.New("predicates: unknown predicate type")

ErrUnknownType is returned by Build when a Spec.Type is not registered.

Functions

func RegisterTyped

func RegisterTyped[T any](r *Registry, name string, fn func(params T) (Predicate, error))

RegisterTyped binds a factory whose params decode into T. The wrapper handles json.Unmarshal so the user-supplied function takes T directly.

Types

type Predicate

type Predicate interface {
	Eval(ctx context.Context, src Source) bool
}

Predicate evaluates a Source and returns true if satisfied.

func And

func And(p ...Predicate) Predicate

And returns a predicate that is true only when every child is true. Empty And is true.

func Build

func Build(r *Registry, spec Spec) (Predicate, error)

Build materialises a Spec into a Predicate using the registry. "and", "or", "not" are reserved composite types and never look at the registry.

func Not

func Not(p Predicate) Predicate

Not inverts a predicate.

func Or

func Or(p ...Predicate) Predicate

Or returns a predicate that is true when any child is true. Empty Or is false.

type PredicateFunc

type PredicateFunc func(ctx context.Context, src Source) bool

PredicateFunc adapts a function to the Predicate interface.

func (PredicateFunc) Eval

func (f PredicateFunc) Eval(ctx context.Context, src Source) bool

type Registry

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

Registry holds factory functions keyed by predicate-type name. Factories receive the raw JSON params bytes so they can decode into their typed param struct.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Register

func (r *Registry) Register(name string, fn func(params json.RawMessage) (Predicate, error))

Register binds a factory to a type name. Factories that expect typed params can use the generic helper RegisterTyped instead.

type Source

type Source interface{}

Source is the marker interface for a predicate's evaluation context. Consumers define their own concrete types.

type Spec

type Spec struct {
	Type   string          `json:"type"`
	Params json.RawMessage `json:"params,omitempty"`
	Sub    []Spec          `json:"sub,omitempty"`
}

Spec is a serialisable predicate description: a leaf has Type + Params; composite nodes have Type "and" / "or" / "not" and Sub children.

Jump to

Keyboard shortcuts

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