Skip to content

Commit

Permalink
fix: last param match /
Browse files Browse the repository at this point in the history
  • Loading branch information
tonny-zhang committed Jun 6, 2023
1 parent ee192bb commit d0d126d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vscode
.DS_Store
4 changes: 2 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (n *node) insertNode(key string, fullpath string) {
}

func (n *node) find(path string) (result resultFind) {
result.params = make(map[string]string)
if path == "/" && n.key == path {
if n.isRealNode {
result.node = n
Expand All @@ -194,7 +195,6 @@ func (n *node) find(path string) (result resultFind) {
var ok bool
var start, lenstr = 1, len(path)

result.params = make(map[string]string)
for i := start; i < lenstr; i++ {
if path[i] == '/' {
key := path[start:i]
Expand Down Expand Up @@ -227,7 +227,7 @@ func (n *node) find(path string) (result resultFind) {
}
key := path[start:]
child, ok = n.children[key]
if !ok {
if !ok && key != "/" {
child, ok = n.children[keyParam]
if ok {
result.params[child.paramName] = key
Expand Down
21 changes: 21 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ func TestTree(t *testing.T) {
}
}
}
func TestEmptyParam(t *testing.T) {
tree := newTree()
tree.add("/test/:conf", nil)
tree.add("/test1/:conf/:test", nil)

result := tree.root.find("/test/abc")
assert.Equal(t, map[string]string{
"conf": "abc",
}, result.params)

// not match anything
result = tree.root.find("/test/")
assert.Equal(t, map[string]string{}, result.params)
// not match anything
result = tree.root.find("/")
assert.Equal(t, map[string]string{}, result.params)

// not match anything
result = tree.root.find("/test1/")
assert.Equal(t, map[string]string{}, result.params)
}
func TestTreeNotFound(t *testing.T) {
tree := newTree()
tree.add("/v1/a", nil)
Expand Down

0 comments on commit d0d126d

Please sign in to comment.