Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed May 29, 2021
1 parent d30a7e1 commit ce46d86
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
23 changes: 22 additions & 1 deletion core/app_config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Compose(config AppConfig, moduleRegistry *module.Registry) (types.Applicati
modSet := &moduleSet{
container: container,
modMap: map[string]app.Handler{},
configMap: map[string]*ModuleConfig{},
}

for _, mod := range config.Modules {
Expand Down Expand Up @@ -63,14 +64,23 @@ func Compose(config AppConfig, moduleRegistry *module.Registry) (types.Applicati
type moduleSet struct {
container *module.Container
modMap map[string]app.Handler
configMap map[string]*ModuleConfig
}

func (ms *moduleSet) addModule(interfaceRegistry codectypes.InterfaceRegistry, registry *module.Registry, config *ModuleConfig) error {
ms.configMap[config.Name] = config

msg, err := interfaceRegistry.Resolve(config.Module.TypeUrl)
if err != nil {
return err
}

// TODO:
//typeProvider, ok := msg.(codec.TypeProvider)
//if !ok {
// typeProvider.RegisterTypes(interfaceRegistry)
//}

err = proto.Unmarshal(config.Module.Value, msg)
if err != nil {
return err
Expand Down Expand Up @@ -142,7 +152,18 @@ func (ms *moduleSet) addModule(interfaceRegistry codectypes.InterfaceRegistry, r
}

func (ms *moduleSet) initialize() error {
return ms.container.InitializeAll()
err := ms.container.InitializeAll()
if err != nil {
return err
}

for name := range ms.configMap {
if ms.modMap[name] == nil {
return fmt.Errorf("module %s failed to initialize", name)
}
}

return nil
}

func isErrorTyp(ty reflect.Type) bool {
Expand Down
2 changes: 1 addition & 1 deletion core/module/app/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

import "github.com/cosmos/cosmos-sdk/core/module"

var defaultRegistry = module.NewRegistry((*Handler)(nil))
var defaultRegistry = module.NewRegistry((interface{})(nil), (*Handler)(nil))

func DefaultRegistry() *module.Registry {
return defaultRegistry
Expand Down
13 changes: 9 additions & 4 deletions core/module/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (

type Registry struct {
hmap map[reflect.Type]interface{}
configType reflect.Type
handlerType reflect.Type
}

func NewRegistry(handlerType interface{}) *Registry {
func NewRegistry(configType interface{}, handlerType interface{}) *Registry {
return &Registry{
hmap: map[reflect.Type]interface{}{},
configType: reflect.TypeOf(configType),
handlerType: reflect.TypeOf(handlerType),
}
}
Expand All @@ -26,9 +28,12 @@ func (r *Registry) Register(constructor interface{}) {
panic("TODO")
}

configArg := typ.In(0)
configType := typ.In(0)
if !configType.AssignableTo(r.configType) {
panic("TODO")
}

if _, ok := r.hmap[configArg]; ok {
if _, ok := r.hmap[configType]; ok {
panic("TODO")
}

Expand All @@ -41,7 +46,7 @@ func (r *Registry) Register(constructor interface{}) {
panic("TODO")
}

r.hmap[configArg] = constructor
r.hmap[configType] = constructor
}

func (r *Registry) Resolve(configType interface{}) interface{} {
Expand Down

0 comments on commit ce46d86

Please sign in to comment.