cosmos-sdk/baseapp/router.go
Ethan Buchman fe7ae1151d baseapp: refactor tests
* simplify mock tx type, msgs, and handlers
* remove dependencies on auth and bank
* dedup with setupBaseApp
* lots of comments and cleanup
* fixes where we weren't checking results
* use some table driven tests
* remove TestValidatorChange - its not testing anything since baseapp
doesnt track validator changes
* prepare for CheckTx only running the AnteHandler
* fix runTx gas handling and add more tests
* new tests for multi-msgs
2018-07-07 13:41:36 -04:00

56 lines
1.1 KiB
Go

package baseapp
import (
"regexp"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Router provides handlers for each transaction type.
type Router interface {
AddRoute(r string, h sdk.Handler) (rtr Router)
Route(path string) (h sdk.Handler)
}
// map a transaction type to a handler and an initgenesis function
type route struct {
r string
h sdk.Handler
}
type router struct {
routes []route
}
// nolint
// NewRouter - create new router
// TODO either make Function unexported or make return type (router) Exported
func NewRouter() *router {
return &router{
routes: make([]route, 0),
}
}
var isAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
// AddRoute - TODO add description
func (rtr *router) AddRoute(r string, h sdk.Handler) Router {
if !isAlpha(r) {
panic("route expressions can only contain alphabet characters")
}
rtr.routes = append(rtr.routes, route{r, h})
return rtr
}
// Route - TODO add description
// TODO handle expressive matches.
func (rtr *router) Route(path string) (h sdk.Handler) {
for _, route := range rtr.routes {
if route.r == path {
return route.h
}
}
return nil
}