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

View File

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