Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Fluent Method Chaining for Status and Type Methods Using Generics #3221 #3296

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
198 changes: 109 additions & 89 deletions app.go

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
// generation tool `go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c`
// https://github.com/vburenin/ifacemaker/blob/975a95966976eeb2d4365a7fb236e274c54da64c/ifacemaker.go#L14-L30
//
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface Ctx --pkg fiber --output ctx_interface_gen.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface CtxGeneric --pkg fiber --output ctx_interface.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
//go:generate go run ctx_interface_gen.go
type DefaultCtx struct {

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / govulncheck-check

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

DefaultCtx refers to

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

invalid recursive type DefaultCtx

Check failure on line 52 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

DefaultCtx refers to
app *App // Reference to *App
route *Route // Reference to *Route
app *App[*DefaultCtx] // Reference to *App
route *Route[*DefaultCtx] // Reference to *Route
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
bind *Bind // Default bind reference
redirect *Redirect // Default redirect reference
Expand All @@ -71,6 +72,8 @@
matched bool // Non use route matched
}

type Ctx = CtxGeneric[*DefaultCtx]

Check failure on line 75 in ctx.go

View workflow job for this annotation

GitHub Actions / govulncheck-check

Ctx refers to

Check failure on line 75 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

Ctx refers to

Check failure on line 75 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

Ctx refers to

Check failure on line 75 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

Ctx refers to

Check failure on line 75 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

Ctx refers to

Check failure on line 75 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

Ctx refers to

// SendFile defines configuration options when to transfer file with SendFile.
type SendFile struct {
// FS is the file system to serve the static files from.
Expand Down Expand Up @@ -196,7 +199,7 @@
}

// ResFmt associates a Content Type to a fiber.Handler for c.Format
type ResFmt struct {

Check failure on line 202 in ctx.go

View workflow job for this annotation

GitHub Actions / govulncheck-check

ResFmt refers to

Check failure on line 202 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, ubuntu-latest)

ResFmt refers to

Check failure on line 202 in ctx.go

View workflow job for this annotation

GitHub Actions / unit (1.23.x, macos-latest)

ResFmt refers to

Check failure on line 202 in ctx.go

View workflow job for this annotation

GitHub Actions / lint

ResFmt refers to

Check failure on line 202 in ctx.go

View workflow job for this annotation

GitHub Actions / repeated

ResFmt refers to

Check failure on line 202 in ctx.go

View workflow job for this annotation

GitHub Actions / Compare

ResFmt refers to
Handler func(Ctx) error
MediaType string
}
Expand All @@ -222,7 +225,7 @@
}

// App returns the *App reference to the instance of the Fiber application
func (c *DefaultCtx) App() *App {
func (c *DefaultCtx) App() *App[*DefaultCtx] {
return c.app
}

Expand Down Expand Up @@ -1045,6 +1048,7 @@
}

// Continue handler stack
// TODO: reduce this with generics
if c.app.newCtxFunc != nil {
_, err := c.app.nextCustom(c)
return err
Expand All @@ -1060,6 +1064,7 @@
var err error

c.indexRoute = -1
// TODO: reduce this with generics
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
Expand Down Expand Up @@ -1730,7 +1735,7 @@

// Status sets the HTTP status for the response.
// This method is chainable.
func (c *DefaultCtx) Status(status int) Ctx {
func (c *DefaultCtx) Status(status int) *DefaultCtx {
c.fasthttp.Response.SetStatusCode(status)
return c
}
Expand Down Expand Up @@ -1775,7 +1780,7 @@
}

// Type sets the Content-Type HTTP header to the MIME type specified by the file extension.
func (c *DefaultCtx) Type(extension string, charset ...string) Ctx {
func (c *DefaultCtx) Type(extension string, charset ...string) *DefaultCtx {
if len(charset) > 0 {
c.fasthttp.Response.Header.SetContentType(utils.GetMIME(extension) + "; charset=" + charset[0])
} else {
Expand Down Expand Up @@ -1975,7 +1980,7 @@
c.matched = matched
}

func (c *DefaultCtx) setRoute(route *Route) {
func (c *DefaultCtx) setRoute(route *Route[*DefaultCtx]) {
c.route = route
}

Expand Down
70 changes: 70 additions & 0 deletions ctx_custom_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io

package fiber

import (
"errors"

"github.com/valyala/fasthttp"
)

type CustomCtx[T any] interface {
CtxGeneric[T]

// Reset is a method to reset context fields by given request when to use server handlers.
Reset(fctx *fasthttp.RequestCtx)

// Methods to use with next stack.
getMethodINT() int
getIndexRoute() int
getTreePath() string
getDetectionPath() string
getPathOriginal() string
getValues() *[maxParams]string
getMatched() bool
setIndexHandler(handler int)
setIndexRoute(route int)
setMatched(matched bool)
setRoute(route *Route[T])
}

func NewDefaultCtx[TCtx *DefaultCtx](app *App[*DefaultCtx]) TCtx {
// return ctx
return &DefaultCtx{
// Set app reference
app: app,
}
}

func (app *App[TCtx]) newCtx() CtxGeneric[TCtx] {
var c CtxGeneric[TCtx]

// TODO: fix this with generics ?
if app.newCtxFunc != nil {
c = app.newCtxFunc(app)
} else {
c = NewDefaultCtx(app)
}

return c
}

// AcquireCtx retrieves a new Ctx from the pool.
func (app *App[TCtx]) AcquireCtx(fctx *fasthttp.RequestCtx) TCtx {
ctx, ok := app.pool.Get().(TCtx)

if !ok {
panic(errors.New("failed to type-assert to Ctx"))
}
ctx.Reset(fctx)

return ctx
}

// ReleaseCtx releases the ctx back into the pool.
func (app *App[TCtx]) ReleaseCtx(c TCtx) {
c.release()
app.pool.Put(c)
}
Loading
Loading