Documentation
¶
Overview ¶
Package di provides a small declarative layer on top of Fx for building apps.
The package models apps as nodes (Provide, Invoke, Module, etc.) that can be composed, planned, and built into Fx options. It also includes helpers for:
- conditional wiring (If/When/Switch)
- replacements/defaults
- configuration loading (Viper)
- diagnostics and lifecycle hooks
- auto-grouping of providers
See the examples directory for usage patterns.
Index ¶
- Constants
- func App(nodes ...any) *appNode
- func Case(cond bool, nodes ...any) caseNode
- func ConfigAutomaticEnv() configOption
- func ConfigDefault(key string, value any) configOption
- func ConfigEnvKeyReplacer(replacer *strings.Replacer) configOption
- func ConfigEnvOverride(prefix ...string) configOption
- func ConfigEnvPrefix(prefix string) configOption
- func ConfigName(name string) configOption
- func ConfigNoEnv() configOption
- func ConfigOptional() configOption
- func ConfigPath(path string) configOption
- func ConfigType(kind string) configOption
- func ConfigWithViper(fn func(*viper.Viper) error) configOption
- func ConvertAnys(nodes []Node) []any
- func DefaultCase(nodes ...any) switchDefaultNode
- func FindAllMetadata[T any](value any) []T
- func FindMetadata[T any](value any) (T, bool)
- func OrderIndex(value any) (int, bool)
- func Plan(nodes ...any) (string, error)
- func PriorityIndex(value any) (int, bool)
- func ReflectMetadata[T any](value any) (T, bool)
- func ReflectMetadataAny(value any) (any, bool)
- func RegisterMetadata(value any, metadata ...any)
- func Run(nodes ...any) error
- func WhenCase(fn any, nodes ...any) caseNode
- type ConfigWatchOption
- type MetadataValue
- type MockConfigWatchOption
- type MockConfigWatchOption_Expecter
- type MockConfigWatchOption_applyWatch_Call
- func (_c *MockConfigWatchOption_applyWatch_Call) Return() *MockConfigWatchOption_applyWatch_Call
- func (_c *MockConfigWatchOption_applyWatch_Call) Run(run func(configWatchConfigMoqParam *configWatchConfig)) *MockConfigWatchOption_applyWatch_Call
- func (_c *MockConfigWatchOption_applyWatch_Call) RunAndReturn(run func(configWatchConfigMoqParam *configWatchConfig)) *MockConfigWatchOption_applyWatch_Call
- type MockNode
- type MockNodeOption
- type MockNodeOption_Build_Call
- type MockNodeOption_Expecter
- type MockNodeOption_applyBind_Call
- func (_c *MockNodeOption_applyBind_Call) Return() *MockNodeOption_applyBind_Call
- func (_c *MockNodeOption_applyBind_Call) Run(run func(bindConfigMoqParam *bindConfig)) *MockNodeOption_applyBind_Call
- func (_c *MockNodeOption_applyBind_Call) RunAndReturn(run func(bindConfigMoqParam *bindConfig)) *MockNodeOption_applyBind_Call
- type MockNodeOption_applyParam_Call
- func (_c *MockNodeOption_applyParam_Call) Return() *MockNodeOption_applyParam_Call
- func (_c *MockNodeOption_applyParam_Call) Run(run func(paramConfigMoqParam *paramConfig)) *MockNodeOption_applyParam_Call
- func (_c *MockNodeOption_applyParam_Call) RunAndReturn(run func(paramConfigMoqParam *paramConfig)) *MockNodeOption_applyParam_Call
- type MockNode_Build_Call
- type MockNode_Expecter
- type MockOption
- type MockOption_Expecter
- type MockOption_applyBind_Call
- func (_c *MockOption_applyBind_Call) Return() *MockOption_applyBind_Call
- func (_c *MockOption_applyBind_Call) Run(run func(bindConfigMoqParam *bindConfig)) *MockOption_applyBind_Call
- func (_c *MockOption_applyBind_Call) RunAndReturn(run func(bindConfigMoqParam *bindConfig)) *MockOption_applyBind_Call
- type MockOption_applyParam_Call
- func (_c *MockOption_applyParam_Call) Return() *MockOption_applyParam_Call
- func (_c *MockOption_applyParam_Call) Run(run func(paramConfigMoqParam *paramConfig)) *MockOption_applyParam_Call
- func (_c *MockOption_applyParam_Call) RunAndReturn(run func(paramConfigMoqParam *paramConfig)) *MockOption_applyParam_Call
- type Node
- func AutoGroup[T any](group ...string) Node
- func AutoInject() Node
- func Config[T any](pathOrKey string, opts ...any) Node
- func ConfigBind[T any](key string, opts ...any) Node
- func ConfigFile(path string, opts ...any) Node
- func Decorate(function any, opts ...Option) Node
- func Default(value any, opts ...any) Node
- func Diagnostics() Node
- func If(cond bool, nodes ...any) Node
- func Invoke(function any, opts ...Option) Node
- func Module(name string, nodes ...any) Node
- func OnStart(fn any) Node
- func OnStop(fn any) Node
- func Populate(args ...any) Node
- func Provide(constructor any, opts ...any) Node
- func Replace(value any, opts ...any) Node
- func ReplaceAfter(value any, opts ...any) Node
- func ReplaceBefore(value any, opts ...any) Node
- func Supply(value any, opts ...any) Node
- func Switch(items ...any) Node
- func When(fn any, nodes ...any) Node
- type NodeOption
- type Option
- func As[T any](tags ...string) Option
- func AsSelf[T any](tags ...string) Option
- func AutoGroupAsSelf() Option
- func AutoGroupFilter(fn func(reflect.Type) bool) Option
- func AutoGroupIgnore() Option
- func AutoGroupIgnoreType[T any](group ...string) Option
- func AutoInjectIgnore() Option
- func Group(name string) Option
- func InGroup(name string) Option
- func InTag(tag string) Option
- func Metadata(value any) Option
- func MetadataGroup() Option
- func Name(name string) Option
- func Optional() Option
- func Params(items ...any) Option
- func Priority(value PriorityLevel) Option
- func Private() Option
- func Public() Option
- func Self() Option
- func Skip() Option
- func ToGroup(name string) Option
- type PriorityLevel
Constants ¶
const MetadataGroupName = "di:metadata"
MetadataGroupName is the group tag for metadata entries.
Variables ¶
This section is empty.
Functions ¶
func App ¶
func App(nodes ...any) *appNode
App collects declarative nodes and builds them into an fx.Option.
func ConfigAutomaticEnv ¶
func ConfigAutomaticEnv() configOption
ConfigAutomaticEnv enables viper.AutomaticEnv.
func ConfigDefault ¶
ConfigDefault sets a default value.
func ConfigEnvKeyReplacer ¶
ConfigEnvKeyReplacer sets the environment key replacer.
func ConfigEnvOverride ¶
func ConfigEnvOverride(prefix ...string) configOption
ConfigEnvOverride enables env overrides using an optional prefix and a dot-to-underscore replacer.
func ConfigEnvPrefix ¶
func ConfigEnvPrefix(prefix string) configOption
ConfigEnvPrefix sets the environment prefix.
func ConfigName ¶
func ConfigName(name string) configOption
ConfigName sets the config name for Viper to search for.
func ConfigNoEnv ¶
func ConfigNoEnv() configOption
ConfigNoEnv disables env overrides for this config.
func ConfigOptional ¶
func ConfigOptional() configOption
ConfigOptional allows missing config files without failing.
func ConfigPath ¶
func ConfigPath(path string) configOption
ConfigPath adds a search path for ConfigName.
func ConfigType ¶
func ConfigType(kind string) configOption
ConfigType sets the config type (e.g. "toml").
func ConfigWithViper ¶
ConfigWithViper allows custom viper configuration.
func ConvertAnys ¶
func DefaultCase ¶
func DefaultCase(nodes ...any) switchDefaultNode
DefaultCase defines the default branch for Switch.
func FindAllMetadata ¶ added in v1.12.1
FindAllMetadata returns all metadata items assignable to T.
func FindMetadata ¶ added in v1.12.1
FindMetadata returns the first metadata item assignable to T.
func OrderIndex ¶ added in v1.1.1
OrderIndex returns the auto-group order index if present.
func PriorityIndex ¶ added in v1.1.1
PriorityIndex returns the priority order index if present.
func ReflectMetadata ¶
ReflectMetadata returns metadata for a provided value cast to T, if registered.
func ReflectMetadataAny ¶
ReflectMetadataAny returns metadata for a provided value, if registered.
func RegisterMetadata ¶
RegisterMetadata attaches metadata to an instance programmatically. Note: only pointer-like values can be registered (same as ReflectMetadata).
Types ¶
type ConfigWatchOption ¶
type ConfigWatchOption interface {
// contains filtered or unexported methods
}
func ConfigDisableWatch ¶
func ConfigDisableWatch() ConfigWatchOption
ConfigDisableWatch disables watching for this config.
func ConfigWatch ¶
func ConfigWatch(opts ...ConfigWatchOption) ConfigWatchOption
ConfigWatch enables config watching for this config or for the whole app if used at top-level.
func ConfigWatchDebounce ¶
func ConfigWatchDebounce(d time.Duration) ConfigWatchOption
ConfigWatchDebounce sets a debounce duration for config change events.
func ConfigWatchKeys ¶
func ConfigWatchKeys(keys ...string) ConfigWatchOption
ConfigWatchKeys watches only specific keys (e.g. "app", "db.host").
type MetadataValue ¶
MetadataValue pairs a provided value with its metadata tag.
type MockConfigWatchOption ¶ added in v1.2.0
MockConfigWatchOption is an autogenerated mock type for the ConfigWatchOption type
func NewMockConfigWatchOption ¶ added in v1.2.0
func NewMockConfigWatchOption(t interface {
mock.TestingT
Cleanup(func())
}) *MockConfigWatchOption
NewMockConfigWatchOption creates a new instance of MockConfigWatchOption. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.
func (*MockConfigWatchOption) EXPECT ¶ added in v1.2.0
func (_m *MockConfigWatchOption) EXPECT() *MockConfigWatchOption_Expecter
type MockConfigWatchOption_Expecter ¶ added in v1.2.0
type MockConfigWatchOption_Expecter struct {
// contains filtered or unexported fields
}
type MockConfigWatchOption_applyWatch_Call ¶ added in v1.2.0
MockConfigWatchOption_applyWatch_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'applyWatch'
func (*MockConfigWatchOption_applyWatch_Call) Return ¶ added in v1.2.0
func (_c *MockConfigWatchOption_applyWatch_Call) Return() *MockConfigWatchOption_applyWatch_Call
func (*MockConfigWatchOption_applyWatch_Call) Run ¶ added in v1.2.0
func (_c *MockConfigWatchOption_applyWatch_Call) Run(run func(configWatchConfigMoqParam *configWatchConfig)) *MockConfigWatchOption_applyWatch_Call
func (*MockConfigWatchOption_applyWatch_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockConfigWatchOption_applyWatch_Call) RunAndReturn(run func(configWatchConfigMoqParam *configWatchConfig)) *MockConfigWatchOption_applyWatch_Call
type MockNode ¶ added in v1.2.0
MockNode is an autogenerated mock type for the Node type
func NewMockNode ¶ added in v1.2.0
NewMockNode creates a new instance of MockNode. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.
func (*MockNode) EXPECT ¶ added in v1.2.0
func (_m *MockNode) EXPECT() *MockNode_Expecter
type MockNodeOption ¶ added in v1.2.0
MockNodeOption is an autogenerated mock type for the NodeOption type
func NewMockNodeOption ¶ added in v1.2.0
func NewMockNodeOption(t interface {
mock.TestingT
Cleanup(func())
}) *MockNodeOption
NewMockNodeOption creates a new instance of MockNodeOption. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.
func (*MockNodeOption) Build ¶ added in v1.2.0
func (_mock *MockNodeOption) Build() (fx.Option, error)
Build provides a mock function for the type MockNodeOption
func (*MockNodeOption) EXPECT ¶ added in v1.2.0
func (_m *MockNodeOption) EXPECT() *MockNodeOption_Expecter
type MockNodeOption_Build_Call ¶ added in v1.2.0
MockNodeOption_Build_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Build'
func (*MockNodeOption_Build_Call) Return ¶ added in v1.2.0
func (_c *MockNodeOption_Build_Call) Return(option fx.Option, err error) *MockNodeOption_Build_Call
func (*MockNodeOption_Build_Call) Run ¶ added in v1.2.0
func (_c *MockNodeOption_Build_Call) Run(run func()) *MockNodeOption_Build_Call
func (*MockNodeOption_Build_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockNodeOption_Build_Call) RunAndReturn(run func() (fx.Option, error)) *MockNodeOption_Build_Call
type MockNodeOption_Expecter ¶ added in v1.2.0
type MockNodeOption_Expecter struct {
// contains filtered or unexported fields
}
func (*MockNodeOption_Expecter) Build ¶ added in v1.2.0
func (_e *MockNodeOption_Expecter) Build() *MockNodeOption_Build_Call
Build is a helper method to define mock.On call
type MockNodeOption_applyBind_Call ¶ added in v1.2.0
MockNodeOption_applyBind_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'applyBind'
func (*MockNodeOption_applyBind_Call) Return ¶ added in v1.2.0
func (_c *MockNodeOption_applyBind_Call) Return() *MockNodeOption_applyBind_Call
func (*MockNodeOption_applyBind_Call) Run ¶ added in v1.2.0
func (_c *MockNodeOption_applyBind_Call) Run(run func(bindConfigMoqParam *bindConfig)) *MockNodeOption_applyBind_Call
func (*MockNodeOption_applyBind_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockNodeOption_applyBind_Call) RunAndReturn(run func(bindConfigMoqParam *bindConfig)) *MockNodeOption_applyBind_Call
type MockNodeOption_applyParam_Call ¶ added in v1.2.0
MockNodeOption_applyParam_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'applyParam'
func (*MockNodeOption_applyParam_Call) Return ¶ added in v1.2.0
func (_c *MockNodeOption_applyParam_Call) Return() *MockNodeOption_applyParam_Call
func (*MockNodeOption_applyParam_Call) Run ¶ added in v1.2.0
func (_c *MockNodeOption_applyParam_Call) Run(run func(paramConfigMoqParam *paramConfig)) *MockNodeOption_applyParam_Call
func (*MockNodeOption_applyParam_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockNodeOption_applyParam_Call) RunAndReturn(run func(paramConfigMoqParam *paramConfig)) *MockNodeOption_applyParam_Call
type MockNode_Build_Call ¶ added in v1.2.0
MockNode_Build_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Build'
func (*MockNode_Build_Call) Return ¶ added in v1.2.0
func (_c *MockNode_Build_Call) Return(option fx.Option, err error) *MockNode_Build_Call
func (*MockNode_Build_Call) Run ¶ added in v1.2.0
func (_c *MockNode_Build_Call) Run(run func()) *MockNode_Build_Call
func (*MockNode_Build_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockNode_Build_Call) RunAndReturn(run func() (fx.Option, error)) *MockNode_Build_Call
type MockNode_Expecter ¶ added in v1.2.0
type MockNode_Expecter struct {
// contains filtered or unexported fields
}
func (*MockNode_Expecter) Build ¶ added in v1.2.0
func (_e *MockNode_Expecter) Build() *MockNode_Build_Call
Build is a helper method to define mock.On call
type MockOption ¶ added in v1.2.0
MockOption is an autogenerated mock type for the Option type
func NewMockOption ¶ added in v1.2.0
func NewMockOption(t interface {
mock.TestingT
Cleanup(func())
}) *MockOption
NewMockOption creates a new instance of MockOption. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.
func (*MockOption) EXPECT ¶ added in v1.2.0
func (_m *MockOption) EXPECT() *MockOption_Expecter
type MockOption_Expecter ¶ added in v1.2.0
type MockOption_Expecter struct {
// contains filtered or unexported fields
}
type MockOption_applyBind_Call ¶ added in v1.2.0
MockOption_applyBind_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'applyBind'
func (*MockOption_applyBind_Call) Return ¶ added in v1.2.0
func (_c *MockOption_applyBind_Call) Return() *MockOption_applyBind_Call
func (*MockOption_applyBind_Call) Run ¶ added in v1.2.0
func (_c *MockOption_applyBind_Call) Run(run func(bindConfigMoqParam *bindConfig)) *MockOption_applyBind_Call
func (*MockOption_applyBind_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockOption_applyBind_Call) RunAndReturn(run func(bindConfigMoqParam *bindConfig)) *MockOption_applyBind_Call
type MockOption_applyParam_Call ¶ added in v1.2.0
MockOption_applyParam_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'applyParam'
func (*MockOption_applyParam_Call) Return ¶ added in v1.2.0
func (_c *MockOption_applyParam_Call) Return() *MockOption_applyParam_Call
func (*MockOption_applyParam_Call) Run ¶ added in v1.2.0
func (_c *MockOption_applyParam_Call) Run(run func(paramConfigMoqParam *paramConfig)) *MockOption_applyParam_Call
func (*MockOption_applyParam_Call) RunAndReturn ¶ added in v1.2.0
func (_c *MockOption_applyParam_Call) RunAndReturn(run func(paramConfigMoqParam *paramConfig)) *MockOption_applyParam_Call
type Node ¶
Node is a declarative DI node that can be built into fx.Options.
func AutoGroup ¶
AutoGroup registers an interface to be auto-grouped within the scope. Default group name is lowercased interface name unless provided.
func AutoInject ¶
func AutoInject() Node
AutoInject enables automatic injection into tagged struct fields. Supported tags:
di:"inject" di:"name=prod" di:"group=loggers" di:"optional"
Tags can be comma-separated.
func Config ¶
Config loads a configuration file or binds a key from a shared ConfigFile. If the first argument looks like a file path, it is treated as a config file path. Otherwise it is treated as a key and requires ConfigFile or Config* options.
func ConfigBind ¶
ConfigBind provides a sub-config by key from a shared ConfigFile.
func ConfigFile ¶
ConfigFile loads a config file once and exposes it for ConfigBind.
func ReplaceAfter ¶
ReplaceAfter applies only to nodes declared after it in the same scope.
func ReplaceBefore ¶
ReplaceBefore applies only to nodes declared before it in the same scope.
type NodeOption ¶
NodeOption is implemented by types that can act as both Node and Option.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is applied to providers and/or invocations.
func AsSelf ¶
AsSelf creates a new Option that provides the given value as a dependency and tags it with the given tags. Example usage:
di.Provide(NewHandler, di.AsSelf[realtime.Authorizer]())),
Equivalent to:
di.Provide(NewHandler, di.As[realtime.Authorizer](), di.Self()),
func AutoGroupAsSelf ¶
func AutoGroupAsSelf() Option
AutoGroupAsSelf ensures the concrete type is provided alongside auto-grouping.
func AutoGroupFilter ¶
AutoGroupFilter narrows auto grouping to types that pass the predicate.
func AutoGroupIgnore ¶
func AutoGroupIgnore() Option
AutoGroupIgnore disables auto-grouping for this provider.
func AutoGroupIgnoreType ¶
AutoGroupIgnoreType ignores auto-grouping for the given interface. If a group is provided, it targets that group; otherwise it ignores all groups for the interface.
func AutoInjectIgnore ¶
func AutoInjectIgnore() Option
AutoInjectIgnore disables auto field injection for this provider.
func Group ¶
Group applies a group tag. For Provide it sets the output group, for Invoke/Decorate it sets the input tag.
func MetadataGroup ¶
func MetadataGroup() Option
MetadataGroup tags the next parameter to receive metadata entries.
func Name ¶
Name applies a name tag. For Provide it sets the output name, for Invoke/Decorate it sets the input tag.
func Priority ¶ added in v1.1.1
func Priority(value PriorityLevel) Option
Priority overrides the auto-group order for a provided value.
type PriorityLevel ¶ added in v1.1.1
type PriorityLevel int
PriorityLevel controls auto-group ordering for provided values. Lower values are ordered earlier.
const ( Earliest PriorityLevel = -10000 Earlier PriorityLevel = -5000 Normal PriorityLevel = 0 Later PriorityLevel = 5000 Latest PriorityLevel = 10000 )
func Between ¶ added in v1.1.1
func Between(lower, upper PriorityLevel) PriorityLevel
Between returns a PriorityLevel between lower and upper.
Source Files
¶
- app.go
- auto_group.go
- auto_group_order.go
- auto_inject.go
- collect.go
- condition.go
- config_resolver.go
- config_viper.go
- config_watch.go
- decorate.go
- default.go
- diagnostics.go
- doc.go
- errors.go
- expand.go
- fx_helpers.go
- invoke.go
- lifecycle.go
- metadata.go
- mocks.go
- module.go
- options_base.go
- options_bind.go
- options_param.go
- plan.go
- populate.go
- priority.go
- provide.go
- replace.go
- replace_rewrite.go
- switch.go
- types.go
- util.go
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
auto_group
command
|
|
|
basic
command
|
|
|
composit
command
|
|
|
config_default
command
|
|
|
config_env
command
|
|
|
config_json
command
|
|
|
config_toml
command
|
|
|
config_toml_multi
command
|
|
|
config_watch
command
|
|
|
config_watch_switch
command
|
|
|
config_yaml
command
|
|
|
decorate
command
|
|
|
diagnostics
command
|
|
|
diagnostics_multi
command
|
|
|
diagnostics_panic
command
|
|
|
diagnostics_pkg
command
|
|
|
diagnostics_zap
command
|
|
|
if
command
|
|
|
lifecycle
command
|
|
|
module
command
|
|
|
module2
command
|
|
|
params
command
|
|
|
plan
command
|
|
|
populate
command
|
|
|
replace
command
|
|
|
replace2
command
|
|
|
switch
command
|