router: minor improvement

This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-09-18 20:46:37 +03:00
parent 3d99983d96
commit 070a81d38f
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503

View File

@ -127,24 +127,22 @@ func (tr *trie) insert(path string, route context.RouteReadOnly, handlers contex
var paramKeys []string var paramKeys []string
for _, s := range input { for i, s := range input {
if len(s) == 0 { if len(s) == 0 {
continue continue
} }
c := s[0] c := s[0]
if isParam, isWildcard := c == paramStartCharacter, c == wildcardParamStartCharacter; isParam || isWildcard { if len(s)-1 > i+1 && s[i+1] != '/' { // has next character and it's not slash.
n.hasDynamicChild = true // get the next character, if not slash then this is a parameter.
paramKeys = append(paramKeys, s[1:]) // without : or *. // E.g:
// If /test/:param (or /test/*param) then it's dynamic.
// if node has already a wildcard, don't force a value, check for true only. // If /test/: (or /test/*) then it's static.
if isParam { if c == paramStartCharacter {
n.childNamedParameter = true n.childNamedParameter = true
s = ParamStart s = ParamStart
} } else if c == wildcardParamStartCharacter {
if isWildcard {
n.childWildcardParameter = true n.childWildcardParameter = true
s = WildcardParamStart s = WildcardParamStart
if tr.root == n { if tr.root == n {
@ -205,16 +203,6 @@ func (tr *trie) search(q string, params *context.RequestParams) *trieNode {
segment := q[start:i] segment := q[start:i]
if child := n.getChild(segment); child != nil { if child := n.getChild(segment); child != nil {
n = child n = child
// Possible reserved param character, should catch it as
// dynamic node instead of static-path based.
if segment == ParamStart { // len(n.paramKeys) > 0 && (segment == ParamStart || segment == WildcardParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = segment
} else {
paramValues = append(paramValues, segment)
}
}
} else if n.childNamedParameter { } else if n.childNamedParameter {
n = n.getChild(ParamStart) n = n.getChild(ParamStart)
if ln := len(paramValues); cap(paramValues) > ln { if ln := len(paramValues); cap(paramValues) > ln {