-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add NewWithCustomCtx initialization helper #3476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6da4783
4fbcebf
c956f6c
40dea8e
fe01057
4bacc1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -512,12 +512,13 @@ func (app *App) Handler() fasthttp.RequestHandler | |
func (app *App) ErrorHandler(ctx Ctx, err error) error | ||
``` | ||
|
||
## NewCtxFunc | ||
## NewWithCustomCtx | ||
|
||
`NewCtxFunc` allows you to customize the `ctx` struct as needed. | ||
`NewWithCustomCtx` creates a new `*App` and sets the custom context factory | ||
function at construction time. | ||
|
||
```go title="Signature" | ||
func (app *App) NewCtxFunc(function func(app *App) CustomCtx) | ||
func NewWithCustomCtx(fn func(app *App) CustomCtx, config ...Config) *App | ||
``` | ||
Comment on lines
+515
to
522
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Document generic type parameter in signature func NewWithCustomCtx[CC Ctx](fn func(app *App) CC, config ...Config) *App 🤖 Prompt for AI Agents
|
||
|
||
```go title="Example" | ||
|
@@ -533,22 +534,18 @@ type CustomCtx struct { | |
fiber.DefaultCtx | ||
} | ||
|
||
// Custom method | ||
func (c *CustomCtx) Params(key string, defaultValue ...string) string { | ||
return "prefix_" + c.DefaultCtx.Params(key) | ||
} | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx { | ||
app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.CustomCtx { | ||
return &CustomCtx{ | ||
DefaultCtx: *fiber.NewDefaultCtx(app), | ||
} | ||
}) | ||
Comment on lines
541
to
546
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix incorrect return type in factory signature app := fiber.NewWithCustomCtx(func(app *fiber.App) CustomCtx {
return &CustomCtx{ DefaultCtx: *fiber.NewDefaultCtx(app) }
}) Or explicitly return the interface type: func(app *fiber.App) fiber.Ctx { … } 🤖 Prompt for AI Agents
|
||
|
||
app.Get("/:id", func(c fiber.Ctx) error { | ||
// Use custom method - output: prefix_123 | ||
return c.SendString(c.Params("id")) | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
LGTM! Context unification looks good, but verify performance impact.
The signature change from
Ctx
toCustomCtx
aligns well with the unified context handling approach. The type assertion ensures type safety at runtime.However, based on the PR objectives, there are performance concerns (~10ns regression) with this new implementation. Consider monitoring the performance impact of this type assertion in hot paths.
🏁 Script executed:
Length of output: 70365
🏁 Script executed:
Length of output: 350
Update panic message in AcquireCtx to reflect CustomCtx
ctx_interface.go
(line 50), the panic readsCustomCtx
. Please change the message to something like:AcquireCtx
already handle theCustomCtx
return (verified across router.go, tests, etc.).🤖 Prompt for AI Agents