1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-15 01:34:53 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2015-04-13 21:57:36 -07:00
parent 604d9411fd
commit 90ff3c14b2
2 changed files with 47 additions and 49 deletions

View File

@ -8,15 +8,15 @@ type (
echo *Echo echo *Echo
} }
node struct { node struct {
label byte label byte
prefix string prefix string
parent *node parent *node
edges edges children children
handler HandlerFunc handler HandlerFunc
echo *Echo echo *Echo
} }
edges []*node children []*node
param struct { param struct {
Name string Name string
Value string Value string
} }
@ -30,8 +30,8 @@ func NewRouter(e *Echo) (r *router) {
} }
for _, m := range methods { for _, m := range methods {
r.trees[m] = &node{ r.trees[m] = &node{
prefix: "", prefix: "",
edges: edges{}, children: children{},
} }
} }
return return
@ -75,8 +75,8 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
} }
} else if l < pl { } else if l < pl {
// Split node // Split node
n := newNode(cn.prefix[l:], cn, cn.edges, cn.handler, cn.echo) n := newNode(cn.prefix[l:], cn, cn.children, cn.handler, cn.echo)
cn.edges = edges{n} // Add to parent cn.children = children{n} // Add to parent
// Reset parent node // Reset parent node
cn.label = cn.prefix[0] cn.label = cn.prefix[0]
@ -90,20 +90,20 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
cn.echo = echo cn.echo = echo
} else { } else {
// Create child node // Create child node
n = newNode(search[l:], cn, edges{}, h, echo) n = newNode(search[l:], cn, children{}, h, echo)
cn.edges = append(cn.edges, n) cn.children = append(cn.children, n)
} }
} else if l < sl { } else if l < sl {
search = search[l:] search = search[l:]
e := cn.findEdge(search[0]) c := cn.findChild(search[0])
if e != nil { if c != nil {
// Go deeper // Go deeper
cn = e cn = c
continue continue
} }
// Create child node // Create child node
n := newNode(search, cn, edges{}, h, echo) n := newNode(search, cn, children{}, h, echo)
cn.edges = append(cn.edges, n) cn.children = append(cn.children, n)
} else { } else {
// Node already exists // Node already exists
if h != nil { if h != nil {
@ -115,22 +115,22 @@ func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
} }
} }
func newNode(pfx string, p *node, e edges, h HandlerFunc, echo *Echo) (n *node) { func newNode(pfx string, p *node, c children, h HandlerFunc, echo *Echo) (n *node) {
n = &node{ n = &node{
label: pfx[0], label: pfx[0],
prefix: pfx, prefix: pfx,
parent: p, parent: p,
edges: e, children: c,
handler: h, handler: h,
echo: echo, echo: echo,
} }
return return
} }
func (n *node) findEdge(l byte) *node { func (n *node) findChild(l byte) *node {
for _, e := range n.edges { for _, c := range n.children {
if e.label == l { if c.label == l {
return e return c
} }
} }
return nil return nil
@ -151,7 +151,8 @@ func lcp(a, b string) (i int) {
func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *Echo) { func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *Echo) {
cn := r.trees[method] // Current node as root cn := r.trees[method] // Current node as root
search := path search := path
n := 0 // Param count n := 0 // Param count
c := new(node) // Child node
// Search order static > param > catch-all // Search order static > param > catch-all
for { for {
@ -162,30 +163,27 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
return return
} }
var e *node
pl := len(cn.prefix) pl := len(cn.prefix)
l := lcp(search, cn.prefix) l := lcp(search, cn.prefix)
if l == pl { if l == pl {
search = search[l:] search = search[l:]
} else if l < pl { } else if l < pl && cn.label != ':' {
if cn.label != ':' { goto Up
goto Up
}
} }
// Static node // Static node
e = cn.findEdge(search[0]) c = cn.findChild(search[0])
if e != nil { if c != nil {
cn = e cn = c
continue continue
} }
// Param node // Param node
Param: Param:
e = cn.findEdge(':') c = cn.findChild(':')
if e != nil { if c != nil {
cn = e cn = c
i, l := 0, len(search) i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ { for ; i < l && search[i] != '/'; i++ {
} }
@ -197,9 +195,9 @@ func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *
} }
// Catch-all node // Catch-all node
e = cn.findEdge('*') c = cn.findChild('*')
if e != nil { if c != nil {
cn = e cn = c
p := params[:n+1] p := params[:n+1]
p[n].Name = "_name" p[n].Name = "_name"
p[n].Value = search p[n].Value = search

View File

@ -531,14 +531,14 @@ func (n *node) printTree(pfx string, tail bool) {
p := prefix(tail, pfx, "└── ", "├── ") p := prefix(tail, pfx, "└── ", "├── ")
fmt.Printf("%s%s, %p: parent=%p, handler=%v, echo=%v\n", p, n.prefix, n, n.parent, n.handler, n.echo) fmt.Printf("%s%s, %p: parent=%p, handler=%v, echo=%v\n", p, n.prefix, n, n.parent, n.handler, n.echo)
nodes := n.edges children := n.children
l := len(nodes) l := len(children)
p = prefix(tail, pfx, " ", "│ ") p = prefix(tail, pfx, " ", "│ ")
for i := 0; i < l-1; i++ { for i := 0; i < l-1; i++ {
nodes[i].printTree(p, false) children[i].printTree(p, false)
} }
if l > 0 { if l > 0 {
nodes[l-1].printTree(p, true) children[l-1].printTree(p, true)
} }
} }