Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix zeromicro#683

* fix errors
  • Loading branch information
kevwan authored May 15, 2021
1 parent edf743c commit 491213a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
55 changes: 37 additions & 18 deletions core/search/tree.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package search

import "errors"
import (
"errors"
"fmt"
)

const (
colon = ':'
slash = '/'
)

var (
// ErrDupItem means adding duplicated item.
ErrDupItem = errors.New("duplicated item")
// ErrDupSlash means item is started with more than one slash.
ErrDupSlash = errors.New("duplicated slash")
// ErrEmptyItem means adding empty item.
ErrEmptyItem = errors.New("empty item")
// ErrInvalidState means search tree is in an invalid state.
ErrInvalidState = errors.New("search tree is in an invalid state")
// ErrNotFromRoot means path is not starting with slash.
ErrNotFromRoot = errors.New("path should start with /")
// errDupItem means adding duplicated item.
errDupItem = errors.New("duplicated item")
// errDupSlash means item is started with more than one slash.
errDupSlash = errors.New("duplicated slash")
// errEmptyItem means adding empty item.
errEmptyItem = errors.New("empty item")
// errInvalidState means search tree is in an invalid state.
errInvalidState = errors.New("search tree is in an invalid state")
// errNotFromRoot means path is not starting with slash.
errNotFromRoot = errors.New("path should start with /")

// NotFound is used to hold the not found result.
NotFound Result
Expand Down Expand Up @@ -58,14 +61,22 @@ func NewTree() *Tree {
// Add adds item to associate with route.
func (t *Tree) Add(route string, item interface{}) error {
if len(route) == 0 || route[0] != slash {
return ErrNotFromRoot
return errNotFromRoot
}

if item == nil {
return ErrEmptyItem
return errEmptyItem
}

return add(t.root, route[1:], item)
err := add(t.root, route[1:], item)
switch err {
case errDupItem:
return duplicatedItem(route)
case errDupSlash:
return duplicatedSlash(route)
default:
return err
}
}

// Search searches item that associates with given route.
Expand Down Expand Up @@ -141,15 +152,15 @@ func (nd *node) getChildren(route string) map[string]*node {
func add(nd *node, route string, item interface{}) error {
if len(route) == 0 {
if nd.item != nil {
return ErrDupItem
return errDupItem
}

nd.item = item
return nil
}

if route[0] == slash {
return ErrDupSlash
return errDupSlash
}

for i := range route {
Expand All @@ -161,7 +172,7 @@ func add(nd *node, route string, item interface{}) error {
return add(child, route[i+1:], item)
}

return ErrInvalidState
return errInvalidState
}

child := newNode(nil)
Expand All @@ -173,7 +184,7 @@ func add(nd *node, route string, item interface{}) error {
children := nd.getChildren(route)
if child, ok := children[route]; ok {
if child.item != nil {
return ErrDupItem
return errDupItem
}

child.item = item
Expand All @@ -192,6 +203,14 @@ func addParam(result *Result, k, v string) {
result.Params[k] = v
}

func duplicatedItem(item string) error {
return fmt.Errorf("duplicated item for %s", item)
}

func duplicatedSlash(item string) error {
return fmt.Errorf("duplicated slash for %s", item)
}

func match(pat, token string) innerResult {
if pat[0] == colon {
return innerResult{
Expand Down
12 changes: 6 additions & 6 deletions core/search/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func TestAddDuplicate(t *testing.T) {
err := tree.Add("/a/b", 1)
assert.Nil(t, err)
err = tree.Add("/a/b", 2)
assert.Equal(t, ErrDupItem, err)
assert.Error(t, errDupItem, err)
err = tree.Add("/a/b/", 2)
assert.Equal(t, ErrDupItem, err)
assert.Error(t, errDupItem, err)
}

func TestPlain(t *testing.T) {
Expand All @@ -169,19 +169,19 @@ func TestPlain(t *testing.T) {
func TestSearchWithDoubleSlashes(t *testing.T) {
tree := NewTree()
err := tree.Add("//a", 1)
assert.Error(t, ErrDupSlash, err)
assert.Error(t, errDupSlash, err)
}

func TestSearchInvalidRoute(t *testing.T) {
tree := NewTree()
err := tree.Add("", 1)
assert.Equal(t, ErrNotFromRoot, err)
assert.Equal(t, errNotFromRoot, err)
err = tree.Add("bad", 1)
assert.Equal(t, ErrNotFromRoot, err)
assert.Equal(t, errNotFromRoot, err)
}

func TestSearchInvalidItem(t *testing.T) {
tree := NewTree()
err := tree.Add("/", nil)
assert.Equal(t, ErrEmptyItem, err)
assert.Equal(t, errEmptyItem, err)
}

0 comments on commit 491213a

Please sign in to comment.