From b7be67fee578babe54f68d2e84d968044d8b477b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Sun, 18 May 2025 21:43:36 +0200 Subject: [PATCH] Implement Fluent Method Chaining for Status and Type Methods Using Generics #3221 --- app.go | 4 ++-- ctx.go | 13 +------------ ctx_custom_interface.go | 10 +++++----- ctx_test.go | 13 ++++--------- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/app.go b/app.go index 93ba996a..f266c663 100644 --- a/app.go +++ b/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 diff --git a/ctx.go b/ctx.go index 4e196df3..a176292b 100644 --- a/ctx.go +++ b/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 } diff --git a/ctx_custom_interface.go b/ctx_custom_interface.go index b4b343a2..829e3d65 100644 --- a/ctx_custom_interface.go +++ b/ctx_custom_interface.go @@ -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 diff --git a/ctx_test.go b/ctx_test.go index 2b10316f..6af29483 100644 --- a/ctx_test.go +++ b/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