mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
Implement Fluent Method Chaining for Status and Type Methods Using Generics #3221
This commit is contained in:
parent
c3a532c4fa
commit
b7be67fee5
4
app.go
4
app.go
@ -101,7 +101,7 @@ type App[TCtx CtxGeneric[TCtx]] struct {
|
||||
// Latest route & group
|
||||
latestRoute *Route[TCtx]
|
||||
// newCtxFunc
|
||||
newCtxFunc func(app *App[TCtx]) CustomCtx[TCtx]
|
||||
newCtxFunc func(app *App[TCtx]) TCtx
|
||||
// TLS handler
|
||||
tlsHandler *TLSHandler
|
||||
// Mount fields
|
||||
@ -529,7 +529,7 @@ func New(config ...Config[*DefaultCtx]) *App[*DefaultCtx] {
|
||||
// Prefork: true,
|
||||
// ServerHeader: "Fiber",
|
||||
// })
|
||||
func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc func(app *App[TCtx]) CustomCtx[TCtx], config ...Config[TCtx]) *App[TCtx] {
|
||||
func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc func(app *App[TCtx]) TCtx, config ...Config[TCtx]) *App[TCtx] {
|
||||
app := newApp[TCtx](config...)
|
||||
|
||||
// Set newCtxFunc
|
||||
|
13
ctx.go
13
ctx.go
@ -1048,12 +1048,6 @@ func (c *DefaultCtx) Next() error {
|
||||
}
|
||||
|
||||
// Continue handler stack
|
||||
// TODO: reduce this with generics
|
||||
if c.app.newCtxFunc != nil {
|
||||
_, err := c.app.nextCustom(c)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := c.app.next(c)
|
||||
return err
|
||||
}
|
||||
@ -1064,12 +1058,7 @@ func (c *DefaultCtx) RestartRouting() error {
|
||||
var err error
|
||||
|
||||
c.indexRoute = -1
|
||||
// TODO: reduce this with generics
|
||||
if c.app.newCtxFunc != nil {
|
||||
_, err = c.app.nextCustom(c)
|
||||
} else {
|
||||
_, err = c.app.next(c)
|
||||
}
|
||||
_, err = c.app.next(c)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -17,9 +17,9 @@ type CustomCtx[T any] interface {
|
||||
Reset(fctx *fasthttp.RequestCtx)
|
||||
|
||||
// Methods to use with next stack.
|
||||
getMethodINT() int
|
||||
getMethodInt() int
|
||||
getIndexRoute() int
|
||||
getTreePath() string
|
||||
getTreePathHash() int
|
||||
getDetectionPath() string
|
||||
getPathOriginal() string
|
||||
getValues() *[maxParams]string
|
||||
@ -42,14 +42,14 @@ func NewDefaultCtx[TCtx *DefaultCtx](app *App[*DefaultCtx]) TCtx {
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (app *App[TCtx]) newCtx() CtxGeneric[TCtx] {
|
||||
var c CtxGeneric[TCtx]
|
||||
func (app *App[TCtx]) newCtx() TCtx {
|
||||
var c TCtx
|
||||
|
||||
// TODO: fix this with generics ?
|
||||
if app.newCtxFunc != nil {
|
||||
c = app.newCtxFunc(app)
|
||||
} else {
|
||||
c = NewDefaultCtx(app)
|
||||
c = any(NewDefaultCtx[*DefaultCtx](app)).(TCtx)
|
||||
}
|
||||
|
||||
return c
|
||||
|
13
ctx_test.go
13
ctx_test.go
@ -109,9 +109,7 @@ func (c *customCtx) Params(key string, defaultValue ...string) string { //revive
|
||||
func Test_Ctx_CustomCtx(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := New()
|
||||
|
||||
app.NewCtxFunc(func(app *App[TCtx]) CustomCtx {
|
||||
app := NewWithCustomCtx(func(app *App[*DefaultCtx]) *customCtx {
|
||||
return &customCtx{
|
||||
DefaultCtx: *NewDefaultCtx(app),
|
||||
}
|
||||
@ -133,15 +131,12 @@ func Test_Ctx_CustomCtx_and_Method(t *testing.T) {
|
||||
|
||||
// Create app with custom request methods
|
||||
methods := append(DefaultMethods, "JOHN") //nolint:gocritic // We want a new slice here
|
||||
app := New(Config{
|
||||
RequestMethods: methods,
|
||||
})
|
||||
|
||||
// Create custom context
|
||||
app.NewCtxFunc(func(app *App[TCtx]) CustomCtx {
|
||||
app := NewWithCustomCtx(func(app *App[*DefaultCtx]) *customCtx {
|
||||
return &customCtx{
|
||||
DefaultCtx: *NewDefaultCtx(app),
|
||||
}
|
||||
}, Config{
|
||||
RequestMethods: methods,
|
||||
})
|
||||
|
||||
// Add route with custom method
|
||||
|
Loading…
x
Reference in New Issue
Block a user