di

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 7 Imported by: 1 module, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

DI

Go Reference

Dependency injection using Generics.

Features

  • Provide dependencies in a natural and type-safe way
  • Register dependencies with other dependencies (e.g. register a controller with the router)
  • Swap out dependencies during testing
  • Middleware support
  • Unmarshal dependencies into a struct

Install

go get github.com/livebud/di

Example

In the following example, we load the logger which loads the environment:

type Env struct {
  DatabaseURL string
}

func provideEnv(in di.Injector) (*Env, error) {
  return &Env{
    DatabaseURL: "postgres://localhost:5432/db",
  }, nil
}

type Log struct {
  env *Env
}

func provideLog(in di.Injector) (*Log, error) {
  env, err := di.Load[*Env](in)
  if err != nil {
    return nil, err
  }
  return &Log{env}, nil
}

func main() {
  in := di.New()
  di.Provide(in, provideEnv)
  di.Provide(in, provideLog)
  log, err := di.Load[*Log](in)
  fmt.Println(log.env.DatabaseURL)
}

Contributors

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoLoader = fmt.Errorf("di: no loader")
View Source
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

func Append[Dep any](in Injector, loader func(in Injector, dep Dep) error) error

Append a loader to a dependency. These loaders will be called after the dependency is loaded.

func Load

func Load[Dep any](in Injector) (dep Dep, err error)

Load a dependency from an injector and cache the result for future loads

func LoadFrom added in v0.0.4

func LoadFrom[Dep any](ctx context.Context) (d Dep, err error)

FromContext returns the injector from a context if present

func Loader added in v0.0.4

func Loader[Dep any](in Injector, fn func(in Injector) (d Dep, err error)) error
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

func Middleware(parent Injector) func(next http.Handler) http.Handler

Middleware creates middleware to inject the injector into the request context

func Unmarshal

func Unmarshal(in Injector, ctx any) (err error)

func WithInjector added in v0.0.4

func WithInjector(parent context.Context, in Injector) context.Context

WithInjector returns a new context that contains the injector

Types

type Injector

type Injector interface {
	// contains filtered or unexported methods
}

func Clone

func Clone(in Injector) Injector

Clone an injector

func FromContext

func FromContext(ctx context.Context) (Injector, error)

FromContext returns the injector from a context if present

func New

func New() Injector

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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