feat: add core module with app config support (#11914)

This commit is contained in:
Aaron Craelius
2022-05-10 14:41:52 -04:00
committed by GitHub
parent 40b59537eb
commit 90272e3b46
22 changed files with 3295 additions and 24 deletions
+5
View File
@@ -0,0 +1,5 @@
// Package appmodule defines the functionality for registering Cosmos SDK app
// modules that are assembled using the github.com/cosmos/cosmos-sdk/container
// dependency injection system and the declarative app configuration format
// handled by the appconfig package.
package appmodule
+35
View File
@@ -0,0 +1,35 @@
package appmodule
import (
"github.com/cosmos/cosmos-sdk/container"
"cosmossdk.io/core/internal"
)
// Option is a functional option for implementing modules.
type Option interface {
apply(*internal.ModuleInitializer) error
}
type funcOption func(initializer *internal.ModuleInitializer) error
func (f funcOption) apply(initializer *internal.ModuleInitializer) error {
return f(initializer)
}
// Provide registers providers with the dependency injection system that will be
// run within the module scope. See github.com/cosmos/cosmos-sdk/container for
// documentation on the dependency injection system.
func Provide(providers ...interface{}) Option {
return funcOption(func(initializer *internal.ModuleInitializer) error {
for _, provider := range providers {
desc, err := container.ExtractProviderDescriptor(provider)
if err != nil {
return err
}
initializer.Providers = append(initializer.Providers, desc)
}
return nil
})
}
+34
View File
@@ -0,0 +1,34 @@
package appmodule
import (
"reflect"
"google.golang.org/protobuf/proto"
"cosmossdk.io/core/internal"
)
// Register registers a module with the global module registry. The provided
// protobuf message is used only to uniquely identify the protobuf module config
// type. The instance of the protobuf message used in the actual configuration
// will be injected into the container and can be requested by a provider
// function. All module initialization should be handled by the provided options.
//
// Protobuf message types used for module configuration should define the
// cosmos.app.v1alpha.module option and must explicitly specify go_package
// to make debugging easier for users.
func Register(msg proto.Message, options ...Option) {
ty := reflect.TypeOf(msg)
init := &internal.ModuleInitializer{
ConfigProtoMessage: msg,
ConfigGoType: ty,
}
internal.ModuleRegistry[ty] = init
for _, option := range options {
init.Error = option.apply(init)
if init.Error != nil {
return
}
}
}