Skip to content

Commit

Permalink
✨ feature: bind support for render (#1754)
Browse files Browse the repository at this point in the history
* Bind support for Render.

* update

* fix tests

* split Pass-locals-to-views & Bind from Render

* update comments.

* add benchs.

* Update ctx.go

Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>

* Update ctx.go

* optimize

* switch dictpool.

* ✨ feature: bind support for render
- improve performance

* ✨ feature: bind support for render
- improve performance

Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
Co-authored-by: wernerr <rene.werner@verivox.com>
Co-authored-by: wernerr <rene@gofiber.io>
  • Loading branch information
4 people authored Feb 12, 2022
1 parent e3d2586 commit 937713e
Show file tree
Hide file tree
Showing 28 changed files with 1,116 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_ckb.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,4 @@ func main() {
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_de.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
2 changes: 2 additions & 0 deletions .github/README_es.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,5 @@ Copyright (c) 2019-presente [Fenny](https://github.com/fenny) y [contribuyentes]
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)

1 change: 1 addition & 0 deletions .github/README_fa.md
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_he.md
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,5 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
</div>
2 changes: 2 additions & 0 deletions .github/README_id.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,5 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)

1 change: 1 addition & 0 deletions .github/README_it.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,4 @@ Copyright (c) 2019-ora [Fenny](https://github.com/fenny) e [Contributors](https:
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_nl.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,4 @@ O logo oficial foi criado por [Vic Shóstak](https://github.com/koddr) e distrib
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_sa.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_tr.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,4 @@ Telif (c) 2019-günümüz [Fenny](https://github.com/fenny) ve [Contributors](ht
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,4 @@ Copyright (c) 2019-present [Fenny](https://github.com/fenny) and [Contributors](
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 1 addition & 0 deletions .github/README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,4 @@ Fiber 是一個以贊助維生的開源專案,像是: 網域、gitbook、netli
- [gopsutil](https://github.com/shirou/gopsutil/blob/master/LICENSE)
- [go-ole](https://github.com/go-ole/go-ole)
- [wmi](https://github.com/StackExchange/wmi)
- [dictpool](https://github.com/savsgio/dictpool)
1 change: 0 additions & 1 deletion .github/testdata/template.html

This file was deleted.

1 change: 1 addition & 0 deletions .github/testdata/template.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>{{.Title}} {{.Summary}}</h1>
4 changes: 2 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ func Test_App_Static_Prefix(t *testing.T) {

app.Static("/prefix", "./.github/testdata")

req = httptest.NewRequest(MethodGet, "/prefix/template.html", nil)
req = httptest.NewRequest(MethodGet, "/prefix/index.html", nil)
resp, err = app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
Expand Down Expand Up @@ -1146,7 +1146,7 @@ func Test_App_ListenTLS_Prefork(t *testing.T) {
app := New(Config{DisableStartupMessage: true, Prefork: true})

// invalid key file content
utils.AssertEqual(t, false, app.ListenTLS(":0", "./.github/testdata/ssl.pem", "./.github/testdata/template.html") == nil)
utils.AssertEqual(t, false, app.ListenTLS(":0", "./.github/testdata/ssl.pem", "./.github/testdata/template.tmpl") == nil)

utils.AssertEqual(t, nil, app.ListenTLS(":99999", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key"))
}
Expand Down
63 changes: 44 additions & 19 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/gofiber/fiber/v2/internal/bytebufferpool"
"github.com/gofiber/fiber/v2/internal/dictpool"
"github.com/gofiber/fiber/v2/internal/go-json"
"github.com/gofiber/fiber/v2/internal/schema"
"github.com/gofiber/fiber/v2/utils"
Expand Down Expand Up @@ -62,6 +63,7 @@ type Ctx struct {
values [maxParams]string // Route parameter values
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
matched bool // Non use route matched
viewBindMap *dictpool.Dict // Default view map to bind template engine
}

// Range data for c.Range
Expand Down Expand Up @@ -137,6 +139,9 @@ func (app *App) ReleaseCtx(c *Ctx) {
// Reset values
c.route = nil
c.fasthttp = nil
if c.viewBindMap != nil {
dictpool.ReleaseDict(c.viewBindMap)
}
app.pool.Put(c)
}

Expand Down Expand Up @@ -1060,6 +1065,20 @@ func (c *Ctx) Redirect(location string, status ...int) error {
return nil
}

// Add vars to default view var map binding to template engine.
// Variables are read by the Render method and may be overwritten.
func (c *Ctx) Bind(vars Map) error {
// init viewBindMap - lazy map
if c.viewBindMap == nil {
c.viewBindMap = dictpool.AcquireDict()
}
for k, v := range vars {
c.viewBindMap.Set(k, v)
}

return nil
}

// get URL location from route using parameters
func (c *Ctx) getLocationFromRoute(route Route, params Map) (string, error) {
buf := bytebufferpool.Get()
Expand Down Expand Up @@ -1113,25 +1132,8 @@ func (c *Ctx) Render(name string, bind interface{}, layouts ...string) error {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

// Check if the PassLocalsToViews option is enabled (By default it is disabled)
if c.app.config.PassLocalsToViews {
// Safely cast the bind interface to a map
bindMap, ok := bind.(Map)
// Check if the bind is a map
if ok {
// Loop through each local and set it in the map
c.fasthttp.VisitUserValues(func(key []byte, val interface{}) {
// check if bindMap doesn't contain the key
if _, ok := bindMap[string(key)]; !ok {
// Set the key and value in the bindMap
bindMap[string(key)] = val
}
})
// set the original bind to the map
bind = bindMap
}

}
// Pass-locals-to-views & bind
c.renderExtensions(bind)

rendered := false
for prefix, app := range c.app.appList {
Expand Down Expand Up @@ -1179,6 +1181,29 @@ func (c *Ctx) Render(name string, bind interface{}, layouts ...string) error {
return err
}

func (c *Ctx) renderExtensions(bind interface{}) {
if bindMap, ok := bind.(Map); ok {
// Bind view map
if c.viewBindMap != nil {
for _, v := range c.viewBindMap.D {
bindMap[v.Key] = v.Value
}
}

// Check if the PassLocalsToViews option is enabled (by default it is disabled)
if c.app.config.PassLocalsToViews {
// Loop through each local and set it in the map
c.fasthttp.VisitUserValues(func(key []byte, val interface{}) {
// check if bindMap doesn't contain the key
if _, ok := bindMap[utils.UnsafeString(key)]; !ok {
// Set the key and value in the bindMap
bindMap[utils.UnsafeString(key)] = val
}
})
}
}
}

// Route returns the matched Route struct.
func (c *Ctx) Route() *Route {
if c.route == nil {
Expand Down
Loading

0 comments on commit 937713e

Please sign in to comment.