hook

package
v3.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 2 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextHook added in v3.3.0

type ContextHook[T any] func(ctx context.Context, next T) (T, error)

ContextHook is a hook function that takes a context and returns an error. This is useful for hooks that need to perform I/O operations or other context-aware processing.

func ChainContext added in v3.3.0

func ChainContext[T any](hooks ...ContextHook[T]) ContextHook[T]

ChainContext returns a single hook function that chains the given context hooks together. Nil hooks are automatically filtered out. When the hook is called, each hook in the chain is called with the result of the previous hook as its argument. If any hook returns an error, the chain stops immediately and returns that error. If the chain is empty or all hooks are nil, this function returns nil.

func PrependContext added in v3.3.0

func PrependContext[T any](previous ContextHook[T], chain ...ContextHook[T]) ContextHook[T]

PrependContext returns a hook function that first runs the given previous hook and then runs the remaining hooks in the chain. When executed, previous hook will be the first one to process the value. Nil hooks are automatically filtered out.

type Hook

type Hook[T any] func(next T) T

func Chain

func Chain[T any](hooks ...Hook[T]) Hook[T]

Chain returns a single hook function that chains the given hooks together. Nil hooks are automatically filtered out. When the hook is called, each hook in the chain is called with the result of the previous hook as its argument. The last hook in the chain is called with the argument passed to the chained hook. If the chain is empty or all hooks are nil, this function returns nil.

Example:

hooks := []Hook[int]{
    func(next int) int { return next * 2 },
    func(next int) int { return next + 1 },
}
chainedHook := Chain(hooks...)
result := chainedHook(0)
fmt.Println(result) // Output: 2

func Prepend

func Prepend[T any](previous Hook[T], chain ...Hook[T]) Hook[T]

Prepend returns a hook function that first runs the given previous hook and then runs the remaining hooks in the chain. When executed, previous hook will be the first one to process the value, despite being appended to the end of the slice internally (since hooks are executed in reverse order). Nil hooks are automatically filtered out.

Jump to

Keyboard shortcuts

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