Skip to content

Commit

Permalink
refactor: rename container.Option to container.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed May 23, 2022
1 parent 27869a5 commit b5a2139
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (container) [#12021](https://github.com/cosmos/cosmos-sdk/pull/12021) Rename `container.Option` to `container.Config`
* (x/auth/ante) [#11985](https://github.com/cosmos/cosmos-sdk/pull/11985) The `MempoolFeeDecorator` has been removed. Instead, the `DeductFeeDecorator` takes a new argument of type `TxFeeChecker`, to define custom fee models. If `nil` is passed to this `TxFeeChecker` argument, then it will default to `checkTxFeeWithValidatorMinGasPrices`, which is the exact same behavior as the old `MempoolFeeDecorator` (i.e. checking fees against validator's own min gas price).
* (x/auth/ante) [#11985](https://github.com/cosmos/cosmos-sdk/pull/11985) The `ExtensionOptionsDecorator` takes an argument of type `ExtensionOptionChecker`. For backwards-compatibility, you can pass `nil`, which defaults to the old behavior of rejecting all tx extensions.
* (crypto/keyring) [#11932](https://github.com/cosmos/cosmos-sdk/pull/11932) Remove `Unsafe*` interfaces from keyring package. Please use interface casting if you wish to access those unsafe functions.
Expand Down
18 changes: 9 additions & 9 deletions container/build.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions container/option.go → container/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
38 changes: 19 additions & 19 deletions container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
),
Expand All @@ -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 },
),
Expand All @@ -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 },
),
Expand All @@ -326,15 +326,15 @@ 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",
)

var y map[string]OnePerModuleInt
var z string
require.NoError(t,
container.Build(
container.Options(
container.Configs(
container.ProvideInModule("a",
func() OnePerModuleInt { return 3 },
),
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestOnePerModule(t *testing.T) {

require.NoError(t,
container.Build(
container.Options(),
container.Configs(),
&m,
),
"no providers",
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestManyPerContainer(t *testing.T) {

require.NoError(t,
container.Build(
container.Options(),
container.Configs(),
&xs,
),
"no providers",
Expand All @@ -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 }),
),
Expand All @@ -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 }),
),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestDebugOptions(t *testing.T) {
container.FileVisualizer(graphfile.Name()),
container.StdoutLogger(),
),
container.Options(),
container.Configs(),
))

require.Contains(t, logOut, "digraph")
Expand All @@ -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{})),
Expand All @@ -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`)
Expand All @@ -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)
Expand Down

0 comments on commit b5a2139

Please sign in to comment.