hook

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: Apache-2.0 Imports: 4 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddInitiator

func AddInitiator(kind Kind)

AddInitiator registers a new Kind as an initiator kind. Initiator kinds are used to identify the source or trigger type of an operation (e.g. UI, System).

Types

type Event

type Event[T any] = Registry[EventFunc[T], T]

Event defines a registry of event hooks for type T. Event hooks are used to observe or react to lifecycle events without modifying data.

type EventFunc

type EventFunc[T any] func(c context.Context, kinds Kinds, value T) error

EventFunc represents a non-mutating hook function for a value of type T. It can be used for logging, auditing, notifications, or other side effects.

type Filter

type Filter[T any] func(c context.Context, kinds Kinds, value T) bool

Filter defines a predicate used to determine whether a hook should apply based on the current context, hook kinds, and target value.

func AndFilters

func AndFilters[T any](list ...Filter[T]) Filter[T]

AndFilters combines multiple filters using logical AND. Returns true only if all filters return true.

func ExcludeKind

func ExcludeKind[T any](kind Kind) Filter[T]

ExcludeKind returns a filter that matches only if the given kind is NOT present.

func MatchAllKinds

func MatchAllKinds[T any](list ...Kind) Filter[T]

MatchAllKinds returns a filter that matches only if all specified kinds are present.

func MatchAny

func MatchAny[T any]() Filter[T]

MatchAny returns a filter that always evaluates to true.

func MatchAnyKinds

func MatchAnyKinds[T any](list ...Kind) Filter[T]

MatchAnyKinds returns a filter that matches if at least one of the specified kinds is present.

func MatchKind

func MatchKind[T any](kind Kind) Filter[T]

MatchKind returns a filter that matches only if the given kind is present.

func MatchOnlyKinds

func MatchOnlyKinds[T any](list ...Kind) Filter[T]

MatchOnlyKinds returns a filter that matches only if the kinds exactly match the provided list.

func OrFilters

func OrFilters[T any](list ...Filter[T]) Filter[T]

OrFilters combines multiple filters using logical OR. Returns true if at least one filter returns true.

type Kind

type Kind string

Kind represents a type or phase of a hookable event.

const (
	KindBefore Kind = "Before"
	KindAfter  Kind = "After"

	KindFind   Kind = "Find"
	KindCreate Kind = "Create"
	KindUpdate Kind = "Update"
	KindDelete Kind = "Delete"

	KindLock Kind = "Lock"
	KindOne  Kind = "One"
	KindMany Kind = "Many"
	KindByID Kind = "ByID"

	KindUI     Kind = "UI"
	KindSystem Kind = "System"
	KindSilent Kind = "Silent"
)

Base hook kinds commonly used across modules.

func (Kind) String

func (a Kind) String() string

String returns the Kind as a string.

type Kinds

type Kinds map[Kind]bool

Kinds is a set of Kind values used to define which events a hook applies to.

func NewKinds

func NewKinds(list ...Kind) Kinds

NewKinds creates a Kinds set from a list of Kind values.

func (Kinds) Has

func (a Kinds) Has(item Kind) bool

Has checks whether the given kind is present in the set.

func (Kinds) HasAll

func (a Kinds) HasAll(list ...Kind) bool

HasAll checks if all provided kinds are present in the set.

func (Kinds) HasAny

func (a Kinds) HasAny(list ...Kind) bool

HasAny checks if at least one of the provided kinds is present in the set.

func (Kinds) HasOnly

func (a Kinds) HasOnly(list ...Kind) bool

HasOnly checks if the set contains only the specified kinds (no more, no less).

func (Kinds) Not

func (a Kinds) Not(item Kind) bool

Not returns true if the given kind is not present in the set.

func (Kinds) Pick

func (a Kinds) Pick(list ...Kind) Kinds

Pick returns a new Kinds set containing only the kinds from the input list that are present in the original set.

func (Kinds) PickInitiatorKinds

func (a Kinds) PickInitiatorKinds() Kinds

PickInitiatorKinds returns a new Kinds set containing only the kinds from the current set that match known initiator kinds (e.g., UI, System, Silent).

func (Kinds) String

func (a Kinds) String() string

String returns a comma-separated list of kind names in the set.

func (Kinds) With

func (a Kinds) With(list ...Kind) Kinds

With returns a new Kinds set containing the current kinds plus the provided ones.

type Mutator

type Mutator[T any] = Registry[MutatorFunc[T], T]

Mutator defines a registry of mutation hooks for type T. Mutators can transform a value before or after a specific action.

type MutatorFunc

type MutatorFunc[T any] func(c context.Context, kinds Kinds, value T) (T, error)

MutatorFunc represents a hook function that can modify a value of type T before it proceeds through the system. It receives context and hook kinds.

type Provider

type Provider[H any, V any] interface {
	// Find returns a sequence of handlers matching the given context, kinds, and value.
	Find(c context.Context, kinds Kinds, value V) iter.Seq[H]
}

Provider defines an interface for retrieving applicable hook handlers based on the current context, kinds, and value.

type Registry

type Registry[H any, V any] interface {
	// Add registers a new hook handler with an associated filter.
	Add(filter Filter[V], hook H) Registry[H, V]
}

Registry defines a hook registry that allows adding hook handlers with filters. The registry may contain multiple handlers that match different kinds or values.

Directories

Path Synopsis
impl module

Jump to

Keyboard shortcuts

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