cosmos-sdk/container/option.go
Aaron Craelius 02e3919539
refactor(container)!: move logging and visualization options to DebugOption (#10302)
## Description



<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### 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...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] 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)
2021-10-14 16:49:50 +00:00

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 constructors. Each constructor will be called at most once with the
// exception of scoped constructors which are called at most once per scope
// (see Scope).
func Provide(constructors ...interface{}) Option {
return containerOption(func(ctr *container) error {
return provide(ctr, nil, constructors)
})
}
// ProvideWithScope creates a container option which registers the provided dependency
// injection constructors that are to be run in the provided scope. Each constructor
// will be called at most once.
func ProvideWithScope(scopeName string, constructors ...interface{}) Option {
return containerOption(func(ctr *container) error {
if scopeName == "" {
return errors.Errorf("expected non-empty scope name")
}
return provide(ctr, ctr.createOrGetScope(scopeName), constructors)
})
}
func provide(ctr *container, scope Scope, constructors []interface{}) error {
for _, c := range constructors {
rc, err := ExtractProviderDescriptor(c)
if err != nil {
return errors.WithStack(err)
}
_, err = ctr.addNode(&rc, scope)
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)