-
-
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
v3: Add QueryParser for get query using generic #2776
Conversation
Introduced a new method, QueryParser, to parse query parameters from a given context into specified types: integer, boolean, float, and string. The method provides default values for empty or invalid keys. Corresponding tests for each type have also been added to validate the functionality.
@efectn Is it correct to add this function to ctx.go or are there any recommendations for placing this function? |
Hello, there's another PR for this that was opened right before this one at #2777. Let's compare these implementations. |
I think it's better idea to use a function parameter. It gives us more extenibility. If we want extendibility with reflect, then we will need to check struct methods like https://go.dev/tour/methods/17 that seems to be overkill. |
@efectn I follow whichever fiber is best, because I built it based solely on ease of use. If there is a better solution in my opinion it is better to use the solution that you think is better. |
Sure. This is open to discussion and development. It was just my opinion. |
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.
@ryanbekhen
thx for the effort
some little adjustments are needed
pls also check the linter hints
@ReneWerner87 Thank you for your input, maybe tonight I can work on it. |
Refactored the existing QueryParser method in the code to simplify its structure. Instead of reflecting on types, it now uses explicit type checking. In addition to the existing support for integers, booleans, and floats, the QueryParser method now also supports string parsing. Corresponding tests for the updated method and new feature were added as well.
Updated the method call example in the comment for the Query function in the ctx.go file. Previously, it was incorrectly demonstrating a call to "QueryParser("wanna_cake", 1)", but this has been updated to correctly represent the method it is commenting, resulting in "Query("wanna_cake", 1)".
The update introduces better type assertion handling in the Query function. A switch statement is now employed to determine the type of the value as opposed to the previous if clauses. In addition, a validation step has been added to ensure the context passed into the function is of the correct type.
The Query function in ctx.go has been refactored for better and clearer type handling. The code now uses a 'QueryType' interface, replacing explicit string, bool, float, and int declarations. This change also improves the error message when a type assertion fails, making it more descriptive about the specific failure.
@ryanbekhen last task is the linter hints |
command for local |
Updated the code in ctx.go to add a type assertion check for all case statements. The function now checks if the returned value is of the expected type, and if not, it throws a panic with a description of the failed type assertion.
@ReneWerner87 I've done a push |
@ryanbekhen we forgot the documentation and the old methods old methods:
could you pls change this |
…mentation Updated the assertValueType function to utilize the utils.UnsafeBytes method for byte conversion. Enhanced the documentation for query parameter types to offer clearer, more comprehensive explanations and examples, including QueryTypeInteger, QueryTypeFloat, and subcategories.
@nickajacks1 @ReneWerner87 @efectn This ci error is because on cache_test.go, extractors.go, keyauth.go, proxy_test.go, store.go and tags.go must be changed from c.Query to Query, is this also me pushing so that the CI can run? |
yes pls |
In this commit, the conventional `c.Query()` calls across multiple middleware and document files are updated to use the new `fiber.Query` syntax. The changes align with the updated function signatures in Fiber library that provides type-specific querying. These enhancements contribute to the project's overall robustness and consistency.
@ReneWerner87 I've pushed, please check. |
@ryanbekhen sorry, we have misunderstood each other query should already be part of the ctx current: func Query[V QueryType](c Ctx, key string, defaultValue ...V) V { please change like this func (c *DefaultCtx) Query[V QueryType](key string, defaultValue ...V) V { |
I think it's not possible to use generics on struct methods golang/go#49085 |
Unfortunately in Go Generics it doesn't support up to there, that's also what kept me from removing c.Query yesterday. |
Oh, I didn't know that |
The changes made if you want to be 'c.Query' are as follows, but this must be considered first. |
It would change a lot if done like this, my suggestion if you want to use Query on Ctx keep using Query which returns a string like yesterday. |
@ReneWerner87 I have done the push and below is my final benchmark:
|
In the query method, the utils.UnsafeBytes function was replaced with the ctx.app.getBytes method. This change enhances the extraction of query string parameters by making it safer and more context-specific.
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.
looks very good already, we are almost through
sorry for the other comments
@@ -1349,95 +1349,6 @@ app.Get("/", func(c fiber.Ctx) error { | |||
> _Returned value is only valid within the handler. Do not store any references. | |||
> Make copies or use the_ [_**`Immutable`**_](ctx.md) _setting instead._ [_Read more..._](../#zero-allocation) | |||
|
|||
## QueryBool |
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.
please show alternatives with the other new method and other data types to document the Query
method
if necessary, create a new block for the new Query
generic method and link to it in a note box in ctx.Query
it is also important that people know that they have to make a copy outside (the box that we had on the old stuff
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.
@ryanbekhen don´t forget this -> we need a hint in the ctx.Query
method
The parsing functions in query handlers have been refactored to simplify the process. Parsing code has been extracted into dedicated functions like 'parseIntWithDefault' and 'parseFloatWithDefault', and they now reside in a new utils file. This modularization improves readability and maintainability of the code. Additionally, documentation is updated to reflect the changes.
The parsing functions have been restructured to enhance readability and reduce repetition in the ctx.go file. This was achieved by creating generalised parsing functions that handle defaults and ensure the correct value type is returned. As a result, various single-use parsing functions in the utils.go file have been removed.
@ReneWerner87 I have done the push and below is my final benchmark, please check:
|
almost done, only the documentation part is missing |
@ReneWerner87 what else should I add to both documentation and code? |
For the method in context, you should add a hint box in the documentation explaining that we have the generic method for other data types and then link it In the documentation for the generic method, the part with the zero allocations must also be mentioned, which is also found in most other methods |
do you mean like this? |
Ok maybe this is enough, |
@ReneWerner87 |
I suppose we can do the same for Params and Get |
Introduced a new method, QueryParser, to parse query parameters from a given context into specified types: integer, boolean, float, and string. The method provides default values for empty or invalid keys. Corresponding tests for each type have also been added to validate the functionality.
Description
This Pull Request introduces a feature within the GoFiber project, specifically a QueryParser function. This is a generic function that parses a given query parameter from a context, and returns it as a specified type. This function achieves utility and versatility through it's support for various types, including integers, booleans, floats, and strings.
When a client sends a GET request with a query parameter, the QueryParser function can be leveraged to convert the parameter’s value to the required type. It gracefully handles failed parsing scenarios by using provided default values.
If the Generics are not explicitly specified, it will infer the type based on the provided default value to the function.
This extends the capacity of GoFiber to interact with and process HTTP requests, improving the manner GoFiber applications interpret and handle data.
Below is an example of its usage:
In this example, the QueryParser function takes wanna_cake from the GET request as a key and 1 as the default value if parsing to the preferred type was unsuccessful or if the key doesn’t exist. The result of this function call would be the integer 2 (parsed from the value corresponding to wanna_cake key in the GET request).
Fixes # (#2758)
Type of change
Please delete options that are not relevant.
Checklist:
Commit formatting:
Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md