Address Rigel review - also run gofmt

This commit is contained in:
Christopher Goes
2018-03-28 11:24:31 +02:00
parent 5b642062a7
commit 578392d4b2
6 changed files with 23 additions and 38 deletions
+1 -12
View File
@@ -241,18 +241,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
// TODO Return something intelligent
panic(err)
}
err = app.Router().ForEach(func(r string, _ sdk.Handler, i sdk.InitGenesis) error {
if i != nil {
encoded, exists := (*genesisState)[r]
if !exists {
// TODO should this be a Cosmos SDK standard error?
return errors.New(fmt.Sprintf("Expected module genesis information for module %s but it was not present", r))
} else {
return i(app.deliverState.ctx, encoded)
}
}
return nil
})
err = app.Router().InitGenesis(app.deliverState.ctx, *genesisState)
if err != nil {
// TODO Return something intelligent
panic(err)
+1 -1
View File
@@ -180,7 +180,7 @@ func TestInitChainer(t *testing.T) {
// set initChainer and try again - should see the value
app.SetInitChainer(initChainer)
app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")})
app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")}) // must have valid JSON genesis file, even if empty
app.Commit()
res = app.Query(query)
assert.Equal(t, value, res.Value)
+16 -20
View File
@@ -1,6 +1,8 @@
package baseapp
import (
"encoding/json"
"fmt"
"regexp"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -10,8 +12,7 @@ import (
type Router interface {
AddRoute(r string, h sdk.Handler, i sdk.InitGenesis) (rtr Router)
Route(path string) (h sdk.Handler)
RouteGenesis(path string) (i sdk.InitGenesis)
ForEach(func(r string, h sdk.Handler, i sdk.InitGenesis) error) error
InitGenesis(ctx sdk.Context, data map[string]json.RawMessage) error
}
// map a transaction type to a handler and an initgenesis function
@@ -46,34 +47,29 @@ func (rtr *router) AddRoute(r string, h sdk.Handler, i sdk.InitGenesis) Router {
return rtr
}
// TODO handle expressive matches.
func matchRoute(path string, route string) bool {
return path == route
}
// Route - TODO add description
// TODO handle expressive matches.
func (rtr *router) Route(path string) (h sdk.Handler) {
for _, route := range rtr.routes {
if matchRoute(path, route.r) {
if route.r == path {
return route.h
}
}
return nil
}
func (rtr *router) RouteGenesis(path string) (i sdk.InitGenesis) {
// InitGenesis - call `InitGenesis`, where specified, for all routes
// Return the first error if any, otherwise nil
func (rtr *router) InitGenesis(ctx sdk.Context, data map[string]json.RawMessage) error {
for _, route := range rtr.routes {
if matchRoute(path, route.r) {
return route.i
}
}
return nil
}
func (rtr *router) ForEach(f func(string, sdk.Handler, sdk.InitGenesis) error) error {
for _, route := range rtr.routes {
if err := f(route.r, route.h, route.i); err != nil {
return err
if route.i != nil {
encoded, found := data[route.r]
if !found {
return sdk.ErrGenesisParse(fmt.Sprintf("Expected module genesis information for module %s but it was not present", route.r))
}
if err := route.i(ctx, encoded); err != nil {
return err
}
}
}
return nil