mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
removed Context#P()
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
15eb5b0aad
commit
8623669cbc
11
context.go
11
context.go
@ -55,9 +55,6 @@ type (
|
||||
// SetPath sets the registered path for the handler.
|
||||
SetPath(string)
|
||||
|
||||
// P returns path parameter by index.
|
||||
P(int) string
|
||||
|
||||
// Param returns path parameter by name.
|
||||
Param(string) string
|
||||
|
||||
@ -266,14 +263,6 @@ func (c *context) SetPath(p string) {
|
||||
c.path = p
|
||||
}
|
||||
|
||||
func (c *context) P(i int) (value string) {
|
||||
l := len(c.pnames)
|
||||
if i < l {
|
||||
value = c.pvalues[i]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *context) Param(name string) (value string) {
|
||||
l := len(c.pnames)
|
||||
for i, n := range c.pnames {
|
||||
|
@ -253,9 +253,6 @@ func TestContextPathParam(t *testing.T) {
|
||||
c.SetParamValues("101", "501")
|
||||
assert.EqualValues(t, []string{"101", "501"}, c.ParamValues())
|
||||
|
||||
// P
|
||||
assert.Equal(t, "101", c.P(0))
|
||||
|
||||
// Param
|
||||
assert.Equal(t, "501", c.Param("fid"))
|
||||
}
|
||||
|
2
echo.go
2
echo.go
@ -382,7 +382,7 @@ func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middlew
|
||||
// provided root directory.
|
||||
func (e *Echo) Static(prefix, root string) {
|
||||
e.GET(prefix+"*", func(c Context) error {
|
||||
return c.File(path.Join(root, c.P(0)))
|
||||
return c.File(path.Join(root, c.Param("_*")))
|
||||
})
|
||||
}
|
||||
|
||||
|
2
group.go
2
group.go
@ -138,7 +138,7 @@ func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group {
|
||||
// Static implements `Echo#Static()` for sub-routes within the Group.
|
||||
func (g *Group) Static(prefix, root string) {
|
||||
g.GET(g.prefix+prefix+"*", func(c Context) error {
|
||||
return c.File(path.Join(root, c.P(0)))
|
||||
return c.File(path.Join(root, c.Param("_*")))
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
||||
fs := http.Dir(config.Root)
|
||||
p := c.Request().URL.Path
|
||||
if strings.Contains(c.Path(), "*") { // If serving from a group, e.g. `/static*`.
|
||||
p = c.P(0)
|
||||
p = c.Param("_*")
|
||||
}
|
||||
file := path.Clean(p)
|
||||
f, err := fs.Open(file)
|
||||
|
@ -295,7 +295,7 @@ func TestRouterParam(t *testing.T) {
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(GET, "/users/1", c)
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
assert.Equal(t, "1", c.Param("id"))
|
||||
}
|
||||
|
||||
func TestRouterTwoParam(t *testing.T) {
|
||||
@ -307,8 +307,8 @@ func TestRouterTwoParam(t *testing.T) {
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
r.Find(GET, "/users/1/files/1", c)
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
assert.Equal(t, "1", c.P(1))
|
||||
assert.Equal(t, "1", c.Param("uid"))
|
||||
assert.Equal(t, "1", c.Param("fid"))
|
||||
}
|
||||
|
||||
// Issue #378
|
||||
@ -347,13 +347,13 @@ func TestRouterMatchAny(t *testing.T) {
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
|
||||
r.Find(GET, "/", c)
|
||||
assert.Equal(t, "", c.P(0))
|
||||
assert.Equal(t, "", c.Param("_*"))
|
||||
|
||||
r.Find(GET, "/download", c)
|
||||
assert.Equal(t, "download", c.P(0))
|
||||
assert.Equal(t, "download", c.Param("_*"))
|
||||
|
||||
r.Find(GET, "/users/joe", c)
|
||||
assert.Equal(t, "joe", c.P(0))
|
||||
assert.Equal(t, "joe", c.Param("_*"))
|
||||
}
|
||||
|
||||
func TestRouterMicroParam(t *testing.T) {
|
||||
@ -364,9 +364,9 @@ func TestRouterMicroParam(t *testing.T) {
|
||||
}, e)
|
||||
c := e.NewContext(nil, nil).(*context)
|
||||
r.Find(GET, "/1/2/3", c)
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
assert.Equal(t, "2", c.P(1))
|
||||
assert.Equal(t, "3", c.P(2))
|
||||
assert.Equal(t, "1", c.Param("a"))
|
||||
assert.Equal(t, "2", c.Param("b"))
|
||||
assert.Equal(t, "3", c.Param("c"))
|
||||
}
|
||||
|
||||
func TestRouterMixParamMatchAny(t *testing.T) {
|
||||
@ -381,7 +381,7 @@ func TestRouterMixParamMatchAny(t *testing.T) {
|
||||
|
||||
r.Find(GET, "/users/joe/comments", c)
|
||||
c.handler(c)
|
||||
assert.Equal(t, "joe", c.P(0))
|
||||
assert.Equal(t, "joe", c.Param("id"))
|
||||
}
|
||||
|
||||
func TestRouterMultiRoute(t *testing.T) {
|
||||
@ -405,7 +405,7 @@ func TestRouterMultiRoute(t *testing.T) {
|
||||
|
||||
// Route > /users/:id
|
||||
r.Find(GET, "/users/1", c)
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
assert.Equal(t, "1", c.Param("id"))
|
||||
|
||||
// Route > /user
|
||||
c = e.NewContext(nil, nil).(*context)
|
||||
@ -542,14 +542,14 @@ func TestRouterParamNames(t *testing.T) {
|
||||
// Route > /users/:id
|
||||
r.Find(GET, "/users/1", c)
|
||||
assert.Equal(t, "id", c.pnames[0])
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
assert.Equal(t, "1", c.Param("id"))
|
||||
|
||||
// Route > /users/:uid/files/:fid
|
||||
r.Find(GET, "/users/1/files/1", c)
|
||||
assert.Equal(t, "uid", c.pnames[0])
|
||||
assert.Equal(t, "1", c.P(0))
|
||||
assert.Equal(t, "1", c.Param("uid"))
|
||||
assert.Equal(t, "fid", c.pnames[1])
|
||||
assert.Equal(t, "1", c.P(1))
|
||||
assert.Equal(t, "1", c.Param("fid"))
|
||||
}
|
||||
|
||||
func TestRouterAPI(t *testing.T) {
|
||||
@ -566,7 +566,7 @@ func TestRouterAPI(t *testing.T) {
|
||||
r.Find(route.Method, route.Path, c)
|
||||
for i, n := range c.pnames {
|
||||
if assert.NotEmpty(t, n) {
|
||||
assert.Equal(t, ":"+n, c.P(i))
|
||||
assert.Equal(t, n, c.pnames[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user