Implement Fluent Method Chaining for Status and Type Methods Using Generics
We propose implementing the fluent concept for the methods Status and Type in the Fiber framework using generics, as discussed in the following articles:
- Generic Interface Functions in Go
- Go Generics Playground Example
- Self-Referencing Interfaces in Golang 1.18
Background:
Using generics allows for method chaining by having methods return a generic interface type, similar to the self keyword in other languages. Since Go does not have a self keyword, generics provide a feasible alternative without significant language changes.
Proposal:
Modify the
ContextInterface:Change methods like
StatusandTypeto return a generic interface instead of a concrete type.This enables fluent method chaining while maintaining type safety and flexibility.
Implement a Generic
CtxInterface onApp:- To prevent class switching when registering a custom context that uses fluent methods, implement a generic interface for
Ctxon theApp.
- To prevent class switching when registering a custom context that uses fluent methods, implement a generic interface for
Provide Multiple
NewMethods:- Offer additional constructor methods so developers who do not need a custom context can continue using
fiber.Newwithout any changes. - Provide alternative constructors for those who wish to customize the context.
- Offer additional constructor methods so developers who do not need a custom context can continue using
Benefits:
- Maintains ease of use for existing users.
- Enhances flexibility for developers requiring custom contexts.
- Utilizes Go's generics feature introduced in 1.18 for improved design patterns.
References:
- Original PR: gofiber/contrib#1202
- Related Discussion: Comment in PR
Requested by: @ReneWerner87
Source: gofiber/fiber