Skip to content

Commit 99f95b2

Browse files
FennyReneWerner87
andauthored
v1.9.6 (#360)
**🚀 Fiber `v1.9.6`** Special thanks to @renanbastos93 & @ReneWerner87 for optimizing the current router. Help use translate our API documentation by [clicking here](https://crowdin.com/project/gofiber) 🔥 New - `AcquireCtx` / `ReleaseCtx` The Ctx pool is now accessible for third-party packages - Fiber docs merged [Russian](https://docs.gofiber.io/v/ru/) translations **84%** - Fiber docs merged [Spanish](https://docs.gofiber.io/v/es/) translations **65%** - Fiber docs merged [French](https://docs.gofiber.io/v/fr/) translations **40%** - Fiber docs merged [German](https://docs.gofiber.io/v/de/) translations **32%** - Fiber docs merged [Portuguese](https://docs.gofiber.io/v/pt/) translations **24%** 🩹 Fixes - Hotfix for interpolated params in nested routes #354 - Some `Ctx` methods didn't work correctly when called without an `*App` pointer. - `ctx.Vary` sometimes added duplicates to the response header - Improved router by ditching regexp and increased performance by **817%** without allocations. ```go // Tested with 350 github API routes Benchmark_Router_OLD-4 614 2467460 ns/op 68902 B/op 600 allocs/op Benchmark_Router_NEW-4 3429 302033 ns/op 0 B/op 0 allocs/op ``` 🧹 Updates - Add context benchmarks - Remove some unnecessary functions from `utils` - Add router & param test cases - Add new coffee supporters to readme - Add third party middlewares to readme - Add more comments to source code - Cleanup some old helper functions 🧬 Middleware - [gofiber/adaptor](https://github.com/gofiber/adaptor) `v0.0.1` Converter for net/http handlers to/from Fiber handlers - [gofiber/session](https://github.com/gofiber/session) `v1.0.0` big improvements and support for storage providers - [gofiber/logger](https://github.com/gofiber/logger) `v0.0.6` supports `${error}` param - [gofiber/embed](https://github.com/gofiber/embed) `v0.0.9` minor improvements and support for directory browsing Co-authored-by: ReneWerner87 <[email protected]>
1 parent 6e58bfc commit 99f95b2

File tree

5 files changed

+683
-659
lines changed

5 files changed

+683
-659
lines changed

app_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ func testStatus200(t *testing.T, app *App, url string, method string) {
2020
assertEqual(t, 200, resp.StatusCode, "Status code")
2121
}
2222

23+
func Test_Nested_Params(t *testing.T) {
24+
app := New()
25+
26+
app.Get("/test", func(c *Ctx) {
27+
t.Log(c.Route().Path)
28+
c.Status(400).Send("Should move on")
29+
})
30+
app.Get("/test/:param", func(c *Ctx) {
31+
t.Log(c.Route().Path)
32+
c.Status(400).Send("Should move on")
33+
})
34+
app.Get("/test/:param/test", func(c *Ctx) {
35+
t.Log(c.Route().Path)
36+
c.Status(400).Send("Should move on")
37+
})
38+
app.Get("/test/:param/test/:param2", func(c *Ctx) {
39+
t.Log(c.Route().Path)
40+
c.Status(200).Send("Good job")
41+
})
42+
43+
req := httptest.NewRequest("GET", "/test/john/test/doe", nil)
44+
resp, err := app.Test(req)
45+
46+
assertEqual(t, nil, err, "app.Test(req)")
47+
assertEqual(t, 200, resp.StatusCode, "Status code")
48+
}
49+
2350
func Test_Raw(t *testing.T) {
2451
app := New()
2552
app.Get("/", func(c *Ctx) {
@@ -65,9 +92,6 @@ func Test_Methods(t *testing.T) {
6592
app.Connect("/:john?/:doe?", dummyHandler)
6693
testStatus200(t, app, "/john/doe", "CONNECT")
6794

68-
app.Connect("/:john?/:doe?", dummyHandler)
69-
testStatus200(t, app, "/john/doe", "CONNECT")
70-
7195
app.Put("/:john?/:doe?", dummyHandler)
7296
testStatus200(t, app, "/john/doe", "CONNECT")
7397

params.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
// paramsParser holds the path segments and param names
1515
type parsedParams struct {
16-
Segs []paramSeg
17-
Keys []string
16+
Segs []paramSeg
17+
Params []string
1818
}
1919

2020
// paramsSeg holds the segment metadata
@@ -28,11 +28,10 @@ type paramSeg struct {
2828

2929
var paramsDummy = make([]string, 100, 100)
3030

31+
const wildcardParam string = "*"
32+
3133
// New ...
3234
func parseParams(pattern string) (p parsedParams) {
33-
if pattern[0] != '/' {
34-
pattern = "/" + pattern
35-
}
3635
var patternCount int
3736
aPattern := []string{""}
3837
if pattern != "" {
@@ -53,7 +52,7 @@ func parseParams(pattern string) (p parsedParams) {
5352
out[segIndex] = paramSeg{
5453
Param: paramTrimmer(aPattern[i]),
5554
IsParam: true,
56-
IsOptional: aPattern[i] == "*" || aPattern[i][partLen-1] == '?',
55+
IsOptional: aPattern[i] == wildcardParam || aPattern[i][partLen-1] == '?',
5756
}
5857
params = append(params, out[segIndex].Param)
5958
} else {
@@ -75,14 +74,14 @@ func parseParams(pattern string) (p parsedParams) {
7574
}
7675
out[segIndex-1].IsLast = true
7776

78-
p = parsedParams{Segs: out[:segIndex:segIndex], Keys: params}
79-
// fmt.Printf("%+v\n", p)
77+
p = parsedParams{Segs: out[:segIndex:segIndex], Params: params}
78+
//fmt.Printf("%+v\n", p)
8079
return
8180
}
8281

8382
// Match ...
8483
func (p *parsedParams) matchParams(s string) ([]string, bool) {
85-
lenKeys := len(p.Keys)
84+
lenKeys := len(p.Params)
8685
params := paramsDummy[0:lenKeys:lenKeys]
8786
var i, j, paramsIterator, partLen int
8887
if len(s) > 0 {
@@ -93,11 +92,13 @@ func (p *parsedParams) matchParams(s string) ([]string, bool) {
9392
// check parameter
9493
if segment.IsParam {
9594
// determine parameter length
96-
if segment.IsLast {
97-
i = partLen
98-
} else if segment.Param == "*" {
99-
// for the expressjs behavior -> "/api/*/:param" - "/api/joker/batman/robin/1" -> "joker/batman/robin", "1"
100-
i = findCharPos(s, '/', strings.Count(s, "/")-(len(p.Segs)-(index+1))+1)
95+
if segment.Param == wildcardParam {
96+
if segment.IsLast {
97+
i = partLen
98+
} else {
99+
// for the expressjs behavior -> "/api/*/:param" - "/api/joker/batman/robin/1" -> "joker/batman/robin", "1"
100+
i = findCharPos(s, '/', strings.Count(s, "/")-(len(p.Segs)-(index+1))+1)
101+
}
101102
} else {
102103
i = strings.IndexByte(s, '/')
103104
}
@@ -129,10 +130,12 @@ func (p *parsedParams) matchParams(s string) ([]string, bool) {
129130
s = s[j:]
130131
}
131132
}
133+
if len(s) != 0 {
134+
return nil, false
135+
}
132136

133137
return params, true
134138
}
135-
136139
func paramTrimmer(param string) string {
137140
start := 0
138141
end := len(param)

router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (app *App) registerMethod(method, path string, handlers ...func(*Ctx)) {
168168

169169
Path: path,
170170
Method: method,
171-
Params: isParsed.Keys,
171+
Params: isParsed.Params,
172172
Handler: handlers[i],
173173
}
174174
if method == "*" {

0 commit comments

Comments
 (0)