## Description Closes: #11944 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package depinject
|
|
|
|
// Inject builds the container specified by containerConfig and extracts the
|
|
// requested outputs from the container or returns an error. It is the single
|
|
// entry point for building and running a dependency injection container.
|
|
// Each of the values specified as outputs must be pointers to types that
|
|
// can be provided by the container.
|
|
//
|
|
// Ex:
|
|
// var x int
|
|
// Inject(Provide(func() int { return 1 }), &x)
|
|
//
|
|
// Inject uses the debug mode provided by AutoDebug which means there will be
|
|
// verbose debugging information if there is an error and nothing upon success.
|
|
// Use InjectDebug to configure debug behavior.
|
|
func Inject(containerConfig Config, outputs ...interface{}) error {
|
|
loc := LocationFromCaller(1)
|
|
return inject(loc, AutoDebug(), containerConfig, outputs...)
|
|
}
|
|
|
|
// InjectDebug is a version of Inject which takes an optional DebugOption for
|
|
// logging and visualization.
|
|
func InjectDebug(debugOpt DebugOption, config Config, outputs ...interface{}) error {
|
|
loc := LocationFromCaller(1)
|
|
return inject(loc, debugOpt, config, outputs...)
|
|
}
|
|
|
|
func inject(loc Location, debugOpt DebugOption, config Config, outputs ...interface{}) error {
|
|
cfg, err := newDebugConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// debug cleanup
|
|
defer func() {
|
|
for _, f := range cfg.cleanup {
|
|
f()
|
|
}
|
|
}()
|
|
|
|
err = doInject(cfg, loc, debugOpt, config, outputs...)
|
|
if err != nil {
|
|
cfg.logf("Error: %v", err)
|
|
if cfg.onError != nil {
|
|
err2 := cfg.onError.applyConfig(cfg)
|
|
if err2 != nil {
|
|
return err2
|
|
}
|
|
}
|
|
return err
|
|
} else {
|
|
if cfg.onSuccess != nil {
|
|
err2 := cfg.onSuccess.applyConfig(cfg)
|
|
if err2 != nil {
|
|
return err2
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func doInject(cfg *debugConfig, loc Location, debugOpt DebugOption, config Config, outputs ...interface{}) error {
|
|
defer cfg.generateGraph() // always generate graph on exit
|
|
|
|
if debugOpt != nil {
|
|
err := debugOpt.applyConfig(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
cfg.logf("Registering providers")
|
|
cfg.indentLogger()
|
|
ctr := newContainer(cfg)
|
|
err := config.apply(ctr)
|
|
if err != nil {
|
|
cfg.logf("Failed registering providers because of: %+v", err)
|
|
return err
|
|
}
|
|
cfg.dedentLogger()
|
|
|
|
return ctr.build(loc, outputs...)
|
|
}
|