Address Rigel review - also run gofmt

This commit is contained in:
Christopher Goes 2018-03-27 21:46:06 +02:00
parent 5b642062a7
commit 578392d4b2
6 changed files with 23 additions and 38 deletions

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)

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)

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

View File

@ -32,7 +32,7 @@ const (
CodeInsufficientCoins CodeType = 10
CodeInvalidCoins CodeType = 11
CodeGenesisParse CodeType = 0xdead // TODO: remove ?
CodeGenesisParse CodeType = 0xdead // TODO: remove ? // why remove?
)
// NOTE: Don't stringer this, we'll put better messages in later.

View File

@ -4,7 +4,7 @@ import (
"encoding/json"
)
/* Run only once on chain initialization, should write genesis state to store
or throw an error if some required information was not provided, in which case
the application will panic. */
// Run only once on chain initialization, should write genesis state to store
// or throw an error if some required information was not provided, in which case
// the application will panic.
type InitGenesis func(ctx Context, data json.RawMessage) error

View File

@ -16,7 +16,7 @@ import (
type commander struct {
storeName string
cdc *wire.Codec
decoder sdk.AccountDecoder
decoder sdk.AccountDecoder
}
func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) func(http.ResponseWriter, *http.Request) {