di

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

DI 容器

使用说明

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

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

Build builds a specified object. Build cannot build a named object.

func Provide

func Provide[T any](b Builder[T], opts ...Option)

Provide is used to provide a type T to the container. The provided type T must be a struct or a pointer to a struct, or a interface

func RegisterFlagBinder

func RegisterFlagBinder(typ reflect.Type, bind FlagBind)

Types

type BuildFunc

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

func Func

func Func[T any](fn func(ctx context.Context) (T, error)) BuildFunc[T]

func (BuildFunc[T]) Build

func (bf BuildFunc[T]) Build(c context.Context) (T, error)

type Builder

type Builder[T any] interface {
	Build(ctx context.Context) (T, error)
}

Builder defines the object builder

type Context

type Context interface {
	Path() string
	// contains filtered or unexported methods
}

Context defines the context for building objects, as well as getting dependencies and getting the context itself.

type FlagBind

type FlagBind func(fs *flag.FlagSet, value reflect.Value, name, def, usage string) error

type InjectBuilder

type InjectBuilder[T any, K any] struct {
	Opt K `flag:""`
	// contains filtered or unexported fields
}

func Inject

func Inject[T any, K any](fn func(ctx context.Context, option K) (T, error)) *InjectBuilder[T, K]

func (*InjectBuilder[T, K]) Build

func (ib *InjectBuilder[T, K]) Build(c context.Context) (T, error)

type Option

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

func WithFlagset

func WithFlagset(fs *flag.FlagSet) Option

func WithImplement

func WithImplement[I any, T any]() Option

WithImplement use to specify the implementation of interface the first type is interface, the second type is implementation

func WithName

func WithName(name string) Option

func WithOptional added in v1.1.0

func WithOptional[T any](fn func(name string, err error)) Option

func WithOverride

func WithOverride() Option

func WithSelect

func WithSelect[T any](name string) Option

type Registry

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

Registry holds all the registered constructors

func GetRegistry

func GetRegistry() Registry

func NewRegistry

func NewRegistry() Registry

NewRegistry create and initialize object container

func (Registry) Provide

func (r Registry) Provide(typ reflect.Type, flaggerBuilder any, buildFunc func(context.Context) (any, error), opts ...Option)

func (Registry) ValidateFlags

func (r Registry) ValidateFlags() error

func (Registry) Visit

func (r Registry) Visit(fn func(v Value))

VisitAll Iterate all the built constructors

type Value

type Value struct {
	Name string
	// contains filtered or unexported fields
}

func (Value) Builder

func (v Value) Builder() any

func (Value) Instance

func (v Value) Instance() any

Jump to

Keyboard shortcuts

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