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

This commit is contained in:
René 2025-05-18 21:43:36 +02:00
parent c3a532c4fa
commit b7be67fee5
4 changed files with 12 additions and 28 deletions

4
app.go
View File

@ -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
View File

@ -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
}

View File

@ -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

View File

@ -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