A little optimisation in get() and add() methods.
This commit is contained in:
parent
32e7468eef
commit
32ae3a3d0d
27
httpr.go
27
httpr.go
@ -89,7 +89,9 @@ outer:
|
|||||||
path[i] = curNode.endpoint + ":" + path[i]
|
path[i] = curNode.endpoint + ":" + path[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
if pathLen == i+1 {
|
pathNextIdx := i + 1
|
||||||
|
|
||||||
|
if pathLen == pathNextIdx {
|
||||||
var params Params = make(Params)
|
var params Params = make(Params)
|
||||||
|
|
||||||
for _, part := range path {
|
for _, part := range path {
|
||||||
@ -102,14 +104,15 @@ outer:
|
|||||||
return curNode.handler, params
|
return curNode.handler, params
|
||||||
}
|
}
|
||||||
|
|
||||||
if pathLen > i+1 {
|
if pathLen > pathNextIdx {
|
||||||
var paramNode *node
|
|
||||||
if len(curNode.children) == 0 {
|
if len(curNode.children) == 0 {
|
||||||
break outer
|
break outer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var paramNode *node
|
||||||
|
|
||||||
for _, next := range curNode.children {
|
for _, next := range curNode.children {
|
||||||
if next.endpoint == path[i+1] {
|
if next.endpoint == path[pathNextIdx] {
|
||||||
curNode = next
|
curNode = next
|
||||||
continue outer
|
continue outer
|
||||||
}
|
}
|
||||||
@ -132,12 +135,12 @@ outer:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) add(path path, handler http.HandlerFunc) error {
|
func (n *node) add(path path, handler http.HandlerFunc) error {
|
||||||
pathLen := len(path)
|
pathLastIdx := len(path) - 1
|
||||||
curNode := n
|
curNode := n
|
||||||
|
|
||||||
outer:
|
outer:
|
||||||
for i := range path {
|
for i := range path {
|
||||||
if pathLen == i+1 {
|
if pathLastIdx == i {
|
||||||
if curNode.handler != nil {
|
if curNode.handler != nil {
|
||||||
return errors.New("attempt to redefine a handler for already existing path")
|
return errors.New("attempt to redefine a handler for already existing path")
|
||||||
}
|
}
|
||||||
@ -146,27 +149,29 @@ outer:
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pathNextIdx := i + 1
|
||||||
|
|
||||||
for _, child := range curNode.children {
|
for _, child := range curNode.children {
|
||||||
firstChar := path[i+1][0]
|
firstChar := path[pathNextIdx][0]
|
||||||
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
|
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
|
||||||
// Do not allow different param names, because only the first one
|
// Do not allow different param names, because only the first one
|
||||||
// is saved, so a param won't be available by a new name.
|
// is saved, so a param won't be available by a new name.
|
||||||
// Therefore, it is good to return an error because in this case
|
// Therefore, it is good to return an error because in this case
|
||||||
// you're doing something wrong.
|
// you're doing something wrong.
|
||||||
if path[i+1] != child.endpoint {
|
if path[pathNextIdx] != child.endpoint {
|
||||||
return errors.New("param names " + path[i+1] + " and " + child.endpoint + " are differ")
|
return errors.New("param names " + path[pathNextIdx] + " and " + child.endpoint + " are differ")
|
||||||
}
|
}
|
||||||
curNode = child
|
curNode = child
|
||||||
continue outer
|
continue outer
|
||||||
}
|
}
|
||||||
|
|
||||||
if child.endpoint == path[i+1] {
|
if child.endpoint == path[pathNextIdx] {
|
||||||
curNode = child
|
curNode = child
|
||||||
continue outer
|
continue outer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newChild := &node{endpoint: path[i+1]}
|
newChild := &node{endpoint: path[pathNextIdx]}
|
||||||
curNode.children = append(curNode.children, newChild)
|
curNode.children = append(curNode.children, newChild)
|
||||||
curNode = newChild
|
curNode = newChild
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user