1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00

router changes

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-10-21 20:40:36 -07:00
parent 4d7d141870
commit 8d97858b68
4 changed files with 43 additions and 43 deletions

View File

@ -234,12 +234,12 @@ func TestContextPath(t *testing.T) {
e := New() e := New()
r := e.Router() r := e.Router()
r.Add(GET, "/users/:id", nil, e) r.Add(GET, "/users/:id", nil)
c := e.NewContext(nil, nil) c := e.NewContext(nil, nil)
r.Find(GET, "/users/1", c) r.Find(GET, "/users/1", c)
assert.Equal(t, "/users/:id", c.Path()) assert.Equal(t, "/users/:id", c.Path())
r.Add(GET, "/users/:uid/files/:fid", nil, e) r.Add(GET, "/users/:uid/files/:fid", nil)
c = e.NewContext(nil, nil) c = e.NewContext(nil, nil)
r.Find(GET, "/users/1/files/1", c) r.Find(GET, "/users/1/files/1", c)
assert.Equal(t, "/users/:uid/files/:fid", c.Path()) assert.Equal(t, "/users/:uid/files/:fid", c.Path())
@ -400,7 +400,7 @@ func TestContextHandler(t *testing.T) {
r.Add(GET, "/handler", func(Context) error { r.Add(GET, "/handler", func(Context) error {
_, err := b.Write([]byte("handler")) _, err := b.Write([]byte("handler"))
return err return err
}, e) })
c := e.NewContext(nil, nil) c := e.NewContext(nil, nil)
r.Find(GET, "/handler", c) r.Find(GET, "/handler", c)
c.Handler()(c) c.Handler()(c)

View File

@ -473,7 +473,7 @@ func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...Middl
h = middleware[i](h) h = middleware[i](h)
} }
return h(c) return h(c)
}, e) })
r := Route{ r := Route{
Method: method, Method: method,
Path: path, Path: path,

View File

@ -51,7 +51,7 @@ func NewRouter(e *Echo) *Router {
} }
// Add registers a new route for method and path with matching handler. // Add registers a new route for method and path with matching handler.
func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) { func (r *Router) Add(method, path string, h HandlerFunc) {
// Validate path // Validate path
if path == "" { if path == "" {
panic("echo: path cannot be empty") panic("echo: path cannot be empty")
@ -66,7 +66,7 @@ func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
if path[i] == ':' { if path[i] == ':' {
j := i + 1 j := i + 1
r.insert(method, path[:i], nil, skind, "", nil, e) r.insert(method, path[:i], nil, skind, "", nil)
for ; i < l && path[i] != '/'; i++ { for ; i < l && path[i] != '/'; i++ {
} }
@ -75,26 +75,26 @@ func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
i, l = j, len(path) i, l = j, len(path)
if i == l { if i == l {
r.insert(method, path[:i], h, pkind, ppath, pnames, e) r.insert(method, path[:i], h, pkind, ppath, pnames)
return return
} }
r.insert(method, path[:i], nil, pkind, ppath, pnames, e) r.insert(method, path[:i], nil, pkind, ppath, pnames)
} else if path[i] == '*' { } else if path[i] == '*' {
r.insert(method, path[:i], nil, skind, "", nil, e) r.insert(method, path[:i], nil, skind, "", nil)
pnames = append(pnames, "_*") pnames = append(pnames, "_*")
r.insert(method, path[:i+1], h, akind, ppath, pnames, e) r.insert(method, path[:i+1], h, akind, ppath, pnames)
return return
} }
} }
r.insert(method, path, h, skind, ppath, pnames, e) r.insert(method, path, h, skind, ppath, pnames)
} }
func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string, e *Echo) { func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string, pnames []string) {
// Adjust max param // Adjust max param
l := len(pnames) l := len(pnames)
if *e.maxParam < l { if *r.echo.maxParam < l {
*e.maxParam = l *r.echo.maxParam = l
} }
cn := r.tree // Current node as root cn := r.tree // Current node as root

View File

@ -280,7 +280,7 @@ func TestRouterStatic(t *testing.T) {
r.Add(GET, path, func(c Context) error { r.Add(GET, path, func(c Context) error {
c.Set("path", path) c.Set("path", path)
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
r.Find(GET, path, c) r.Find(GET, path, c)
c.handler(c) c.handler(c)
@ -292,7 +292,7 @@ func TestRouterParam(t *testing.T) {
r := e.router r := e.router
r.Add(GET, "/users/:id", func(c Context) error { r.Add(GET, "/users/:id", func(c Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/1", c) r.Find(GET, "/users/1", c)
assert.Equal(t, "1", c.P(0)) assert.Equal(t, "1", c.P(0))
@ -303,7 +303,7 @@ func TestRouterTwoParam(t *testing.T) {
r := e.router r := e.router
r.Add(GET, "/users/:uid/files/:fid", func(Context) error { r.Add(GET, "/users/:uid/files/:fid", func(Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/1/files/1", c) r.Find(GET, "/users/1/files/1", c)
@ -318,11 +318,11 @@ func TestRouterParamWithSlash(t *testing.T) {
r.Add(GET, "/a/:b/c/d/:e", func(c Context) error { r.Add(GET, "/a/:b/c/d/:e", func(c Context) error {
return nil return nil
}, e) })
r.Add(GET, "/a/:b/c/:d/:f", func(c Context) error { r.Add(GET, "/a/:b/c/:d/:f", func(c Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
@ -337,13 +337,13 @@ func TestRouterMatchAny(t *testing.T) {
// Routes // Routes
r.Add(GET, "/", func(Context) error { r.Add(GET, "/", func(Context) error {
return nil return nil
}, e) })
r.Add(GET, "/*", func(Context) error { r.Add(GET, "/*", func(Context) error {
return nil return nil
}, e) })
r.Add(GET, "/users/*", func(Context) error { r.Add(GET, "/users/*", func(Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/", c) r.Find(GET, "/", c)
@ -361,7 +361,7 @@ func TestRouterMicroParam(t *testing.T) {
r := e.router r := e.router
r.Add(GET, "/:a/:b/:c", func(c Context) error { r.Add(GET, "/:a/:b/:c", func(c Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/1/2/3", c) r.Find(GET, "/1/2/3", c)
assert.Equal(t, "1", c.P(0)) assert.Equal(t, "1", c.P(0))
@ -376,7 +376,7 @@ func TestRouterMixParamMatchAny(t *testing.T) {
// Route // Route
r.Add(GET, "/users/:id/*", func(c Context) error { r.Add(GET, "/users/:id/*", func(c Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/users/joe/comments", c) r.Find(GET, "/users/joe/comments", c)
@ -392,10 +392,10 @@ func TestRouterMultiRoute(t *testing.T) {
r.Add(GET, "/users", func(c Context) error { r.Add(GET, "/users", func(c Context) error {
c.Set("path", "/users") c.Set("path", "/users")
return nil return nil
}, e) })
r.Add(GET, "/users/:id", func(c Context) error { r.Add(GET, "/users/:id", func(c Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
// Route > /users // Route > /users
@ -422,31 +422,31 @@ func TestRouterPriority(t *testing.T) {
r.Add(GET, "/users", func(c Context) error { r.Add(GET, "/users", func(c Context) error {
c.Set("a", 1) c.Set("a", 1)
return nil return nil
}, e) })
r.Add(GET, "/users/new", func(c Context) error { r.Add(GET, "/users/new", func(c Context) error {
c.Set("b", 2) c.Set("b", 2)
return nil return nil
}, e) })
r.Add(GET, "/users/:id", func(c Context) error { r.Add(GET, "/users/:id", func(c Context) error {
c.Set("c", 3) c.Set("c", 3)
return nil return nil
}, e) })
r.Add(GET, "/users/dew", func(c Context) error { r.Add(GET, "/users/dew", func(c Context) error {
c.Set("d", 4) c.Set("d", 4)
return nil return nil
}, e) })
r.Add(GET, "/users/:id/files", func(c Context) error { r.Add(GET, "/users/:id/files", func(c Context) error {
c.Set("e", 5) c.Set("e", 5)
return nil return nil
}, e) })
r.Add(GET, "/users/newsee", func(c Context) error { r.Add(GET, "/users/newsee", func(c Context) error {
c.Set("f", 6) c.Set("f", 6)
return nil return nil
}, e) })
r.Add(GET, "/users/*", func(c Context) error { r.Add(GET, "/users/*", func(c Context) error {
c.Set("g", 7) c.Set("g", 7)
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
// Route > /users // Route > /users
@ -496,11 +496,11 @@ func TestRouterPriorityNotFound(t *testing.T) {
r.Add(GET, "/a/foo", func(c Context) error { r.Add(GET, "/a/foo", func(c Context) error {
c.Set("a", 1) c.Set("a", 1)
return nil return nil
}, e) })
r.Add(GET, "/a/bar", func(c Context) error { r.Add(GET, "/a/bar", func(c Context) error {
c.Set("b", 2) c.Set("b", 2)
return nil return nil
}, e) })
// Find // Find
r.Find(GET, "/a/foo", c) r.Find(GET, "/a/foo", c)
@ -525,13 +525,13 @@ func TestRouterParamNames(t *testing.T) {
r.Add(GET, "/users", func(c Context) error { r.Add(GET, "/users", func(c Context) error {
c.Set("path", "/users") c.Set("path", "/users")
return nil return nil
}, e) })
r.Add(GET, "/users/:id", func(c Context) error { r.Add(GET, "/users/:id", func(c Context) error {
return nil return nil
}, e) })
r.Add(GET, "/users/:uid/files/:fid", func(c Context) error { r.Add(GET, "/users/:uid/files/:fid", func(c Context) error {
return nil return nil
}, e) })
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
// Route > /users // Route > /users
@ -561,15 +561,15 @@ func TestRouterStaticDynamicConflict(t *testing.T) {
r.Add(GET, "/dictionary/skills", func(c Context) error { r.Add(GET, "/dictionary/skills", func(c Context) error {
c.Set("a", 1) c.Set("a", 1)
return nil return nil
}, e) })
r.Add(GET, "/dictionary/:name", func(c Context) error { r.Add(GET, "/dictionary/:name", func(c Context) error {
c.Set("b", 2) c.Set("b", 2)
return nil return nil
}, e) })
r.Add(GET, "/server", func(c Context) error { r.Add(GET, "/server", func(c Context) error {
c.Set("c", 3) c.Set("c", 3)
return nil return nil
}, e) })
r.Find(GET, "/dictionary/skills", c) r.Find(GET, "/dictionary/skills", c)
c.Handler()(c) c.Handler()(c)
@ -591,7 +591,7 @@ func TestRouterAPI(t *testing.T) {
for _, route := range api { for _, route := range api {
r.Add(route.Method, route.Path, func(c Context) error { r.Add(route.Method, route.Path, func(c Context) error {
return nil return nil
}, e) })
} }
c := e.NewContext(nil, nil).(*context) c := e.NewContext(nil, nil).(*context)
for _, route := range api { for _, route := range api {
@ -613,7 +613,7 @@ func BenchmarkRouterGitHubAPI(b *testing.B) {
for _, route := range api { for _, route := range api {
r.Add(route.Method, route.Path, func(c Context) error { r.Add(route.Method, route.Path, func(c Context) error {
return nil return nil
}, e) })
} }
// Find routes // Find routes