container

package
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: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ContextKey is the key used to store the container in the context.
	ContextKey = &struct{ name string }{name: "di.container.ContextKey"}
)

Functions

func Invoke

func Invoke[T any](ctx context.Context) T

Invoke Get a value from the map for a key, or panic if none exists. if need all type of T, please use Invoke[Set[T]](ctx)

func SimpleContext

func SimpleContext(ctx context.Context, objects ...any) context.Context

SimpleContext create a context with a simple container objects can be any type, but must be unique. and the type of objects will be used as the key of the container. usage: ctx := SimpleContext(context.Background(), &MyService{})

ctx := SimpleContext(context.Background(), &MyService{}, &MyOtherService{})

ctx := SimpleContext(context.Background(), &MyService{}, &MyOtherService{}, Set[MyInterface]{&MyService{}, &MyOtherService{}})

Types

type Interface

type Interface interface {
	// Invoke Get a value from the map for a key, or panic if the key does not exist.
	Invoke(ctx context.Context, typ reflect.Type) any
}

Interface Container interface

type Set

type Set[T any] []T

Set is used to return a set of instances for a particular type. This is used to inform the Container that it should return a set of instances (as opposed to a single instance) for the specified type. usage: Invoke[Set[MyInterface]](ctx) Note that this feature depends on the container implementation. If the container allows to register multiple objects of the same type, then Invoke[Set[MyInterface]](ctx) will return all objects of the same type.

type Simple

type Simple map[reflect.Type]any

Simple A simple container for mocking testing scenes. Simple implements the Container interface, but not implement the Set[T] feature. if need Invoke[Set[T]](ctx), please Put Set[T] into Simple.

func (Simple) Invoke

func (s Simple) Invoke(ctx context.Context, typ reflect.Type) any

Invoke Get an object or panic if it doesn't exist

func (Simple) Put

func (s Simple) Put(obj any)

Put an object into the container, the type of the object will be used as the key.

Jump to

Keyboard shortcuts

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