1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00

Cleaner find logic in router

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-04 10:44:48 -07:00
parent b75aa02cfe
commit 3680eb5391
2 changed files with 46 additions and 36 deletions

View File

@ -12,8 +12,8 @@ type (
prefix string
has ntype // Type of node it contains
handler HandlerFunc
echo *Echo
edges edges
echo *Echo
}
edges []*node
ntype byte
@ -49,22 +49,22 @@ func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
l := len(path)
for ; i < l; i++ {
if path[i] == ':' {
r.insert(method, path[:i], nil, echo, pnode)
r.insert(method, path[:i], nil, pnode, echo)
for ; i < l && path[i] != '/'; i++ {
}
if i == l {
r.insert(method, path[:i], h, echo, snode)
r.insert(method, path[:i], h, snode, echo)
return
}
r.insert(method, path[:i], nil, echo, snode)
r.insert(method, path[:i], nil, snode, echo)
} else if path[i] == '*' {
r.insert(method, path[:i], h, echo, anode)
r.insert(method, path[:i], h, anode, echo)
}
}
r.insert(method, path, h, echo, snode)
r.insert(method, path, h, snode, echo)
}
func (r *router) insert(method, path string, h HandlerFunc, echo *Echo, has ntype) {
func (r *router) insert(method, path string, h HandlerFunc, has ntype, echo *Echo) {
cn := r.trees[method] // Current node as root
search := path
@ -85,7 +85,7 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo, has ntyp
return
} else if l < pl {
// Split the node
n := newNode(cn.prefix[l:], cn.has, cn.handler, cn.echo, cn.edges)
n := newNode(cn.prefix[l:], cn.has, cn.handler, cn.edges, cn.echo)
cn.edges = edges{n} // Add to parent
// Reset parent node
@ -100,8 +100,8 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo, has ntyp
cn.handler = h
cn.echo = echo
} else {
// Need to fork a node
n = newNode(search[l:], has, h, echo, edges{})
// Need to create a node
n = newNode(search[l:], has, h, edges{}, echo)
cn.edges = append(cn.edges, n)
}
break
@ -109,7 +109,7 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo, has ntyp
search = search[l:]
e := cn.findEdge(search[0])
if e == nil {
n := newNode(search, has, h, echo, edges{})
n := newNode(search, has, h, edges{}, echo)
cn.edges = append(cn.edges, n)
break
} else {
@ -126,14 +126,14 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo, has ntyp
}
}
func newNode(pfx string, has ntype, h HandlerFunc, echo *Echo, e edges) (n *node) {
func newNode(pfx string, has ntype, h HandlerFunc, e edges, echo *Echo) (n *node) {
n = &node{
label: pfx[0],
prefix: pfx,
has: has,
handler: h,
echo: echo,
edges: e,
echo: echo,
}
return
}
@ -178,32 +178,30 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
if l == pl {
search = search[l:]
switch cn.has {
case pnode:
if cn.has == pnode {
cn = cn.edges[0]
i := 0
l = len(search)
for ; i < l && search[i] != '/'; i++ {
}
p := c.params[:n+1]
p[n].Name = cn.prefix[1:]
p[n].Value = search[:i]
n++
search = search[i:]
if i == l {
// All params read
continue
}
case anode:
} else if cn.has == anode {
p := c.params[:n+1]
p[n].Name = "_name"
p[n].Value = search
search = "" // End search
}
// Search complete
if len(search) == 0 {
continue
}
// Dig more
e := cn.findEdge(search[0])
if e == nil {
// Not found

View File

@ -21,9 +21,23 @@ func TestRouterParam(t *testing.T) {
if h == nil {
t.Fatal("handle not found")
}
p := c.Param("id")
if p != "1" {
t.Errorf("id should be equal to 1, found %s", p)
if c.P(0) != "1" {
t.Error("param id should be 1")
}
}
func TestRouterTwoParam(t *testing.T) {
r := New().Router
r.Add(MethodGET, "/users/:uid/files/:fid", func(c *Context) {}, nil)
h, c, _ := r.Find(MethodGET, "/users/1/files/1")
if h == nil {
t.Fatal("handle not found")
}
if c.P(0) != "1" {
t.Error("param uid should be 1")
}
if c.P(1) != "1" {
t.Error("param fid should be 1")
}
}
@ -39,21 +53,18 @@ func TestRouterCatchAll(t *testing.T) {
func TestRouterMicroParam(t *testing.T) {
r := New().Router
r.Add(MethodGET, "/:a/:b/:c", func(c *Context) {}, nil)
h, c, _ := r.Find(MethodGET, "/a/b/c")
h, c, _ := r.Find(MethodGET, "/1/2/3")
if h == nil {
t.Fatal("handle not found")
}
p1 := c.P(0)
if p1 != "a" {
t.Errorf("p1 should be equal to a, found %s", p1)
if c.P(0) != "1" {
t.Error("param a should be 1")
}
p2 := c.P(1)
if p2 != "b" {
t.Errorf("p2 should be equal to b, found %s", p2)
if c.P(1) != "2" {
t.Error("param b should be 2")
}
p3 := c.P(2)
if p3 != "c" {
t.Errorf("p3 should be equal to c, found %s", p3)
if c.P(2) != "3" {
t.Error("param c should be 3")
}
}
@ -62,6 +73,7 @@ func TestRouterConflict(t *testing.T) {
r.Add("GET", "/users/new", func(*Context) {}, nil)
r.Add("GET", "/users/wen", func(*Context) {}, nil)
r.Add("GET", "/users/:id", func(*Context) {}, nil)
r.Add("GET", "/users/*", func(*Context) {}, nil)
r.trees["GET"].printTree("", true)
}