-
Notifications
You must be signed in to change notification settings - Fork 580
/
router.go
65 lines (55 loc) · 1.62 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// The router is a map from module name to the IBCModule
// which contains all the module-defined callbacks required by ICS-26
type Router struct {
routes map[string]IBCModule
sealed bool
}
func NewRouter() *Router {
return &Router{
routes: make(map[string]IBCModule),
}
}
// Seal prevents the Router from any subsequent route handlers to be registered.
// Seal will panic if called more than once.
func (rtr *Router) Seal() {
if rtr.sealed {
panic("router already sealed")
}
rtr.sealed = true
}
// Sealed returns a boolean signifying if the Router is sealed or not.
func (rtr Router) Sealed() bool {
return rtr.sealed
}
// AddRoute adds IBCModule for a given module name. It returns the Router
// so AddRoute calls can be linked. It will panic if the Router is sealed.
func (rtr *Router) AddRoute(module string, cbs IBCModule) *Router {
if rtr.sealed {
panic(fmt.Sprintf("router sealed; cannot register %s route callbacks", module))
}
if !sdk.IsAlphaNumeric(module) {
panic("route expressions can only contain alphanumeric characters")
}
if rtr.HasRoute(module) {
panic(fmt.Sprintf("route %s has already been registered", module))
}
rtr.routes[module] = cbs
return rtr
}
// HasRoute returns true if the Router has a module registered or false otherwise.
func (rtr *Router) HasRoute(module string) bool {
_, ok := rtr.routes[module]
return ok
}
// GetRoute returns a IBCModule for a given module.
func (rtr *Router) GetRoute(module string) (IBCModule, bool) {
if !rtr.HasRoute(module) {
return nil, false
}
return rtr.routes[module], true
}