hooks

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Discovery

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

Discovery finds lifecycle methods on structs via reflection.

Example:

d := hooks.NewDiscovery()
methods := d.Discover(myService, "OnStart")

func NewDiscovery

func NewDiscovery() *Discovery

NewDiscovery creates a hook discovery instance with caching.

func (*Discovery) Call

func (d *Discovery) Call(v any, name string, args ...any) ([]any, error)

Call invokes a method by name with optional arguments.

func (*Discovery) CallWithContext

func (d *Discovery) CallWithContext(ctx context.Context, v any, name string, args ...any) ([]any, error)

CallWithContext invokes a method with context as first argument.

func (*Discovery) Discover

func (d *Discovery) Discover(v any, prefix string) []MethodInfo

Discover finds all methods on v with the given prefix.

Returns:

A list of discovered matching methods.

func (*Discovery) DiscoverAll

func (d *Discovery) DiscoverAll(v any, prefixes ...string) map[string][]MethodInfo

DiscoverAll finds methods matching any of the given prefixes.

func (*Discovery) HasMethod

func (d *Discovery) HasMethod(v any, name string) bool

HasMethod checks if a specific method exists.

type HookFunc

type HookFunc func(ctx context.Context, key string, args []any) error

HookFunc is a function called at a lifecycle event.

type MethodInfo

type MethodInfo struct {
	Name   string
	Suffix string
	Method reflect.Method
	Value  reflect.Value
}

MethodInfo holds information about a discovered method.

type Runner

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

Runner manages execution of lifecycle hooks with before/after patterns.

func NewRunner

func NewRunner() *Runner

NewRunner creates a hook runner with a shared discovery instance.

func (*Runner) After

func (r *Runner) After(key string, fn HookFunc)

After registers a function to be called after a specific event.

func (*Runner) AfterAll

func (r *Runner) AfterAll(fn HookFunc)

AfterAll registers a function to be called after any event.

func (*Runner) Before

func (r *Runner) Before(key string, fn HookFunc)

Before registers a function to be called before a specific event.

func (*Runner) BeforeAll

func (r *Runner) BeforeAll(fn HookFunc)

BeforeAll registers a function to be called before any event.

func (*Runner) Clear

func (r *Runner) Clear()

Clear removes all registered hooks.

func (*Runner) Discovery

func (r *Runner) Discovery() *Discovery

Discovery returns the underlying discovery instance.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, key string, action func() error, args ...any) error

Run executes before hooks, the action, and after hooks.

Jump to

Keyboard shortcuts

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