di

package
v0.13.0-beta1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: MIT Imports: 6 Imported by: 12

Documentation

Overview

Package di is a thin wrapper around dig. See https://github.com/uber-go/dig

This package is not intended for direct usage. Only use it for libraries written for package core.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v0.10.0

func As(constructor any, as any) any

As constructs the instance and bind it to another interface. As means to be used as an argument to graph.Provide. For example:

As(MyConstructor, new(MyAbstractInterface))

func Bind added in v0.9.0

func Bind(from any, to any) any

Bind binds a type to another. Useful when binding implementation to interfaces. The arguments should be a ptr to the binding types, rather than the types themselves. For example:

Bind(new(MyConcreteStruct), new(MyAbstractInterface))

func IntoPopulator added in v0.9.0

func IntoPopulator(container *dig.Container) contract.DIPopulator

IntoPopulator converts a *dig.Container to contract.DIPopulator.

func LocationForPC added in v0.10.0

func LocationForPC(constructor any, pc uintptr) any

LocationForPC sets the constructor pointer to a specified location. Use this Options to reduce vague debug message when constructor are made by reflect.makeFunc. For example:

LocationForPC(reflect.makeFunc(...), reflect.ValueOf(realConstructor).Pointer())

func Name added in v0.10.0

func Name(constructor any, name string) any

Name constructs a named instance. Name means to be used as an argument to graph.Provide. For example:

Name(MyConstructor, "foo")

Types

type Deps added in v0.2.0

type Deps []any

Deps is a set of providers grouped together. This is used by core.Provide method to identify provider sets.

type Factory

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

Factory is a concurrent safe, generic factory for connections to databases and external network services.

func NewFactory

func NewFactory[T any](constructor func(name string) (Pair[T], error)) *Factory[T]

NewFactory creates a new factory. The constructor is a function that is called to create a new connection.

func (*Factory[T]) Close

func (f *Factory[T]) Close()

Close reloads the factory, purging all cached connections.

func (*Factory[T]) CloseConn

func (f *Factory[T]) CloseConn(name string)

CloseConn closes a specific connection in the factory.

func (*Factory[T]) List

func (f *Factory[T]) List() map[string]Pair[T]

List lists created instance in the factory.

func (*Factory[T]) Make

func (f *Factory[T]) Make(name string) (T, error)

Make returns a connection of the given name. If the connection was already created, it is returned from the cache. If not, a new connection will be established by calling the constructor.

type In

type In struct {
	dig.In
}

In is an alias of dig.In

type Modular added in v0.9.0

type Modular interface {
	// Module returns a core module
	Module() any
}

Modular is a special interface. A type implements Modular to indicate it contains a valid core module. This property is examined and executed by the core.Provide in package core. Other than that, the interface bears no further meaning.

If the module is not a by-product of dependency construction via core.Provide, then there is no need to use this mechanism.

type OptionalProvider added in v0.10.0

type OptionalProvider struct {
	Constructor any
	Options     []dig.ProvideOption
}

OptionalProvider is a struct with constructor and dig options. When OptionalProvider is used as the element in di.Deps, the options are applied to the inner dig.Container automatically.

type Out

type Out struct {
	dig.Out
}

Out is an alias of dig.Out

type Pair

type Pair[T any] struct {
	Conn   T
	Closer func()
}

Pair is a tuple representing a connection and a closer function

Jump to

Keyboard shortcuts

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