Documentation
¶
Overview ¶
Package dim is a slim, generics-based dependency-injection toolkit: lazy providers and managed resources built from plain functions, with structured logging of initialization and cleanup.
A Provider[T] is just a function that returns a value, evaluated lazily and (via Once / OnceWithName / NewResource) memoized so the resource is built exactly once on first use. NewResource additionally pairs the provider with a CleanupFunc, giving each resource an automatically registered teardown that the application can run in reverse (LIFO) order at shutdown. It mirrors the lavka-promoaction pkg/dim design so applications can adopt the same wiring style.
Usage ¶
Declare each dependency as a managed resource; keep the returned cleanup to register with a shutdown coordinator (see the closer package):
store, cleanup := dim.NewResource("Store",
func(ctx context.Context) (Store, dim.CleanupFunc, error) {
st, err := postgres.New(ctx, dsn)
if err != nil {
return nil, nil, err
}
return st, func() error { return st.Close() }, nil
})
closer.Add(cleanup)
db := store(ctx) // built (and logged) on first call, cached thereafter
For a value with no cleanup, wrap a factory in a Provider directly. A factory error is fatal and panics, so initialization failures surface at startup:
cfg := dim.OnceWithName("Config", func(ctx context.Context) (Config, error) {
return loadConfig(ctx)
})
API ¶
- Provider[T]: lazy resource accessor func(ctx) T.
- FactoryFunc[T]: func(ctx) (T, CleanupFunc, error) used by NewResource.
- CleanupFunc: a teardown func() error, run LIFO at shutdown.
- Once / OnceWithName: memoize a factory into a Provider (panic on error).
- NewResource: a memoized Provider plus an auto-registered CleanupFunc.
- NamedCleanup: wrap a teardown with named, timed logging.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewResource ¶
func NewResource[T any](name string, factory func(context.Context) (T, CleanupFunc, error)) (Provider[T], CleanupFunc)
NewResource creates a new managed resource and returns Provider and CleanupFunc. factory must return: (resource, cleanup_function, error).
Usage:
c.Store, cleanup := dim.NewResource("Store", func(ctx context.Context) (store.Store, dim.CleanupFunc, error) {
st := postgres.New(c.Logger)
if err := st.Connect(config); err != nil {
return nil, nil, err
}
return st, func() error { return st.Close() }, nil
})
return cleanup, nil
Note: Go cannot infer T from the named type FactoryFunc[T], so the signature takes an anonymous function literal.
Types ¶
type CleanupFunc ¶
type CleanupFunc func() error
CleanupFunc - function to close/clean up a resource. Functions are added in initialization order and are called in reverse order (LIFO).
func NamedCleanup ¶
func NamedCleanup(name string, fn func() error) CleanupFunc
NamedCleanup creates a function with closure on name that logs the resource name and execution time.
type FactoryFunc ¶
type FactoryFunc[T any] func(ctx context.Context) (T, CleanupFunc, error)
FactoryFunc is a function that creates a resource and returns it along with an optional cleanup function.