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 Plan(nodes ...any) (string, error)
- 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 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 Private() Option
- func Public() Option
- func Self() Option
- func ToGroup(name string) Option
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 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 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.
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
- module.go
- options_base.go
- options_bind.go
- options_param.go
- plan.go
- populate.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
|