From fadbb0a50307d515bf4a062ed32ce3f2e4a834a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Mon, 3 Feb 2025 15:31:39 +0100 Subject: [PATCH] Implement Fluent Method Chaining for Status and Type Methods Using Generics #3221 --- app.go | 12 +++++------- ctx_interface.go | 2 +- group.go | 2 +- hooks.go | 6 +++--- listen.go | 2 +- mount.go | 6 +++--- register.go | 2 +- 7 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app.go b/app.go index c686fb5f48..50ec0bed4e 100644 --- a/app.go +++ b/app.go @@ -37,8 +37,6 @@ const Version = "3.0.0-beta.4" // Handler defines a function to serve HTTP requests. type Handler = func(ctx Ctx) error -type customCtxFunc = func(app *App[Ctx]) CustomCtx[Ctx] - // Map is a shortcut for map[string]any, useful for JSON returns type Map map[string]any @@ -103,7 +101,7 @@ type App[TCtx CtxGeneric[TCtx]] struct { // Latest route & group latestRoute *Route // newCtxFunc - newCtxFunc customCtxFunc + newCtxFunc func(app *App[TCtx]) CustomCtx[TCtx] // TLS handler tlsHandler *TLSHandler // Mount fields @@ -492,8 +490,8 @@ func DefaultErrorHandler(c Ctx, err error) error { // Prefork: true, // ServerHeader: "Fiber", // }) -func New(config ...Config) *App[DefaultCtx] { - app := newApp[DefaultCtx](config...) +func New(config ...Config) *App[*DefaultCtx] { + app := newApp[*DefaultCtx](config...) // Init app app.init() @@ -519,7 +517,7 @@ func New(config ...Config) *App[DefaultCtx] { // Prefork: true, // ServerHeader: "Fiber", // }) -func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc customCtxFunc, config ...Config) *App[TCtx] { +func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc func(app *App[TCtx]) CustomCtx[TCtx], config ...Config) *App[TCtx] { app := newApp[TCtx](config...) // Set newCtxFunc @@ -758,7 +756,7 @@ func (app *App[TCtx]) Use(args ...any) Router { switch arg := args[i].(type) { case string: prefix = arg - case *App: + case *App[TCtx]: subApp = arg case []string: prefixes = arg diff --git a/ctx_interface.go b/ctx_interface.go index 7f32428905..be58c25f04 100644 --- a/ctx_interface.go +++ b/ctx_interface.go @@ -24,7 +24,7 @@ type CtxGeneric[T any] interface { // AcceptsLanguages checks if the specified language is acceptable. AcceptsLanguages(offers ...string) string // App returns the *App[T] reference to the instance of the Fiber application - App() *App[T] + App() *App[*DefaultCtx] // Append the specified value to the HTTP response header field. // If the header is not already set, it creates the header with the specified value. Append(field string, values ...string) diff --git a/group.go b/group.go index fdf5118e50..6101f34e00 100644 --- a/group.go +++ b/group.go @@ -11,7 +11,7 @@ import ( // Group struct type Group struct { - app *App[Ctx] + app *App[*DefaultCtx] parentGroup *Group name string diff --git a/hooks.go b/hooks.go index b7c974f193..1b820280a3 100644 --- a/hooks.go +++ b/hooks.go @@ -13,13 +13,13 @@ type ( OnListenHandler = func(ListenData) error OnShutdownHandler = func() error OnForkHandler = func(int) error - OnMountHandler = func(*App[Ctx]) error + OnMountHandler = func(*App[*DefaultCtx]) error ) // Hooks is a struct to use it with App. type Hooks struct { // Embed app - app *App[Ctx] + app *App[*DefaultCtx] // Hooks onRoute []OnRouteHandler @@ -39,7 +39,7 @@ type ListenData struct { TLS bool } -func newHooks(app *App[Ctx]) *Hooks { +func newHooks(app *App[*DefaultCtx]) *Hooks { return &Hooks{ app: app, onRoute: make([]OnRouteHandler, 0), diff --git a/listen.go b/listen.go index 561ce72c20..cd9470a03e 100644 --- a/listen.go +++ b/listen.go @@ -58,7 +58,7 @@ type ListenConfig struct { // BeforeServeFunc allows customizing and accessing fiber app before serving the app. // // Default: nil - BeforeServeFunc func(app *App[any]) error `json:"before_serve_func"` + BeforeServeFunc func(app *App[*DefaultCtx]) error `json:"before_serve_func"` // OnShutdownError allows to customize error behavior when to graceful shutdown server by given signal. // diff --git a/mount.go b/mount.go index 8d32235ca5..76e152b323 100644 --- a/mount.go +++ b/mount.go @@ -15,7 +15,7 @@ import ( // Put fields related to mounting. type mountFields struct { // Mounted and main apps - appList map[string]*App[Ctx] + appList map[string]*App[*DefaultCtx] // Prefix of app if it was mounted mountPath string // Ordered keys of apps (sorted by key length for Render) @@ -27,9 +27,9 @@ type mountFields struct { } // Create empty mountFields instance -func newMountFields(app *App[any]) *mountFields { +func newMountFields(app *App[*DefaultCtx]) *mountFields { return &mountFields{ - appList: map[string]*App[any]{"": app}, + appList: map[string]*App[*DefaultCtx]{"": app}, appListKeys: make([]string, 0), } } diff --git a/register.go b/register.go index 913d983f1b..66d4d43475 100644 --- a/register.go +++ b/register.go @@ -26,7 +26,7 @@ var _ (Register) = (*Registering)(nil) // Registering struct type Registering struct { - app *App[any] + app *App[*DefaultCtx] path string }