Skip to content

Commit

Permalink
Fixed catch all param
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Apr 26, 2015
1 parent 4e69d8d commit 0a075ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type (
Response *response
pnames []string
pvalues []string
pn int // Param count
store store
echo *Echo
}
Expand All @@ -22,16 +21,17 @@ 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
}

// 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
}
Expand Down
15 changes: 8 additions & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down

0 comments on commit 0a075ce

Please sign in to comment.