mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
Fixed catch all param
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
4e69d8d4f5
commit
0a075ce7c5
@ -13,7 +13,6 @@ type (
|
||||
Response *response
|
||||
pnames []string
|
||||
pvalues []string
|
||||
pn int // Param count
|
||||
store store
|
||||
echo *Echo
|
||||
}
|
||||
@ -22,7 +21,7 @@ type (
|
||||
|
||||
// P returns path parameter by index.
|
||||
func (c *Context) P(i int) (value string) {
|
||||
if i <= c.pn {
|
||||
if i <= len(c.pnames) {
|
||||
value = c.pvalues[i]
|
||||
}
|
||||
return
|
||||
@ -30,8 +29,9 @@ func (c *Context) P(i int) (value string) {
|
||||
|
||||
// Param returns path parameter by name.
|
||||
func (c *Context) Param(name string) (value string) {
|
||||
l := len(c.pnames)
|
||||
for i, n := range c.pnames {
|
||||
if n == name && i <= c.pn {
|
||||
if n == name && i <= l {
|
||||
value = c.pvalues[i]
|
||||
break
|
||||
}
|
||||
|
15
router.go
15
router.go
@ -64,8 +64,10 @@ func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
|
||||
}
|
||||
r.insert(method, path[:i], nil, ptype, pnames, echo)
|
||||
} else if path[i] == '*' {
|
||||
r.insert(method, path[:i], nil, ctype, nil, echo)
|
||||
r.insert(method, path[:l], h, ctype, nil, echo)
|
||||
r.insert(method, path[:i], nil, stype, nil, echo)
|
||||
pnames = append(pnames, "_name")
|
||||
r.insert(method, path[:l], h, ctype, pnames, echo)
|
||||
return
|
||||
}
|
||||
}
|
||||
r.insert(method, path, h, stype, nil, echo)
|
||||
@ -203,7 +205,7 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
|
||||
cn := r.trees[method] // Current node as root
|
||||
search := path
|
||||
chn := new(node) // Child node
|
||||
c.pn = 0 // Param count
|
||||
n := 0 // Param counter
|
||||
|
||||
// Search order static > param > catch-all
|
||||
for {
|
||||
@ -239,8 +241,8 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
|
||||
i, l := 0, len(search)
|
||||
for ; i < l && search[i] != '/'; i++ {
|
||||
}
|
||||
c.pvalues[c.pn] = search[:i]
|
||||
c.pn++
|
||||
c.pvalues[n] = search[:i]
|
||||
n++
|
||||
search = search[i:]
|
||||
continue
|
||||
}
|
||||
@ -249,8 +251,7 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
|
||||
chn = cn.findCchild()
|
||||
if chn != nil {
|
||||
cn = chn
|
||||
c.pnames[c.pn] = "_name"
|
||||
c.pvalues[c.pn] = search
|
||||
c.pvalues[n] = search
|
||||
search = "" // End search
|
||||
continue
|
||||
}
|
||||
|
@ -340,15 +340,16 @@ func TestRouterTwoParam(t *testing.T) {
|
||||
|
||||
func TestRouterCatchAll(t *testing.T) {
|
||||
r := New().Router
|
||||
r.Add(GET, "/static/*", func(*Context) error {
|
||||
r.Add(GET, "/users/*", func(*Context) error {
|
||||
return nil
|
||||
}, nil)
|
||||
h, _ := r.Find(GET, "/static/echo.gif", context)
|
||||
|
||||
h, _ := r.Find(GET, "/users/joe", context)
|
||||
if h == nil {
|
||||
t.Fatal("handler not found")
|
||||
}
|
||||
if context.pvalues[0] != "echo.gif" {
|
||||
t.Error("value should be echo.gif")
|
||||
if context.pvalues[0] != "joe" {
|
||||
t.Error("value should be joe")
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user