get and add methods get rewritten in an iterative manner instead of recursive.
This commit is contained in:
parent
538f1bd676
commit
89c2333a4f
84
httpr.go
84
httpr.go
@ -41,21 +41,26 @@ type node struct {
|
|||||||
handler http.HandlerFunc
|
handler http.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) get(path path, idx int) (http.HandlerFunc, Params) {
|
func (n *node) get(path path) (http.HandlerFunc, Params) {
|
||||||
|
pathLen := len(path)
|
||||||
|
curNode := n
|
||||||
|
|
||||||
|
outer:
|
||||||
|
for i := range path {
|
||||||
// Check if this node is a catch-all endpoint.
|
// Check if this node is a catch-all endpoint.
|
||||||
if n.endpoint[0] == '*' {
|
if curNode.endpoint[0] == '*' {
|
||||||
var p Params = Params{}
|
var p Params = Params{}
|
||||||
p[n.endpoint[1:]] = strings.Join(path[idx:], "/")
|
p[curNode.endpoint[1:]] = strings.Join(path[i:], "/")
|
||||||
return n.handler, p
|
return curNode.handler, p
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this endpoint is a parameter, then add its name to a path's part.
|
// If this is a parametrised endpoint, then add its name to
|
||||||
// This will be used further to fill Params.
|
// a path's part. It will be used further to parse parameters.
|
||||||
if n.endpoint[0] == ':' {
|
if curNode.endpoint[0] == ':' {
|
||||||
path[idx] = n.endpoint + ":" + path[idx]
|
path[i] = curNode.endpoint + ":" + path[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(path) == idx+1 {
|
if pathLen == i+1 {
|
||||||
var params Params = make(Params)
|
var params Params = make(Params)
|
||||||
|
|
||||||
for _, part := range path {
|
for _, part := range path {
|
||||||
@ -68,56 +73,61 @@ func (n *node) get(path path, idx int) (http.HandlerFunc, Params) {
|
|||||||
return n.handler, params
|
return n.handler, params
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(path) > idx+1 {
|
if pathLen > i+1 {
|
||||||
var wildcardOrParam *node
|
var paramNode *node
|
||||||
for _, next := range n.children {
|
for _, next := range n.children {
|
||||||
if next.endpoint == path[idx+1] {
|
if next.endpoint == path[i+1] {
|
||||||
return next.get(path, idx+1)
|
curNode = next
|
||||||
|
continue outer
|
||||||
}
|
}
|
||||||
|
|
||||||
if next.endpoint[0] == ':' || next.endpoint[0] == '*' {
|
if next.endpoint[0] == ':' || next.endpoint[0] == '*' {
|
||||||
wildcardOrParam = next
|
paramNode = next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if wildcardOrParam != nil {
|
if paramNode != nil {
|
||||||
return wildcardOrParam.get(path, idx+1)
|
curNode = paramNode
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) add(path path, idx int, handler http.HandlerFunc) error {
|
func (n *node) add(path path, handler http.HandlerFunc) error {
|
||||||
// If it is a last part of path, then set a handler to this node.
|
pathLen := len(path)
|
||||||
if len(path) == idx+1 {
|
curNode := n
|
||||||
n.endpoint = path[idx]
|
|
||||||
n.handler = handler
|
outer:
|
||||||
|
for i := range path {
|
||||||
|
if pathLen == i+1 {
|
||||||
|
if curNode.handler != nil {
|
||||||
|
return errors.New("attempt to redefine a handler for already existing path")
|
||||||
|
}
|
||||||
|
curNode.endpoint = path[i]
|
||||||
|
curNode.handler = handler
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if next part is a parameter and if it is, then look for
|
|
||||||
// an already existing endpoint with a different key.
|
|
||||||
if path[idx+1][0] == '*' || path[idx+1][0] == ':' {
|
|
||||||
for _, child := range n.children {
|
for _, child := range n.children {
|
||||||
if (child.endpoint[0] == '*' || child.endpoint[0] == ':') && path[idx+1] != child.endpoint {
|
firstChar := path[i+1][0]
|
||||||
return errors.New("there is already a catch-all or regular param in there! You cannot add a second one")
|
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
|
||||||
|
curNode = child
|
||||||
|
continue outer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if child.endpoint == path[i+1] {
|
||||||
|
curNode = child
|
||||||
|
continue outer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for an already existing endpoint.
|
newChild := &node{endpoint: path[i+1]}
|
||||||
for _, child := range n.children {
|
curNode.children = append(curNode.children, newChild)
|
||||||
if child.endpoint == path[idx+1] {
|
curNode = newChild
|
||||||
child.add(path, idx+1, handler)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// No endpoint was found.
|
|
||||||
new_child := &node{endpoint: path[idx+1]}
|
|
||||||
new_child.add(path, idx+1, handler)
|
|
||||||
n.children = append(n.children, new_child)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user