Skip to content

Commit

Permalink
module: pass route by value (#6404)
Browse files Browse the repository at this point in the history
* use instance

* add some comments

* Update types/router.go

* rename Nil to Empty

* run make mocks

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
  • Loading branch information
3 people authored Jun 11, 2020
1 parent 0215b5c commit 49597b1
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 39 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ type testCustomRouter struct {
routes sync.Map
}

func (rtr *testCustomRouter) AddRoute(route *sdk.Route) sdk.Router {
func (rtr *testCustomRouter) AddRoute(route sdk.Route) sdk.Router {
rtr.routes.Store(route.Path(), route.Handler())
return rtr
}
Expand Down
2 changes: 1 addition & 1 deletion baseapp/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewRouter() *Router {

// AddRoute adds a route path to the router with a given handler. The route must
// be alphanumeric.
func (rtr *Router) AddRoute(route *sdk.Route) sdk.Router {
func (rtr *Router) AddRoute(route sdk.Route) sdk.Router {
if !sdk.IsAlphaNumeric(route.Path()) {
panic("route expressions can only contain alphanumeric characters")
}
Expand Down
14 changes: 7 additions & 7 deletions tests/mocks/tendermint_tm_db_DB.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/mocks/types_module_module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/mocks/types_router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type AppModule interface {
RegisterInvariants(sdk.InvariantRegistry)

// routes
Route() *sdk.Route
Route() sdk.Route
// Deprecated: use RegisterQueryService
QuerierRoute() string
// Deprecated: use RegisterQueryService
Expand Down Expand Up @@ -172,7 +172,7 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule {
func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route empty module message route
func (GenesisOnlyAppModule) Route() *sdk.Route { return nil }
func (GenesisOnlyAppModule) Route() sdk.Route { return sdk.Route{} }

// QuerierRoute returns an empty module querier route
func (GenesisOnlyAppModule) QuerierRoute() string { return "" }
Expand Down Expand Up @@ -251,7 +251,7 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) {
// RegisterRoutes registers all module routes and module querier routes
func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) {
for _, module := range m.Modules {
if module.Route() != nil {
if !module.Route().Empty() {
router.AddRoute(module.Route())
}
if module.QuerierRoute() != "" {
Expand Down
17 changes: 11 additions & 6 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGenesisOnlyAppModule(t *testing.T) {
mockInvariantRegistry := mocks.NewMockInvariantRegistry(mockCtrl)
goam := module.NewGenesisOnlyAppModule(mockModule)

require.Nil(t, goam.Route())
require.True(t, goam.Route().Empty())
require.Empty(t, goam.QuerierRoute())
require.Nil(t, goam.NewQuerierHandler())

Expand Down Expand Up @@ -140,11 +140,16 @@ func TestManager_RegisterRoutes(t *testing.T) {
require.Equal(t, 2, len(mm.Modules))

router := mocks.NewMockRouter(mockCtrl)
handler1, handler2 := sdk.Handler(nil), sdk.Handler(nil)
mockAppModule1.EXPECT().Route().Times(2).Return(sdk.NewRoute("route1", handler1))
mockAppModule2.EXPECT().Route().Times(2).Return(sdk.NewRoute("route2", handler2))
router.EXPECT().AddRoute(gomock.Eq(sdk.NewRoute("route1", handler1))).Times(1)
router.EXPECT().AddRoute(gomock.Eq(sdk.NewRoute("route2", handler2))).Times(1)
handler1, handler2 := sdk.Handler(func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
return nil,nil
}), sdk.Handler(func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
return nil,nil
})
route1 := sdk.NewRoute("route1", handler1)
route2 := sdk.NewRoute("route2", handler2)
mockAppModule1.EXPECT().Route().Times(2).Return(route1)
mockAppModule2.EXPECT().Route().Times(2).Return(route2)
router.EXPECT().AddRoute(gomock.Any()).Times(2) // Use of Any due to limitations to compare Functions as the sdk.Handler

queryRouter := mocks.NewMockQueryRouter(mockCtrl)
mockAppModule1.EXPECT().QuerierRoute().Times(2).Return("querierRoute1")
Expand Down
19 changes: 15 additions & 4 deletions types/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "regexp"
import (
"regexp"
"strings"
)

var (
// IsAlphaNumeric defines a regular expression for matching against alpha-numeric
Expand All @@ -26,7 +29,7 @@ var (

// Router provides handlers for each transaction type.
type Router interface {
AddRoute(r *Route) Router
AddRoute(r Route) Router
Route(ctx Context, path string) Handler
}

Expand All @@ -35,18 +38,26 @@ type Route struct {
handler Handler
}

func NewRoute(p string, h Handler) *Route {
return &Route{path: p, handler: h}
// NewRoute returns an instance of Route.
func NewRoute(p string, h Handler) Route {
return Route{path: p, handler: h}
}

// Path returns the path the route has assigned.
func (r Route) Path() string {
return r.path
}

// Handler returns the handler that handles the route.
func (r Route) Handler() Handler {
return r.handler
}

// Empty returns true only if both handler and path are not empty.
func (r Route) Empty() bool {
return r.handler == nil || r.path == strings.TrimSpace("")
}

// QueryRouter provides queryables for each query path.
type QueryRouter interface {
AddRoute(r string, h Querier) QueryRouter
Expand Down
46 changes: 46 additions & 0 deletions types/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNilRoute(t *testing.T) {
tests := []struct{
name string
route Route
expected bool
} {
{
name: "all empty",
route: NewRoute("", nil),
expected: true,
},
{
name: "only path",
route: NewRoute("some", nil),
expected: true,
},
{
name: "only handler",
route: NewRoute("", func(ctx Context, msg Msg) (*Result, error) {
return nil, nil
}),
expected: true,
},
{
name: "correct route",
route: NewRoute("some", func(ctx Context, msg Msg) (*Result, error) {
return nil, nil
}),
expected: false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.route.Empty())
})
}
}
2 changes: 1 addition & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (AppModule) Name() string {
func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route returns the message routing key for the auth module.
func (AppModule) Route() *sdk.Route { return nil }
func (AppModule) Route() sdk.Route { return sdk.Route{} }

// QuerierRoute returns the auth module's querier route name.
func (AppModule) QuerierRoute() string {
Expand Down
2 changes: 1 addition & 1 deletion x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Route returns the message routing key for the bank module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (am AppModule) Name() string {
}

// Route returns the capability module's message routing key.
func (AppModule) Route() *sdk.Route { return nil }
func (AppModule) Route() sdk.Route { return sdk.Route{} }

// QuerierRoute returns the capability module's query routing key.
func (AppModule) QuerierRoute() string { return "" }
Expand Down
2 changes: 1 addition & 1 deletion x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (AppModule) Name() string {
func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route returns the message routing key for the crisis module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(*am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Route returns the message routing key for the distribution module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (am AppModule) Name() string {
}

// Route returns the evidence module's message routing key.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Route returns the message routing key for the gov module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/ibc-transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Route implements the AppModule interface
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/ibc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Route returns the message routing key for the ibc module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(*am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (AppModule) Name() string {
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route returns the message routing key for the mint module.
func (AppModule) Route() *sdk.Route { return nil }
func (AppModule) Route() sdk.Route { return sdk.Route{} }

// QuerierRoute returns the mint module's querier route name.
func (AppModule) QuerierRoute() string {
Expand Down
2 changes: 1 addition & 1 deletion x/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.Raw
return []abci.ValidatorUpdate{}
}

func (AppModule) Route() *sdk.Route { return nil }
func (AppModule) Route() sdk.Route { return sdk.Route{} }

// GenerateGenesisState performs a no-op.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {}
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (AppModule) Name() string {
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route returns the message routing key for the slashing module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// Route returns the message routing key for the staking module.
func (am AppModule) Route() *sdk.Route {
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

Expand Down
2 changes: 1 addition & 1 deletion x/upgrade/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewAppModule(keeper keeper.Keeper) AppModule {
func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route is empty, as we do not handle Messages (just proposals)
func (AppModule) Route() *sdk.Route { return nil }
func (AppModule) Route() sdk.Route { return sdk.Route{} }

// QuerierRoute returns the route we respond to for abci queries
func (AppModule) QuerierRoute() string { return types.QuerierKey }
Expand Down

0 comments on commit 49597b1

Please sign in to comment.