Skip to content

Commit

Permalink
Implement Fluent Method Chaining for Status and Type Methods Using Ge…
Browse files Browse the repository at this point in the history
…nerics #3221
  • Loading branch information
ReneWerner87 committed Feb 3, 2025
1 parent b695df6 commit fadbb0a
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 17 deletions.
12 changes: 5 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ctx_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Group struct
type Group struct {
app *App[Ctx]
app *App[*DefaultCtx]
parentGroup *Group
name string

Expand Down
6 changes: 3 additions & 3 deletions hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
6 changes: 3 additions & 3 deletions mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
}
}
Expand Down
2 changes: 1 addition & 1 deletion register.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ (Register) = (*Registering)(nil)

// Registering struct
type Registering struct {
app *App[any]
app *App[*DefaultCtx]

path string
}
Expand Down

0 comments on commit fadbb0a

Please sign in to comment.