di

package module
v1.0.31 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

di

This is a minimalistic idiomatic dependency injection framework for Golang. It is inspired by new opportunities of Go 1.7.

Advantages:

  • Tiny
  • No reflection
  • No code generation
  • No type assertions

Who needs it?

  • People who want to use DI in their projects
  • People who want to write tests for their code

How to use it? See tests for examples.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Logger interface {
	Log(level LogLevel, component, msg string)
} = new(logger)

Functions

func Check added in v1.0.8

func Check(s interface{})

Check checks all fields of structure c for nil. It used reflection for this. If field is nil, then panic.

func Execute added in v1.0.17

func Execute(
	ctx context.Context,
	constructor Constructor[Application],
	options ...AppOption,
)

func Resolve added in v1.0.7

func Resolve(container, environment interface{})

Resolve iterate by fields of structure environment and for each field search in the container constructor of component. If constructor found, then it calls and return value of constructor writes to the field of structure environment.

Types

type App

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

func GetAppFromContext added in v1.0.17

func GetAppFromContext(ctx context.Context) *App

func (*App) Done

func (a *App) Done(ctx context.Context)

func (*App) Init

func (a *App) Init(ctx context.Context)

func (*App) Run added in v1.0.17

func (a *App) Run(ctx context.Context)

func (*App) Variables added in v1.0.22

func (a *App) Variables() Variables

type AppOption

type AppOption func(opts *AppOptions)

func WithConfigurator added in v1.0.30

func WithConfigurator(configurators ...func(ctx context.Context)) AppOption

func WithDaemon added in v1.0.3

func WithDaemon(daemons ...func(ctx context.Context)) AppOption

func WithService added in v1.0.31

func WithService[T any](constructor ...Constructor[T]) AppOption

type AppOptions

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

type Application added in v1.0.17

type Application interface {
	Init(ctx context.Context)
	Done(ctx context.Context)
	Run(ctx context.Context)
}

func Build

func Build(
	ctx context.Context,
	constructor Constructor[Application],
	options ...AppOption,
) (a Application, c context.Context, err error)

type ApplicationContextType added in v1.0.17

type ApplicationContextType int
var ApplicationContextKey ApplicationContextType = 0

type Builder added in v1.0.10

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

type Constructor added in v1.0.17

type Constructor[T any] func(ctx context.Context) T

func NewComponent

func NewComponent[T any](
	name string,
	builder Builder[T],
	options ...Option[T],
) Constructor[T]

type Finalizer added in v1.0.13

type Finalizer interface {
	Done()
}

type Initializer added in v1.0.13

type Initializer interface {
	Init() error
}

type LogLevel added in v1.0.25

type LogLevel int
const (
	LogLevelDebug LogLevel = iota
	LogLevelInfo
	LogLevelWarning
	LogLevelError
)

type Option

type Option[T any] func(options *Options[T])

func UseDone added in v1.0.14

func UseDone[T Finalizer]() Option[T]

func UseInit added in v1.0.14

func UseInit[T Initializer]() Option[T]

func WithDone

func WithDone[T any](finalizer ...func(ctx context.Context, instance T)) Option[T]

func WithInit

func WithInit[T any](initializer ...func(ctx context.Context, instance T) error) Option[T]

func WithPriority added in v1.0.15

func WithPriority[T any](priority int) Option[T]

type Options

type Options[T any] struct {
	// contains filtered or unexported fields
}

type State added in v1.0.3

type State int
const (
	StateBuild State = iota
	StateInit
	StateDone
)

type Variables added in v1.0.22

type Variables interface {
	Get(key string) interface{}
	GetInt(key string) int
	GetString(key string) string
	GetBool(key string) bool
	GetFloat(key string) float64

	TryGet(key string, defVal interface{}) interface{}
	TryGetInt(key string, defVal int) int
	TryGetString(key string, defVal string) string
	TryGetBool(key string, defVal bool) bool
	TryGetFloat(key string, defVal float64) float64

	Set(key string, val interface{})
	SetInt(key string, val int)
	SetString(key string, val string)
	SetBool(key string, val bool)
	SetFloat(key string, val float64)
}

func GetVariables added in v1.0.22

func GetVariables(ctx context.Context) Variables

func NewVariables added in v1.0.22

func NewVariables(values map[string]interface{}) Variables

Jump to

Keyboard shortcuts

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