refactor: rename container.Option to container.Config (#12021)
## Description Ref: https://github.com/cosmos/cosmos-sdk/issues/11917 The noun should be one which conveys the notion of a *core instruction*. Other names for this type I've considered: - Instruction - Behavior - Specification - [Config](https://github.com/cosmos/cosmos-sdk/issues/11917#issuecomment-1135086189) (probably the most straighforward) - Trait --- ### 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/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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)
This commit is contained in:
parent
27869a5af3
commit
dafdc1070b
|
@ -1,6 +1,6 @@
|
|||
package container
|
||||
|
||||
// Build builds the container specified by containerOption and extracts the
|
||||
// Build 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
|
||||
|
@ -13,19 +13,19 @@ package container
|
|||
// Build 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 BuildDebug to configure debug behavior.
|
||||
func Build(containerOption Option, outputs ...interface{}) error {
|
||||
func Build(containerConfig Config, outputs ...interface{}) error {
|
||||
loc := LocationFromCaller(1)
|
||||
return build(loc, AutoDebug(), containerOption, outputs...)
|
||||
return build(loc, AutoDebug(), containerConfig, outputs...)
|
||||
}
|
||||
|
||||
// BuildDebug is a version of Build which takes an optional DebugOption for
|
||||
// logging and visualization.
|
||||
func BuildDebug(debugOpt DebugOption, option Option, outputs ...interface{}) error {
|
||||
func BuildDebug(debugOpt DebugOption, config Config, outputs ...interface{}) error {
|
||||
loc := LocationFromCaller(1)
|
||||
return build(loc, debugOpt, option, outputs...)
|
||||
return build(loc, debugOpt, config, outputs...)
|
||||
}
|
||||
|
||||
func build(loc Location, debugOpt DebugOption, option Option, outputs ...interface{}) error {
|
||||
func build(loc Location, debugOpt DebugOption, config Config, outputs ...interface{}) error {
|
||||
cfg, err := newDebugConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -38,7 +38,7 @@ func build(loc Location, debugOpt DebugOption, option Option, outputs ...interfa
|
|||
}
|
||||
}()
|
||||
|
||||
err = doBuild(cfg, loc, debugOpt, option, outputs...)
|
||||
err = doBuild(cfg, loc, debugOpt, config, outputs...)
|
||||
if err != nil {
|
||||
cfg.logf("Error: %v", err)
|
||||
if cfg.onError != nil {
|
||||
|
@ -59,7 +59,7 @@ func build(loc Location, debugOpt DebugOption, option Option, outputs ...interfa
|
|||
}
|
||||
}
|
||||
|
||||
func doBuild(cfg *debugConfig, loc Location, debugOpt DebugOption, option Option, outputs ...interface{}) error {
|
||||
func doBuild(cfg *debugConfig, loc Location, debugOpt DebugOption, config Config, outputs ...interface{}) error {
|
||||
defer cfg.generateGraph() // always generate graph on exit
|
||||
|
||||
if debugOpt != nil {
|
||||
|
@ -72,7 +72,7 @@ func doBuild(cfg *debugConfig, loc Location, debugOpt DebugOption, option Option
|
|||
cfg.logf("Registering providers")
|
||||
cfg.indentLogger()
|
||||
ctr := newContainer(cfg)
|
||||
err := option.apply(ctr)
|
||||
err := config.apply(ctr)
|
||||
if err != nil {
|
||||
cfg.logf("Failed registering providers because of: %+v", err)
|
||||
return err
|
||||
|
|
|
@ -6,26 +6,26 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Option is a functional option for a container.
|
||||
type Option interface {
|
||||
// Config is a functional configuration of a container.
|
||||
type Config interface {
|
||||
apply(*container) error
|
||||
}
|
||||
|
||||
// Provide creates a container option which registers the provided dependency
|
||||
// Provide defines a container configuration 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 {
|
||||
func Provide(providers ...interface{}) Config {
|
||||
return containerConfig(func(ctr *container) error {
|
||||
return provide(ctr, nil, providers)
|
||||
})
|
||||
}
|
||||
|
||||
// ProvideInModule creates a container option which registers the provided dependency
|
||||
// ProvideInModule defines container configuration 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 {
|
||||
func ProvideInModule(moduleName string, providers ...interface{}) Config {
|
||||
return containerConfig(func(ctr *container) error {
|
||||
if moduleName == "" {
|
||||
return errors.Errorf("expected non-empty module name")
|
||||
}
|
||||
|
@ -48,9 +48,9 @@ func provide(ctr *container, key *moduleKey, providers []interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func Supply(values ...interface{}) Option {
|
||||
func Supply(values ...interface{}) Config {
|
||||
loc := LocationFromCaller(1)
|
||||
return containerOption(func(ctr *container) error {
|
||||
return containerConfig(func(ctr *container) error {
|
||||
for _, v := range values {
|
||||
err := ctr.supply(reflect.ValueOf(v), loc)
|
||||
if err != nil {
|
||||
|
@ -61,17 +61,17 @@ func Supply(values ...interface{}) Option {
|
|||
})
|
||||
}
|
||||
|
||||
// Error creates an option which causes the dependency injection container to
|
||||
// Error defines configuration which causes the dependency injection container to
|
||||
// fail immediately.
|
||||
func Error(err error) Option {
|
||||
return containerOption(func(*container) error {
|
||||
func Error(err error) Config {
|
||||
return containerConfig(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 {
|
||||
// Configs defines a configuration which bundles together multiple Config definitions.
|
||||
func Configs(opts ...Config) Config {
|
||||
return containerConfig(func(ctr *container) error {
|
||||
for _, opt := range opts {
|
||||
err := opt.apply(ctr)
|
||||
if err != nil {
|
||||
|
@ -82,10 +82,10 @@ func Options(opts ...Option) Option {
|
|||
})
|
||||
}
|
||||
|
||||
type containerOption func(*container) error
|
||||
type containerConfig func(*container) error
|
||||
|
||||
func (c containerOption) apply(ctr *container) error {
|
||||
func (c containerConfig) apply(ctr *container) error {
|
||||
return c(ctr)
|
||||
}
|
||||
|
||||
var _ Option = (*containerOption)(nil)
|
||||
var _ Config = (*containerConfig)(nil)
|
|
@ -82,7 +82,7 @@ func (ModuleB) Provide(dependencies BDependencies) (BProvides, Handler, error) {
|
|||
}, Handler{}, nil
|
||||
}
|
||||
|
||||
var scenarioConfig = container.Options(
|
||||
var scenarioConfig = container.Configs(
|
||||
container.Provide(ProvideMsgClientA),
|
||||
container.ProvideInModule("runtime", ProvideKVStoreKey),
|
||||
container.ProvideInModule("a", wrapMethod0(ModuleA{})),
|
||||
|
@ -170,7 +170,7 @@ func TestBadCtr(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTrivial(t *testing.T) {
|
||||
require.NoError(t, container.Build(container.Options()))
|
||||
require.NoError(t, container.Build(container.Configs()))
|
||||
}
|
||||
|
||||
func TestErrorFunc(t *testing.T) {
|
||||
|
@ -230,7 +230,7 @@ func TestModuleScoped(t *testing.T) {
|
|||
var y float64
|
||||
require.Error(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Provide(
|
||||
func(container.ModuleKey) int { return 0 },
|
||||
func() int { return 1 },
|
||||
|
@ -245,7 +245,7 @@ func TestModuleScoped(t *testing.T) {
|
|||
|
||||
require.Error(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Provide(
|
||||
func() int { return 0 },
|
||||
func(container.ModuleKey) int { return 1 },
|
||||
|
@ -260,7 +260,7 @@ func TestModuleScoped(t *testing.T) {
|
|||
|
||||
require.Error(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Provide(
|
||||
func(container.ModuleKey) int { return 0 },
|
||||
func(container.ModuleKey) int { return 1 },
|
||||
|
@ -275,7 +275,7 @@ func TestModuleScoped(t *testing.T) {
|
|||
|
||||
require.NoError(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Provide(
|
||||
func(container.ModuleKey) int { return 0 },
|
||||
),
|
||||
|
@ -289,7 +289,7 @@ func TestModuleScoped(t *testing.T) {
|
|||
|
||||
require.Error(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Provide(
|
||||
func(container.ModuleKey) int { return 0 },
|
||||
),
|
||||
|
@ -304,7 +304,7 @@ func TestModuleScoped(t *testing.T) {
|
|||
var z float32
|
||||
require.NoError(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Provide(
|
||||
func(container.ModuleKey) int { return 0 },
|
||||
),
|
||||
|
@ -326,7 +326,7 @@ func (OnePerModuleInt) IsOnePerModuleType() {}
|
|||
func TestOnePerModule(t *testing.T) {
|
||||
var x OnePerModuleInt
|
||||
require.Error(t,
|
||||
container.Build(container.Options(), &x),
|
||||
container.Build(container.Configs(), &x),
|
||||
"bad input type",
|
||||
)
|
||||
|
||||
|
@ -334,7 +334,7 @@ func TestOnePerModule(t *testing.T) {
|
|||
var z string
|
||||
require.NoError(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.ProvideInModule("a",
|
||||
func() OnePerModuleInt { return 3 },
|
||||
),
|
||||
|
@ -394,7 +394,7 @@ func TestOnePerModule(t *testing.T) {
|
|||
|
||||
require.NoError(t,
|
||||
container.Build(
|
||||
container.Options(),
|
||||
container.Configs(),
|
||||
&m,
|
||||
),
|
||||
"no providers",
|
||||
|
@ -443,7 +443,7 @@ func TestManyPerContainer(t *testing.T) {
|
|||
|
||||
require.NoError(t,
|
||||
container.Build(
|
||||
container.Options(),
|
||||
container.Configs(),
|
||||
&xs,
|
||||
),
|
||||
"no providers",
|
||||
|
@ -462,7 +462,7 @@ func TestSupply(t *testing.T) {
|
|||
|
||||
require.Error(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Supply(3),
|
||||
container.Provide(func() int { return 4 }),
|
||||
),
|
||||
|
@ -473,7 +473,7 @@ func TestSupply(t *testing.T) {
|
|||
|
||||
require.Error(t,
|
||||
container.Build(
|
||||
container.Options(
|
||||
container.Configs(
|
||||
container.Supply(3),
|
||||
container.Provide(func() int { return 4 }),
|
||||
),
|
||||
|
@ -507,7 +507,7 @@ type TestOutput struct {
|
|||
|
||||
func TestStructArgs(t *testing.T) {
|
||||
var input TestInput
|
||||
require.Error(t, container.Build(container.Options(), &input))
|
||||
require.Error(t, container.Build(container.Configs(), &input))
|
||||
|
||||
require.NoError(t, container.Build(
|
||||
container.Supply(1.3),
|
||||
|
@ -569,7 +569,7 @@ func TestDebugOptions(t *testing.T) {
|
|||
container.FileVisualizer(graphfile.Name()),
|
||||
container.StdoutLogger(),
|
||||
),
|
||||
container.Options(),
|
||||
container.Configs(),
|
||||
))
|
||||
|
||||
require.Contains(t, logOut, "digraph")
|
||||
|
@ -594,7 +594,7 @@ func TestGraphAndLogOutput(t *testing.T) {
|
|||
require.NoError(t, container.BuildDebug(debugOpts, scenarioConfig, &b))
|
||||
golden.Assert(t, graphOut, "example.dot")
|
||||
|
||||
badConfig := container.Options(
|
||||
badConfig := container.Configs(
|
||||
container.ProvideInModule("runtime", ProvideKVStoreKey),
|
||||
container.ProvideInModule("a", wrapMethod0(ModuleA{})),
|
||||
container.ProvideInModule("b", wrapMethod0(ModuleB{})),
|
||||
|
@ -617,7 +617,7 @@ func TestConditionalDebugging(t *testing.T) {
|
|||
var input TestInput
|
||||
require.Error(t, container.BuildDebug(
|
||||
conditionalDebugOpt,
|
||||
container.Options(),
|
||||
container.Configs(),
|
||||
&input,
|
||||
))
|
||||
require.Contains(t, logs, `Initializing logger`)
|
||||
|
@ -629,7 +629,7 @@ func TestConditionalDebugging(t *testing.T) {
|
|||
success = false
|
||||
require.NoError(t, container.BuildDebug(
|
||||
conditionalDebugOpt,
|
||||
container.Options(),
|
||||
container.Configs(),
|
||||
))
|
||||
require.Empty(t, logs)
|
||||
require.True(t, success)
|
||||
|
|
Loading…
Reference in New Issue