Skip to content

Commit fadbb0a

Browse files
committed
Implement Fluent Method Chaining for Status and Type Methods Using Generics #3221
1 parent b695df6 commit fadbb0a

File tree

7 files changed

+15
-17
lines changed

7 files changed

+15
-17
lines changed

app.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const Version = "3.0.0-beta.4"
3737
// Handler defines a function to serve HTTP requests.
3838
type Handler = func(ctx Ctx) error
3939

40-
type customCtxFunc = func(app *App[Ctx]) CustomCtx[Ctx]
41-
4240
// Map is a shortcut for map[string]any, useful for JSON returns
4341
type Map map[string]any
4442

@@ -103,7 +101,7 @@ type App[TCtx CtxGeneric[TCtx]] struct {
103101
// Latest route & group
104102
latestRoute *Route
105103
// newCtxFunc
106-
newCtxFunc customCtxFunc
104+
newCtxFunc func(app *App[TCtx]) CustomCtx[TCtx]
107105
// TLS handler
108106
tlsHandler *TLSHandler
109107
// Mount fields
@@ -492,8 +490,8 @@ func DefaultErrorHandler(c Ctx, err error) error {
492490
// Prefork: true,
493491
// ServerHeader: "Fiber",
494492
// })
495-
func New(config ...Config) *App[DefaultCtx] {
496-
app := newApp[DefaultCtx](config...)
493+
func New(config ...Config) *App[*DefaultCtx] {
494+
app := newApp[*DefaultCtx](config...)
497495

498496
// Init app
499497
app.init()
@@ -519,7 +517,7 @@ func New(config ...Config) *App[DefaultCtx] {
519517
// Prefork: true,
520518
// ServerHeader: "Fiber",
521519
// })
522-
func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc customCtxFunc, config ...Config) *App[TCtx] {
520+
func NewWithCustomCtx[TCtx CtxGeneric[TCtx]](newCtxFunc func(app *App[TCtx]) CustomCtx[TCtx], config ...Config) *App[TCtx] {
523521
app := newApp[TCtx](config...)
524522

525523
// Set newCtxFunc
@@ -758,7 +756,7 @@ func (app *App[TCtx]) Use(args ...any) Router {
758756
switch arg := args[i].(type) {
759757
case string:
760758
prefix = arg
761-
case *App:
759+
case *App[TCtx]:
762760
subApp = arg
763761
case []string:
764762
prefixes = arg

ctx_interface.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// Group struct
1313
type Group struct {
14-
app *App[Ctx]
14+
app *App[*DefaultCtx]
1515
parentGroup *Group
1616
name string
1717

hooks.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ type (
1313
OnListenHandler = func(ListenData) error
1414
OnShutdownHandler = func() error
1515
OnForkHandler = func(int) error
16-
OnMountHandler = func(*App[Ctx]) error
16+
OnMountHandler = func(*App[*DefaultCtx]) error
1717
)
1818

1919
// Hooks is a struct to use it with App.
2020
type Hooks struct {
2121
// Embed app
22-
app *App[Ctx]
22+
app *App[*DefaultCtx]
2323

2424
// Hooks
2525
onRoute []OnRouteHandler
@@ -39,7 +39,7 @@ type ListenData struct {
3939
TLS bool
4040
}
4141

42-
func newHooks(app *App[Ctx]) *Hooks {
42+
func newHooks(app *App[*DefaultCtx]) *Hooks {
4343
return &Hooks{
4444
app: app,
4545
onRoute: make([]OnRouteHandler, 0),

listen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type ListenConfig struct {
5858
// BeforeServeFunc allows customizing and accessing fiber app before serving the app.
5959
//
6060
// Default: nil
61-
BeforeServeFunc func(app *App[any]) error `json:"before_serve_func"`
61+
BeforeServeFunc func(app *App[*DefaultCtx]) error `json:"before_serve_func"`
6262

6363
// OnShutdownError allows to customize error behavior when to graceful shutdown server by given signal.
6464
//

mount.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// Put fields related to mounting.
1616
type mountFields struct {
1717
// Mounted and main apps
18-
appList map[string]*App[Ctx]
18+
appList map[string]*App[*DefaultCtx]
1919
// Prefix of app if it was mounted
2020
mountPath string
2121
// Ordered keys of apps (sorted by key length for Render)
@@ -27,9 +27,9 @@ type mountFields struct {
2727
}
2828

2929
// Create empty mountFields instance
30-
func newMountFields(app *App[any]) *mountFields {
30+
func newMountFields(app *App[*DefaultCtx]) *mountFields {
3131
return &mountFields{
32-
appList: map[string]*App[any]{"": app},
32+
appList: map[string]*App[*DefaultCtx]{"": app},
3333
appListKeys: make([]string, 0),
3434
}
3535
}

register.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var _ (Register) = (*Registering)(nil)
2626

2727
// Registering struct
2828
type Registering struct {
29-
app *App[any]
29+
app *App[*DefaultCtx]
3030

3131
path string
3232
}

0 commit comments

Comments
 (0)