Skip to content

Commit

Permalink
Allowing overlapping catch all parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Apr 4, 2023
1 parent 8b7acb0 commit b53d46f
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 507 deletions.
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type RouteConflictError struct {

func newConflictErr(method, path, catchAllKey string, matched []string) *RouteConflictError {
if catchAllKey != "" {
path += "*" + catchAllKey
path += "*{" + catchAllKey + "}"
}
return &RouteConflictError{
Method: method,
Expand Down
18 changes: 5 additions & 13 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ func (it *Iterator) Handler() Handler {
return nil
}

type stack struct {
method string
edges []*node
}

func newRawIterator(n *node) *rawIterator {
return &rawIterator{
stack: []stack{{edges: []*node{n}}},
Expand All @@ -193,19 +198,6 @@ type rawIterator struct {
stack []stack
}

type stack struct {
method string
edges []*node
}

func (it *rawIterator) fullPath() string {
return it.path
}

func (it *rawIterator) node() *node {
return it.current
}

func (it *rawIterator) hasNext() bool {
for len(it.stack) > 0 {
n := len(it.stack)
Expand Down
21 changes: 21 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,24 @@ func (n *node) string(space int) string {
}
return sb.String()
}

type skippedNodes []skippedNode

func (n *skippedNodes) free(t *Tree) {
if cap(*n) < int(t.maxDepth.Load()) {
return
}
*n = (*n)[:0]
t.np.Put(n)
}

func (n *skippedNodes) pop() skippedNode {
skipped := (*n)[len(*n)-1]
*n = (*n)[:len(*n)-1]
return skipped
}

type skippedNode struct {
node *node
pathIndex int
}
15 changes: 12 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func (fox *Router) NewTree() *Tree {

tree.np = sync.Pool{
New: func() any {
skippedNodes := make([]skippedNode, 0, tree.maxDepth.Load())
return &skippedNodes
skpNds := make(skippedNodes, 0, tree.maxDepth.Load())
return &skpNds
},
}

Expand Down Expand Up @@ -232,7 +232,7 @@ NEXT:
method := nds[i].key
it := newRawIterator(nds[i])
for it.hasNext() {
err := fn(method, it.fullPath(), it.node().handler)
err := fn(method, it.path, it.current.handler)
if err != nil {
if errors.Is(err, SkipMethod) {
continue NEXT
Expand Down Expand Up @@ -520,6 +520,15 @@ func parseRoute(path string) (string, string, int, error) {

func getRouteConflict(n *node) []string {
routes := make([]string, 0)

if n.isCatchAll() {
routes = append(routes, n.path)
return routes
}

if n.paramChildIndex >= 0 {
n = n.children[n.paramChildIndex].Load()
}
it := newRawIterator(n)
for it.hasNext() {
routes = append(routes, it.current.path)
Expand Down
Loading

0 comments on commit b53d46f

Please sign in to comment.