🔨 Make Params available in Route

pull/548/head
Fenny 2020-07-05 11:17:42 +02:00
parent 104f4fcddf
commit 964f3c59ae
4 changed files with 10 additions and 67 deletions

15
ctx.go
View File

@ -635,11 +635,11 @@ func (ctx *Ctx) OriginalURL() string {
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting to use the value outside the Handler.
func (ctx *Ctx) Params(key string, defaultValue ...string) string {
for i := range ctx.route.routeParams {
if len(key) != len(ctx.route.routeParams[i]) {
for i := range ctx.route.Params {
if len(key) != len(ctx.route.Params[i]) {
continue
}
if ctx.route.routeParams[i] == key {
if ctx.route.Params[i] == key {
// in case values are not here
if len(ctx.values) <= i || len(ctx.values[i]) == 0 {
break
@ -650,15 +650,6 @@ func (ctx *Ctx) Params(key string, defaultValue ...string) string {
return defaultString("", defaultValue)
}
// ParamList returns a list of all the parameter names for the current context
func (ctx *Ctx) ParamList() []string {
paramList := make([]string, len(ctx.route.routeParams))
copy(paramList, ctx.route.routeParams)
return paramList
}
// Path returns the path part of the request URL.
// Optionally, you could override the path.
func (ctx *Ctx) Path(override ...string) string {

View File

@ -670,7 +670,7 @@ func Benchmark_Ctx_Params(b *testing.B) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.route = &Route{
routeParams: []string{
Params: []string{
"param1", "param2", "param3", "param4",
},
}
@ -689,54 +689,6 @@ func Benchmark_Ctx_Params(b *testing.B) {
utils.AssertEqual(b, "awesome", res)
}
// go test -race run Test_Ctx_ParamList
func Test_Ctx_ParamList(t *testing.T) {
t.Parallel()
app := New()
app.Get("/test/:user", func(c *Ctx) {
utils.AssertEqual(t, []string{"user"}, c.ParamList())
})
app.Get("/test2/*", func(c *Ctx) {
utils.AssertEqual(t, []string{"*"}, c.ParamList())
})
app.Get("/test3/:optional?", func(c *Ctx) {
utils.AssertEqual(t, []string{"optional"}, c.ParamList())
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/im/a/cookie", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test3", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
}
// go test -v -run=^$ -bench=Benchmark_Ctx_ParamList -benchmem -count=4
func Benchmark_Ctx_ParamList(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
routeParams := []string{
"param1", "param2", "param3", "param4",
}
c.route = &Route{
routeParams: routeParams,
}
c.values = []string{
"john", "doe", "is", "awesome",
}
var res []string
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
res = c.ParamList()
}
utils.AssertEqual(b, routeParams, res)
}
// go test -run Test_Ctx_Path
func Test_Ctx_Path(t *testing.T) {
t.Parallel()

View File

@ -43,11 +43,11 @@ type Route struct {
root bool // Path equals '/'
path string // Prettified path
routeParser routeParser // Parameter parser
routeParams []string // Case sensitive param keys
// Public fields
Method string `json:"method"` // HTTP method
Path string `json:"path"` // Original registered route path
Params []string `json:"params"` // Case sensitive param keys
Name string `json:"name"` // Name of first handler used in route
Handlers []Handler `json:"-"` // Ctx handlers
}
@ -63,7 +63,7 @@ func (r *Route) match(path, original string) (match bool, values []string) {
return true, values
}
// Does this route have parameters
if len(r.routeParams) > 0 {
if len(r.Params) > 0 {
// Match params
if paramPos, match := r.routeParser.getMatch(path, r.use); match {
// Get params from the original path
@ -186,7 +186,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) *Route {
// Path data
path: pathPretty,
routeParser: parsedPretty,
routeParams: parsedRaw.params,
Params: parsedRaw.params,
// Public data
Path: pathRaw,

View File

@ -348,7 +348,7 @@ func Benchmark_Route_Match(b *testing.B) {
root: false,
star: false,
routeParser: parsed,
routeParams: parsed.params,
Params: parsed.params,
path: "/user/keys/:id",
Path: "/user/keys/:id",
@ -374,7 +374,7 @@ func Benchmark_Route_Match_Star(b *testing.B) {
root: false,
star: true,
routeParser: parsed,
routeParams: parsed.params,
Params: parsed.params,
path: "/user/keys/bla",
Path: "/user/keys/bla",
@ -401,7 +401,7 @@ func Benchmark_Route_Match_Root(b *testing.B) {
star: false,
path: "/",
routeParser: parsed,
routeParams: parsed.params,
Params: parsed.params,
Path: "/",
Method: "DELETE",