Skip to content

Commit

Permalink
v1.9.6 (#360)
Browse files Browse the repository at this point in the history
**🚀 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 <renewerner87@googlemail.com>
  • Loading branch information
Fenny and ReneWerner87 committed May 11, 2020
1 parent 6e58bfc commit 99f95b2
Show file tree
Hide file tree
Showing 5 changed files with 683 additions and 659 deletions.
30 changes: 27 additions & 3 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ func testStatus200(t *testing.T, app *App, url string, method string) {
assertEqual(t, 200, resp.StatusCode, "Status code")
}

func Test_Nested_Params(t *testing.T) {
app := New()

app.Get("/test", func(c *Ctx) {
t.Log(c.Route().Path)
c.Status(400).Send("Should move on")
})
app.Get("/test/:param", func(c *Ctx) {
t.Log(c.Route().Path)
c.Status(400).Send("Should move on")
})
app.Get("/test/:param/test", func(c *Ctx) {
t.Log(c.Route().Path)
c.Status(400).Send("Should move on")
})
app.Get("/test/:param/test/:param2", func(c *Ctx) {
t.Log(c.Route().Path)
c.Status(200).Send("Good job")
})

req := httptest.NewRequest("GET", "/test/john/test/doe", nil)
resp, err := app.Test(req)

assertEqual(t, nil, err, "app.Test(req)")
assertEqual(t, 200, resp.StatusCode, "Status code")
}

func Test_Raw(t *testing.T) {
app := New()
app.Get("/", func(c *Ctx) {
Expand Down Expand Up @@ -65,9 +92,6 @@ func Test_Methods(t *testing.T) {
app.Connect("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "CONNECT")

app.Connect("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "CONNECT")

app.Put("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", "CONNECT")

Expand Down
33 changes: 18 additions & 15 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

// paramsParser holds the path segments and param names
type parsedParams struct {
Segs []paramSeg
Keys []string
Segs []paramSeg
Params []string
}

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

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

const wildcardParam string = "*"

// New ...
func parseParams(pattern string) (p parsedParams) {
if pattern[0] != '/' {
pattern = "/" + pattern
}
var patternCount int
aPattern := []string{""}
if pattern != "" {
Expand All @@ -53,7 +52,7 @@ func parseParams(pattern string) (p parsedParams) {
out[segIndex] = paramSeg{
Param: paramTrimmer(aPattern[i]),
IsParam: true,
IsOptional: aPattern[i] == "*" || aPattern[i][partLen-1] == '?',
IsOptional: aPattern[i] == wildcardParam || aPattern[i][partLen-1] == '?',
}
params = append(params, out[segIndex].Param)
} else {
Expand All @@ -75,14 +74,14 @@ func parseParams(pattern string) (p parsedParams) {
}
out[segIndex-1].IsLast = true

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

// Match ...
func (p *parsedParams) matchParams(s string) ([]string, bool) {
lenKeys := len(p.Keys)
lenKeys := len(p.Params)
params := paramsDummy[0:lenKeys:lenKeys]
var i, j, paramsIterator, partLen int
if len(s) > 0 {
Expand All @@ -93,11 +92,13 @@ func (p *parsedParams) matchParams(s string) ([]string, bool) {
// check parameter
if segment.IsParam {
// determine parameter length
if segment.IsLast {
i = partLen
} else if segment.Param == "*" {
// for the expressjs behavior -> "/api/*/:param" - "/api/joker/batman/robin/1" -> "joker/batman/robin", "1"
i = findCharPos(s, '/', strings.Count(s, "/")-(len(p.Segs)-(index+1))+1)
if segment.Param == wildcardParam {
if segment.IsLast {
i = partLen
} else {
// for the expressjs behavior -> "/api/*/:param" - "/api/joker/batman/robin/1" -> "joker/batman/robin", "1"
i = findCharPos(s, '/', strings.Count(s, "/")-(len(p.Segs)-(index+1))+1)
}
} else {
i = strings.IndexByte(s, '/')
}
Expand Down Expand Up @@ -129,10 +130,12 @@ func (p *parsedParams) matchParams(s string) ([]string, bool) {
s = s[j:]
}
}
if len(s) != 0 {
return nil, false
}

return params, true
}

func paramTrimmer(param string) string {
start := 0
end := len(param)
Expand Down
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (app *App) registerMethod(method, path string, handlers ...func(*Ctx)) {

Path: path,
Method: method,
Params: isParsed.Keys,
Params: isParsed.Params,
Handler: handlers[i],
}
if method == "*" {
Expand Down

0 comments on commit 99f95b2

Please sign in to comment.