-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🚀 [Feature]: Implement Generic APIs for Enhanced Variable Type #2758
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
We could do it for v3 not for v2 |
@ReneWerner87 I would like to work on this issue. Can I work on it? In case so, which branch should I use to fork? |
@brunodmartins v3-beta is the latest v3 development branch |
Reopened for Param and Get function |
@ReneWerner87 I would like to do this task. If it's possible |
Ok thx, every help is appreciated |
Thanks @ReneWerner87 =) @dozheiny it's yours =) |
@dozheiny How is the progress ? |
Sorry, I was too busy to do this task. I'll start today. |
please try to abstract the existing functionality so that it can also be used for the other functions |
@ReneWerner87 What do you think if QueryType is changed to GenericValue which suits its purpose so that in the future it can be used for other needs? |
you have this generic function and use it in a QueryType function, which then only fetches the value from the fasthttp resource and passes it into the following function so the QueryType is small and only contains the part for fetching the data + the new generic function only the source of the data changes, the following processing should be the same |
Oh I see. Thank you for the valuable suggestion. |
@ReneWerner87 What do you think about allowing users to pass their parser function in arguments? func Params[T any](c Ctx, key string, convertor func(string) (T, error), defaultValue ...T) (*T, error) {
value, err := convertor(c.Params(key))
if err != nil {
if len(defaultValue) > 0 {
return &defaultValue[0], nil
}
return nil, fmt.Errorf("failed to convert: %w", err)
}
return &value, nil
} It helps users to parse their data type directly, like objectId in Mongo database or UUID. |
That was my proposed solution on #2777. I think that could be good for the users @dozheiny and @ReneWerner87 |
I agree with you. My opinion is not to force users to use the data types supported by Fiber. Let's give them what they want. |
I rather like the way where fiber provides the standard types If I have a custom mapping as a user, I can rebuild the short snippet given here myself |
Of course, we can also provide such a function in addition, but then additionally and without reference to the source, so that the string is given in as input Instead of the ctx and key param Convert[T any](value string, convertor func(string) (T, error), defaultValue ...T) (*T, error) {
... |
I like this method, With the |
I think we can create something like https://pkg.go.dev/fmt#Stringer to allow people implement own data types. package main
import (
"fmt"
"strconv"
"strings"
)
type GenericType interface {
int | string | any
}
type Convertible[T any] interface {
Convert(string) T
}
func genericValue[T GenericType](s string) T {
var t T
switch any(t).(type) {
case int:
i, _ := strconv.Atoi(s)
return assertValueType[T](i)
case string:
return t
default:
c, ok := any(t).(Convertible[T])
if ok {
return c.Convert(s)
}
return t
}
}
type testStruct struct {
key string
value string
}
func (testStruct) Convert(s string) testStruct {
k, v, _ := strings.Cut(s, "=")
return testStruct{k, v}
}
type testStruct2 struct {
key string
}
func main() {
i := genericValue[int]("42")
fmt.Println(i)
j := genericValue[testStruct]("foo=bar")
fmt.Println(j)
k := genericValue[testStruct2]("baz")
fmt.Println(k)
} Output: 42
{foo bar}
{} |
Feature Description
Some APIs like ctx.QueryBool or ctx.QueryFloat convert given variables. Why don't add APIs that let users convert the needed variable types?
For example, Query convertor API can be implemented like this (Since we can't use generics in methods):
Usage:
We can implement other APIs like ctx.Params and ctx.Get functions.
Additional Context (optional)
No response
Code Snippet (optional)
No response
Checklist:
The text was updated successfully, but these errors were encountered: