Documentation
ΒΆ
Index ΒΆ
- Constants
- Variables
- func As[Initial any, Alias any](i Injector) error
- func AsNamed[Initial any, Alias any](i Injector, initial string, alias string) error
- func DescribeInjector(scope Injector) (output string, ok bool)
- func DescribeNamedService[T any](scope Injector, name string) (output string, ok bool)
- func DescribeService[T any](i Injector) (output string, ok bool)
- func ExampleInjector_Clone()
- func ExampleInjector_HealthCheck()
- func ExampleInjector_ListInvokedServices_invoked()
- func ExampleInjector_ListInvokedServices_notInvoked()
- func ExampleInjector_ListProvidedServices()
- func ExampleInjector_Shutdown()
- func ExplainNamedService(scope Injector, name string) (dependencies []EdgeService, dependents []EdgeService, ok bool)
- func ExplainService[T any](scope Injector) (dependencies []EdgeService, dependents []EdgeService, ok bool)
- func HealthCheck[T any](i Injector) error
- func HealthCheckNamed(i Injector, name string) error
- func HealthCheckNamedWithContext(ctx context.Context, i Injector, name string) error
- func HealthCheckWithContext[T any](ctx context.Context, i Injector) error
- func Invoke[T any](i Injector) (T, error)
- func InvokeAs[T any](i Injector) (T, error)
- func InvokeNamed[T any](i Injector, name string) (T, error)
- func MustAs[Initial any, Alias any](i Injector)
- func MustAsNamed[Initial any, Alias any](i Injector, initial string, alias string)
- func MustInvoke[T any](i Injector) T
- func MustInvokeAs[T any](i Injector) T
- func MustInvokeNamed[T any](i Injector, name string) T
- func MustShutdown[T any](i Injector)
- func MustShutdownNamed(i Injector, name string)
- func MustShutdownNamedWithContext(ctx context.Context, i Injector, name string)
- func MustShutdownWithContext[T any](ctx context.Context, i Injector)
- func NameOf[T any]() string
- func Override[T any](i Injector, provider Provider[T])
- func OverrideNamed[T any](i Injector, name string, provider Provider[T])
- func OverrideNamedTransient[T any](i Injector, name string, provider Provider[T])
- func OverrideNamedValue[T any](i Injector, name string, value T)
- func OverrideTransient[T any](i Injector, provider Provider[T])
- func OverrideValue[T any](i Injector, value T)
- func Provide[T any](i Injector, provider Provider[T])
- func ProvideNamed[T any](i Injector, name string, provider Provider[T])
- func ProvideNamedTransient[T any](i Injector, name string, provider Provider[T])
- func ProvideNamedValue[T any](i Injector, name string, value T)
- func ProvideTransient[T any](i Injector, provider Provider[T])
- func ProvideValue[T any](i Injector, value T)
- func Shutdown[T any](i Injector) error
- func ShutdownNamed(i Injector, name string) error
- func ShutdownNamedWithContext(ctx context.Context, i Injector, name string) error
- func ShutdownWithContext[T any](ctx context.Context, i Injector) error
- type DAG
- type EdgeService
- type Healthchecker
- type HealthcheckerWithContext
- type Injector
- type InjectorOpts
- type Provider
- type RootScope
- func (s *RootScope) Ancestors() []*Scope
- func (s *RootScope) ChildByID(id string) (*Scope, bool)
- func (s *RootScope) ChildByName(name string) (*Scope, bool)
- func (s *RootScope) Children() []*Scope
- func (s *RootScope) Clone() *RootScope
- func (s *RootScope) CloneWithOpts(opts *InjectorOpts) *RootScope
- func (s *RootScope) HealthCheck() map[string]error
- func (s *RootScope) HealthCheckWithContext(ctx context.Context) map[string]error
- func (s *RootScope) ID() string
- func (s *RootScope) ListInvokedServices() []EdgeService
- func (s *RootScope) ListProvidedServices() []EdgeService
- func (s *RootScope) Name() string
- func (s *RootScope) RootScope() *RootScope
- func (s *RootScope) Scope(name string) *Scope
- func (s *RootScope) Shutdown() error
- func (s *RootScope) ShutdownOnSIGTERM() (os.Signal, error)
- func (s *RootScope) ShutdownOnSIGTERMOrInterrupt() (os.Signal, error)
- func (s *RootScope) ShutdownOnSIGTERMOrInterruptWithContext(ctx context.Context) (os.Signal, error)
- func (s *RootScope) ShutdownOnSIGTERMWithContext(ctx context.Context) (os.Signal, error)
- func (s *RootScope) ShutdownOnSignals(signals ...os.Signal) (os.Signal, error)
- func (s *RootScope) ShutdownOnSignalsWithContext(ctx context.Context, signals ...os.Signal) (os.Signal, error)
- func (s *RootScope) ShutdownWithContext(ctx context.Context) error
- type Scope
- func (s *Scope) Ancestors() []*Scope
- func (s *Scope) ChildByID(id string) (*Scope, bool)
- func (s *Scope) ChildByName(name string) (*Scope, bool)
- func (s *Scope) Children() []*Scope
- func (s *Scope) HealthCheck() map[string]error
- func (s *Scope) HealthCheckWithContext(ctx context.Context) map[string]error
- func (s *Scope) ID() string
- func (s *Scope) ListInvokedServices() []EdgeService
- func (s *Scope) ListProvidedServices() []EdgeService
- func (s *Scope) Name() string
- func (s *Scope) RootScope() *RootScope
- func (s *Scope) Scope(name string) *Scope
- func (s *Scope) Shutdown() error
- func (s *Scope) ShutdownWithContext(ctx context.Context) error
- type Service
- type ServiceAlias
- type ServiceEager
- type ServiceLazy
- type ServiceTransient
- type ServiceType
- type Shutdowner
- type ShutdownerWithContext
- type ShutdownerWithContextAndError
- type ShutdownerWithError
Examples ΒΆ
Constants ΒΆ
const DefaultRootScopeName = "[root]"
Variables ΒΆ
var ErrCircularDependency = errors.New("DI: circular dependency detected")
var ErrHealthCheckTimeout = errors.New("DI: health check timeout")
var ErrServiceNotFound = errors.New("DI: could not find service")
Functions ΒΆ
func DescribeInjector ΒΆ
DescribeInjector returns a human readable description of the injector, with services and scope tree.
func DescribeNamedService ΒΆ
DescribeNamedService returns a human readable description of the service. It returns false if the service is not found. Please call Invoke[T] before DescribeNamedService[T] to ensure that the service is registered.
func DescribeService ΒΆ
DescribeService returns a human readable description of the service. It returns false if the service is not found. Please call Invoke[T] before DescribeService[T] to ensure that the service is registered.
func ExampleInjector_Clone ΒΆ
func ExampleInjector_Clone()
func ExampleInjector_HealthCheck ΒΆ
func ExampleInjector_HealthCheck()
func ExampleInjector_ListInvokedServices_invoked ΒΆ
func ExampleInjector_ListInvokedServices_invoked()
func ExampleInjector_ListInvokedServices_notInvoked ΒΆ
func ExampleInjector_ListInvokedServices_notInvoked()
func ExampleInjector_ListProvidedServices ΒΆ
func ExampleInjector_ListProvidedServices()
func ExampleInjector_Shutdown ΒΆ
func ExampleInjector_Shutdown()
func ExplainNamedService ΒΆ
func ExplainNamedService(scope Injector, name string) (dependencies []EdgeService, dependents []EdgeService, ok bool)
ExplainNamedService returns a list of dependencies and dependents of a named service.
func ExplainService ΒΆ
func ExplainService[T any](scope Injector) (dependencies []EdgeService, dependents []EdgeService, ok bool)
ExplainService returns a list of dependencies and dependents of a service.
func HealthCheck ΒΆ
HealthCheck returns a service status, using type inference to determine the service name.
func HealthCheckNamed ΒΆ
HealthCheckNamed returns a service status.
func HealthCheckNamedWithContext ΒΆ
HealthCheckNamedWithContext returns a service status.
func HealthCheckWithContext ΒΆ
HealthCheckWithContext returns a service status, using type inference to determine the service name.
func Invoke ΒΆ
Invoke invokes a service in the DI container, using type inference to determine the service name.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
Provide(injector, func(i Injector) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := Invoke[*test](injector)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func InvokeAs ΒΆ
InvokeAs invokes a service in the DI container. The first service matching the provided type or interface will be invoked.
func InvokeNamed ΒΆ
InvokeNamed invokes a named service in the DI container.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideNamed(injector, "my_service", func(i Injector) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := InvokeNamed[*test](injector, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func MustAsNamed ΒΆ
AsNamed declares a named alias for a named service. It panics on error.
func MustInvoke ΒΆ
MustInvoke invokes a service in the DI container, using type inference to determine the service name. It panics on error.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
Provide(injector, func(i Injector) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value := MustInvoke[*test](injector)
fmt.Println(value)
Output: &{foobar}
func MustInvokeAs ΒΆ
MustInvokeAs invokes a service in the DI container. The first service matching the provided type or interface will be invoked. It panics on error.
func MustInvokeNamed ΒΆ
MustInvokeNamed invokes a named service in the DI container. It panics on error.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideNamed(injector, "my_service", func(i Injector) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value := MustInvokeNamed[*test](injector, "my_service")
fmt.Println(value)
Output: &{foobar}
func MustShutdown ΒΆ
MustShutdown stops a service, using type inference to determine the service name. It panics on error.
func MustShutdownNamed ΒΆ
MustShutdownNamed stops a named service. It panics on error.
func MustShutdownNamedWithContext ΒΆ
MustShutdownNamedWithContext stops a named service. It panics on error.
func MustShutdownWithContext ΒΆ
MustShutdownWithContext stops a service, using type inference to determine the service name. It panics on error.
func NameOf ΒΆ
NameOf returns the name of the service in the DI container. This is higly discouraged to use this function, as your code should not declare any dependency explicitly.
func Override ΒΆ
Override replaces the service in the DI container, using type inference to determine the service name.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
Provide(injector, func(i Injector) (*test, error) {
return &test{foobar: "foobar1"}, nil
})
Override(injector, func(i Injector) (*test, error) {
return &test{foobar: "foobar2"}, nil
})
value, err := Invoke[*test](injector)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar2} <nil>
func OverrideNamed ΒΆ
OverrideNamed replaces the named service in the DI container.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideNamed(injector, "my_service", func(i Injector) (*test, error) {
return &test{foobar: "foobar1"}, nil
})
OverrideNamed(injector, "my_service", func(i Injector) (*test, error) {
return &test{foobar: "foobar2"}, nil
})
value, err := InvokeNamed[*test](injector, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar2} <nil>
func OverrideNamedTransient ΒΆ
OverrideNamedTransient replaces the named factory in the DI container.
func OverrideNamedValue ΒΆ
OverrideNamedValue replaces the named value in the DI container.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideNamedValue(injector, "my_service", &test{foobar: "foobar1"})
OverrideNamedValue(injector, "my_service", &test{foobar: "foobar2"})
value, err := InvokeNamed[*test](injector, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar2} <nil>
func OverrideTransient ΒΆ
OverrideTransient replaces the factory in the DI container, using type inference to determine the service name.
func OverrideValue ΒΆ
OverrideValue replaces the value in the DI container, using type inference to determine the service name.
func Provide ΒΆ
Provide registers a service in the DI container, using type inference.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
Provide(injector, func(i Injector) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := Invoke[*test](injector)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ProvideNamed ΒΆ
ProvideNamed registers a named service in the DI container.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideNamed(injector, "my_service", func(i Injector) (*test, error) {
return &test{foobar: "foobar"}, nil
})
value, err := InvokeNamed[*test](injector, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ProvideNamedTransient ΒΆ
ProvideNamedTransient registers a named factory in the DI container.
func ProvideNamedValue ΒΆ
ProvideNamedValue registers a named value in the DI container.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideNamedValue(injector, "my_service", &test{foobar: "foobar"})
value, err := InvokeNamed[*test](injector, "my_service")
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ProvideTransient ΒΆ
ProvideTransient registers a factory in the DI container, using type inference to determine the service name.
func ProvideValue ΒΆ
ProvideValue registers a value in the DI container, using type inference to determine the service name.
Example ΒΆ
injector := New()
type test struct {
foobar string
}
ProvideValue(injector, &test{foobar: "foobar"})
value, err := Invoke[*test](injector)
fmt.Println(value)
fmt.Println(err)
Output: &{foobar} <nil>
func ShutdownNamed ΒΆ
ShutdownNamed stops a named service.
func ShutdownNamedWithContext ΒΆ
ShutdownNamedWithContext stops a named service.
Types ΒΆ
type DAG ΒΆ
type DAG struct {
// contains filtered or unexported fields
}
DAG represents a Directed Acyclic Graph of services, tracking dependencies and dependents.
type EdgeService ΒΆ
EdgeService represents a service in the DAG, identified by scope ID, scope name, and service name.
type Healthchecker ΒΆ
type Healthchecker interface {
HealthCheck() error
}
type Injector ΒΆ
type Injector interface {
// api
ID() string
Name() string
Scope(string) *Scope
RootScope() *RootScope
Ancestors() []*Scope
Children() []*Scope
ChildByID(string) (*Scope, bool)
ChildByName(string) (*Scope, bool)
ListProvidedServices() []EdgeService
ListInvokedServices() []EdgeService
HealthCheck() map[string]error
HealthCheckWithContext(context.Context) map[string]error
Shutdown() error
ShutdownWithContext(context.Context) error
// contains filtered or unexported methods
}
Injector is a DI container.
type InjectorOpts ΒΆ
type InjectorOpts struct {
HookAfterRegistration func(scope *Scope, serviceName string)
HookAfterShutdown func(scope *Scope, serviceName string)
Logf func(format string, args ...any)
HealthCheckParallelism uint // default: all jobs are executed in parallel
HealthCheckGlobalTimeout time.Duration // default: no timeout
HealthCheckTimeout time.Duration // default: no timeout
}
type RootScope ΒΆ
type RootScope struct {
// contains filtered or unexported fields
}
RootScope is the first level of scope tree.
func NewWithOpts ΒΆ
func NewWithOpts(opts *InjectorOpts) *RootScope
NewWithOpts creates a new injector with options.
func (*RootScope) Clone ΒΆ
Clone clones injector with provided services but not with invoked instances.
func (*RootScope) CloneWithOpts ΒΆ
func (s *RootScope) CloneWithOpts(opts *InjectorOpts) *RootScope
CloneWithOpts clones injector with provided services but not with invoked instances, with options.
func (*RootScope) HealthCheck ΒΆ
func (*RootScope) HealthCheckWithContext ΒΆ
func (*RootScope) ListInvokedServices ΒΆ
func (s *RootScope) ListInvokedServices() []EdgeService
func (*RootScope) ListProvidedServices ΒΆ
func (s *RootScope) ListProvidedServices() []EdgeService
func (*RootScope) ShutdownOnSIGTERM ΒΆ
ShutdownOnSIGTERM listens for sigterm signal in order to graceful stop service. It will block until receiving a sigterm signal.
func (*RootScope) ShutdownOnSIGTERMOrInterrupt ΒΆ
ShutdownOnSIGTERMOrInterrupt listens for sigterm or interrupt signal in order to graceful stop service. It will block until receiving a sigterm signal.
func (*RootScope) ShutdownOnSIGTERMOrInterruptWithContext ΒΆ
ShutdownOnSIGTERMOrInterruptWithContext listens for sigterm or interrupt signal in order to graceful stop service. It will block until receiving a sigterm signal.
func (*RootScope) ShutdownOnSIGTERMWithContext ΒΆ
ShutdownOnSIGTERMWithContext listens for sigterm signal in order to graceful stop service. It will block until receiving a sigterm signal.
func (*RootScope) ShutdownOnSignals ΒΆ
ShutdownOnSignals listens for signals defined in signals parameter in order to graceful stop service. It will block until receiving any of these signal. If no signal is provided in signals parameter, syscall.SIGTERM will be added as default signal.
func (*RootScope) ShutdownOnSignalsWithContext ΒΆ
func (s *RootScope) ShutdownOnSignalsWithContext(ctx context.Context, signals ...os.Signal) (os.Signal, error)
ShutdownOnSignalsWithContext listens for signals defined in signals parameter in order to graceful stop service. It will block until receiving any of these signal. If no signal is provided in signals parameter, syscall.SIGTERM will be added as default signal.
type Scope ΒΆ
type Scope struct {
// contains filtered or unexported fields
}
Scope is a DI container. It must be created with injector.Scope("name") method.
func (*Scope) ChildByName ΒΆ
ChildByName returns the child scope recursively by its name.
func (*Scope) HealthCheck ΒΆ
HealthCheck returns the healthcheck results of the scope, in a map of service name to error.
func (*Scope) HealthCheckWithContext ΒΆ
HealthCheckWithContext returns the healthcheck results of the scope, in a map of service name to error.
func (*Scope) ListInvokedServices ΒΆ
func (s *Scope) ListInvokedServices() []EdgeService
ListInvokedServices returns the list of services invoked by the scope.
func (*Scope) ListProvidedServices ΒΆ
func (s *Scope) ListProvidedServices() []EdgeService
ListProvidedServices returns the list of services provided by the scope.
type ServiceAlias ΒΆ
type ServiceEager ΒΆ
type ServiceEager[T any] struct { // contains filtered or unexported fields }
type ServiceLazy ΒΆ
type ServiceLazy[T any] struct { // contains filtered or unexported fields }
type ServiceTransient ΒΆ
type ServiceTransient[T any] struct { // contains filtered or unexported fields }
type ServiceType ΒΆ
type ServiceType string
const ( ServiceTypeLazy ServiceType = "lazy" ServiceTypeEager ServiceType = "eager" ServiceTypeTransient ServiceType = "transient" ServiceTypeAlias ServiceType = "alias" )
type Shutdowner ΒΆ
type Shutdowner interface {
Shutdown()
}
type ShutdownerWithContext ΒΆ
type ShutdownerWithError ΒΆ
type ShutdownerWithError interface {
Shutdown() error
}
Source Files
ΒΆ
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
dag
command
|
|
|
healthcheckable
command
|
|
|
interface
command
|
|
|
nested-scope
command
|
|
|
shutdownable
command
|
|
|
simple
command
|
|