## Description This renames `container.Scope` to `container.ModuleKey` to make it more consistent with the usage of scope for modules. It also renames all usages of `constructor` (in docs and variable name) to `provider` for consistency. --- ### 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/master/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/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package container
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Option is a functional option for a container.
|
|
type Option interface {
|
|
apply(*container) error
|
|
}
|
|
|
|
// Provide creates a container option which registers the provided dependency
|
|
// injection providers. Each provider will be called at most once with the
|
|
// exception of module-scoped providers which are called at most once per module
|
|
// (see ModuleKey).
|
|
func Provide(providers ...interface{}) Option {
|
|
return containerOption(func(ctr *container) error {
|
|
return provide(ctr, nil, providers)
|
|
})
|
|
}
|
|
|
|
// ProvideInModule creates a container option which registers the provided dependency
|
|
// injection providers that are to be run in the named module. Each provider
|
|
// will be called at most once.
|
|
func ProvideInModule(moduleName string, providers ...interface{}) Option {
|
|
return containerOption(func(ctr *container) error {
|
|
if moduleName == "" {
|
|
return errors.Errorf("expected non-empty module name")
|
|
}
|
|
|
|
return provide(ctr, ctr.createOrGetModuleKey(moduleName), providers)
|
|
})
|
|
}
|
|
|
|
func provide(ctr *container, key *moduleKey, providers []interface{}) error {
|
|
for _, c := range providers {
|
|
rc, err := ExtractProviderDescriptor(c)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
_, err = ctr.addNode(&rc, key)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Supply(values ...interface{}) Option {
|
|
loc := LocationFromCaller(1)
|
|
return containerOption(func(ctr *container) error {
|
|
for _, v := range values {
|
|
err := ctr.supply(reflect.ValueOf(v), loc)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Error creates an option which causes the dependency injection container to
|
|
// fail immediately.
|
|
func Error(err error) Option {
|
|
return containerOption(func(*container) error {
|
|
return errors.WithStack(err)
|
|
})
|
|
}
|
|
|
|
// Options creates an option which bundles together other options.
|
|
func Options(opts ...Option) Option {
|
|
return containerOption(func(ctr *container) error {
|
|
for _, opt := range opts {
|
|
err := opt.apply(ctr)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
type containerOption func(*container) error
|
|
|
|
func (c containerOption) apply(ctr *container) error {
|
|
return c(ctr)
|
|
}
|
|
|
|
var _ Option = (*containerOption)(nil)
|