Skip to content

Commit

Permalink
removed Context#P()
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Oct 11, 2016
1 parent 15eb5b0 commit 8623669
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 32 deletions.
11 changes: 0 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
2 changes: 1 addition & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("_*")))
})
}

Expand Down
2 changes: 1 addition & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("_*")))
})
}

Expand Down
2 changes: 1 addition & 1 deletion middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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])
}
}
}
Expand Down

0 comments on commit 8623669

Please sign in to comment.