Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
router orders & more better tools for print routers
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Apr 7, 2015
1 parent a0b54a9 commit 8f4289e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
68 changes: 41 additions & 27 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type (
regexp *regexp.Regexp // regexp if tp is rnode
content string // static content or named
edges edges // children
path string // executor path
}
edges []*node
)
Expand All @@ -131,7 +132,7 @@ func (e edges) Less(i, j int) bool {
if e[j].tp == snode {
return false
}
return true
return i < j
}

const (
Expand Down Expand Up @@ -242,103 +243,116 @@ func parseNodes(path string) []*node {
func printNode(i int, node *node) {
for _, c := range node.edges {
for j := 0; j < i; j++ {
fmt.Print(" ")
fmt.Print(" ")
}
if i > 1 {
fmt.Print("┗", " ")
}
fmt.Println(c.content, c.handle)

fmt.Print(c.content)
if c.handle != nil {
fmt.Print(" ", c.handle.method.Type())
fmt.Printf(" %p", c.handle.method.Interface())
}
fmt.Println()
printNode(i+1, c)
}
}

func (r *router) printTrees() {
for _, method := range SupportMethods {
fmt.Print(method)
printNode(1, r.trees[method])
fmt.Println()
if len(r.trees[method].edges) > 0 {
fmt.Println(method)
printNode(1, r.trees[method])
fmt.Println()
}
}
}

func (r *router) addRoute(method, path string, h *Route) {
nodes := parseNodes(path)
nodes[len(nodes)-1].handle = h
nodes[len(nodes)-1].path = path
if !validNodes(nodes) {
panic(fmt.Sprintln("express", path, "is not supported"))
}
r.addnodes(method, nodes)
//r.printTrees()
}

func (r *router) matchNode(n *node, url string, params Params) (*Route, Params) {
func (r *router) matchNode(n *node, url string, params *Params) *node {
if n.tp == snode {
if strings.HasPrefix(url, n.content) {
if len(url) == len(n.content) {
return n.handle, params
return n
}
for _, c := range n.edges {
e, p := r.matchNode(c, url[len(n.content):], params)
e := r.matchNode(c, url[len(n.content):], params)
if e != nil {
return e, p
return e
}
}
}
} else if n.tp == anode {
if len(n.edges) == 0 {
params = append(params, param{n.content, url})
return n.handle, params
*params = append(*params, param{n.content, url})
return n
}
for _, c := range n.edges {
idx := strings.Index(url, c.content)
idx := strings.LastIndex(url, c.content)
if idx > -1 {
params = append(params, param{n.content, url[:idx]})
*params = append(*params, param{n.content, url[:idx]})
return r.matchNode(c, url[idx:], params)
}
}
} else if n.tp == nnode {
idx := strings.IndexByte(url, '/')
if idx > -1 {
params = append(params, param{n.content, url[:idx]})
for _, c := range n.edges {
h, p := r.matchNode(c, url[idx:], params)
h := r.matchNode(c, url[idx:], params)
if h != nil {
return h, p
*params = append(*params, param{n.content, url[:idx]})
return h
}
}
return nil, nil
return nil
}

if len(n.edges) == 0 {
params = append(params, param{n.content, url})
return n.handle, params
*params = append(*params, param{n.content, url})
return n
}
for _, c := range n.edges {
idx := strings.Index(url, c.content)
if idx > -1 {
params = append(params, param{n.content, url[:idx]})
*params = append(*params, param{n.content, url[:idx]})
return r.matchNode(c, url[idx:], params)
}
}
} else if n.tp == rnode {
if len(n.edges) == 0 && n.regexp.MatchString(url) {
params = append(params, param{n.content, url})
return n.handle, params
*params = append(*params, param{n.content, url})
return n
}
for _, c := range n.edges {
idx := strings.Index(url, c.content)
if idx > -1 && n.regexp.MatchString(url[:idx]) {
params = append(params, param{n.content, url[:idx]})
*params = append(*params, param{n.content, url[:idx]})
return r.matchNode(c, url[idx:], params)
}
}
}
return nil, nil
return nil
}

func (r *router) Match(url, method string) (*Route, Params) {
cn := r.trees[method]
var params = make(Params, 0, strings.Count(url, "/"))
for _, n := range cn.edges {
e, p := r.matchNode(n, url, params)
e := r.matchNode(n, url, &params)
if e != nil {
return e, p
//fmt.Println("matched:", e.path, params)
return e.handle, params
}
}
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion tan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
)

func Version() string {
return "0.4.2.0407"
return "0.4.2.0408"
}

type Tango struct {
Expand Down

0 comments on commit 8f4289e

Please sign in to comment.