2018-07-31 18:48:32 -07:00
|
|
|
package baseapp
|
|
|
|
|
|
|
|
import (
|
2019-02-05 19:11:57 -08:00
|
|
|
"fmt"
|
|
|
|
|
2018-07-31 18:48:32 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2019-10-14 15:05:10 -07:00
|
|
|
type QueryRouter struct {
|
2018-08-16 18:35:17 -07:00
|
|
|
routes map[string]sdk.Querier
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var _ sdk.QueryRouter = NewQueryRouter()
|
|
|
|
|
2019-10-14 15:05:10 -07:00
|
|
|
// NewQueryRouter returns a reference to a new QueryRouter.
|
|
|
|
func NewQueryRouter() *QueryRouter {
|
|
|
|
return &QueryRouter{
|
2018-08-16 18:35:17 -07:00
|
|
|
routes: map[string]sdk.Querier{},
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// AddRoute adds a query path to the router with a given Querier. It will panic
|
|
|
|
// if a duplicate route is given. The route must be alphanumeric.
|
2019-10-14 15:05:10 -07:00
|
|
|
func (qrt *QueryRouter) AddRoute(path string, q sdk.Querier) sdk.QueryRouter {
|
2019-02-05 19:11:57 -08:00
|
|
|
if !isAlphaNumeric(path) {
|
2018-07-31 18:48:32 -07:00
|
|
|
panic("route expressions can only contain alphanumeric characters")
|
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
if qrt.routes[path] != nil {
|
|
|
|
panic(fmt.Sprintf("route %s has already been initialized", path))
|
2018-08-16 18:35:17 -07:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
|
|
|
qrt.routes[path] = q
|
|
|
|
return qrt
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Route returns the Querier for a given query route path.
|
2019-10-14 15:05:10 -07:00
|
|
|
func (qrt *QueryRouter) Route(path string) sdk.Querier {
|
2019-02-05 19:11:57 -08:00
|
|
|
return qrt.routes[path]
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|