Documentation
¶
Index ¶
- Variables
- func Append[Dep any](in Injector, loader func(in Injector, dep Dep) error) error
- func Load[Dep any](in Injector) (dep Dep, err error)
- func LoadFrom[Dep any](ctx context.Context) (d Dep, err error)
- func Loader[Dep any](in Injector, fn func(in Injector) (d Dep, err error)) error
- func Middleware(parent Injector) func(next http.Handler) http.Handler
- func Unmarshal(in Injector, ctx any) (err error)
- func WithInjector(parent context.Context, in Injector) context.Context
- type Injector
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoLoader = fmt.Errorf("di: no loader")
var ErrNotInContext = errors.New("di: injector is not in the context")
ErrNotInContext is returned when the injector is not in the context
Functions ¶
func Append ¶ added in v0.0.4
Append a loader to a dependency. These loaders will be called after the dependency is loaded.
func Loader ¶ added in v0.0.4
Example ¶
package main
import (
"fmt"
"github.com/livebud/di"
)
type Env struct {
name string
}
func loadEnv(in di.Injector) (*Env, error) {
return &Env{"production"}, nil
}
type Log struct {
env *Env
lvl string
}
func loadLog(in di.Injector) (*Log, error) {
env, err := di.Load[*Env](in)
if err != nil {
return nil, err
}
return &Log{env: env, lvl: "info"}, nil
}
func main() {
in := di.New()
di.Loader(in, loadEnv)
di.Loader(in, loadLog)
log, err := di.Load[*Log](in)
if err != nil {
panic(err)
}
fmt.Println(log.env.name)
}
Output: production
func Middleware ¶
Middleware creates middleware to inject the injector into the request context
Types ¶
type Injector ¶
type Injector interface {
// contains filtered or unexported methods
}
func FromContext ¶
FromContext returns the injector from a context if present