增加三个等级分类的路由器
This commit is contained in:
parent
0006ab049f
commit
09f9a2c8bf
|
@ -0,0 +1,32 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewLevel1Router[Route comparable, Handle any]() *Level1Router[Route, Handle] {
|
||||||
|
return &Level1Router[Route, Handle]{
|
||||||
|
routes: map[Route]Handle{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Level1Router 支持一级分类的路由器
|
||||||
|
type Level1Router[Route comparable, Handle any] struct {
|
||||||
|
routes map[Route]Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Level1Router[Route, Handle]) Route(route Route, handleFunc Handle) {
|
||||||
|
if reflect.TypeOf(handleFunc).Kind() != reflect.Func {
|
||||||
|
panic(fmt.Errorf("route[%v] registration failed, handle must be a function type", route))
|
||||||
|
}
|
||||||
|
_, exist := slf.routes[route]
|
||||||
|
if exist {
|
||||||
|
panic(fmt.Errorf("the route[%v] has already been registered, duplicate registration is not allowed", route))
|
||||||
|
}
|
||||||
|
slf.routes[route] = handleFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Level1Router[Route, Handle]) Match(route Route) Handle {
|
||||||
|
return slf.routes[route]
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewLevel2Router[Route comparable, Handle any]() *Level2Router[Route, Handle] {
|
||||||
|
return &Level2Router[Route, Handle]{
|
||||||
|
routes: map[Route]map[Route]Handle{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Level2Router 支持二级分类的路由器
|
||||||
|
type Level2Router[Route comparable, Handle any] struct {
|
||||||
|
routes map[Route]map[Route]Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Level2Router[Route, Handle]) Route(topRoute Route, route Route, handleFunc Handle) {
|
||||||
|
if reflect.TypeOf(handleFunc).Kind() != reflect.Func {
|
||||||
|
panic(fmt.Errorf("route[%v] registration failed, handle must be a function type", route))
|
||||||
|
}
|
||||||
|
routes, exist := slf.routes[topRoute]
|
||||||
|
if !exist {
|
||||||
|
routes = map[Route]Handle{}
|
||||||
|
slf.routes[topRoute] = routes
|
||||||
|
}
|
||||||
|
|
||||||
|
_, exist = routes[route]
|
||||||
|
if exist {
|
||||||
|
panic(fmt.Errorf("the route[%v:%v] has already been registered, duplicate registration is not allowed", topRoute, route))
|
||||||
|
}
|
||||||
|
routes[route] = handleFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Level2Router[Route, Handle]) Match(topRoute Route, route Route) Handle {
|
||||||
|
return slf.routes[topRoute][route]
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewLevel3Router[Route comparable, Handle any]() *Level3Router[Route, Handle] {
|
||||||
|
return &Level3Router[Route, Handle]{
|
||||||
|
routes: map[Route]map[Route]map[Route]Handle{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Level3Router 支持三级分类的路由器
|
||||||
|
type Level3Router[Route comparable, Handle any] struct {
|
||||||
|
routes map[Route]map[Route]map[Route]Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Level3Router[Route, Handle]) Route(topRoute Route, level2Route Route, route Route, handleFunc Handle) {
|
||||||
|
if reflect.TypeOf(handleFunc).Kind() != reflect.Func {
|
||||||
|
panic(fmt.Errorf("route[%v] registration failed, handle must be a function type", route))
|
||||||
|
}
|
||||||
|
routes, exist := slf.routes[topRoute]
|
||||||
|
if !exist {
|
||||||
|
routes = map[Route]map[Route]Handle{}
|
||||||
|
slf.routes[topRoute] = routes
|
||||||
|
}
|
||||||
|
level2Routes, exist := routes[level2Route]
|
||||||
|
if !exist {
|
||||||
|
level2Routes = map[Route]Handle{}
|
||||||
|
routes[level2Route] = level2Routes
|
||||||
|
}
|
||||||
|
_, exist = level2Routes[route]
|
||||||
|
if exist {
|
||||||
|
panic(fmt.Errorf("the route[%v:%v:%v] has already been registered, duplicate registration is not allowed", topRoute, level2Route, route))
|
||||||
|
}
|
||||||
|
level2Routes[route] = handleFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *Level3Router[Route, Handle]) Match(topRoute Route, level2Route Route, route Route) Handle {
|
||||||
|
return slf.routes[topRoute][level2Route][route]
|
||||||
|
}
|
Loading…
Reference in New Issue