-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor parsing functions in query handlers
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.
- Loading branch information
1 parent
2fa8b46
commit 0b63d06
Showing
3 changed files
with
120 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package fiber | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// assertValueType asserts the type of the result to the type of the value | ||
func assertValueType[V QueryType, T any](result T) V { | ||
v, ok := any(result).(V) | ||
if !ok { | ||
panic(fmt.Errorf("failed to type-assert to %T", v)) | ||
} | ||
return v | ||
} | ||
|
||
func parseIntWithDefault[V QueryType](q string, bitSize int, defaultValue ...V) int64 { | ||
result, err := strconv.ParseInt(q, 10, bitSize) | ||
if err != nil { | ||
if len(defaultValue) > 0 { | ||
return assertValueType[int64, V](defaultValue[0]) | ||
} | ||
return int64(0) | ||
} | ||
return result | ||
} | ||
|
||
func parseUintWithDefault[V QueryType](q string, bitSize int, defaultValue ...V) uint64 { | ||
result, err := strconv.ParseUint(q, 10, bitSize) | ||
if err != nil { | ||
if len(defaultValue) > 0 { | ||
return assertValueType[uint64, V](defaultValue[0]) | ||
} | ||
return uint64(0) | ||
} | ||
return result | ||
} | ||
|
||
func parseFloatWithDefault[V QueryType](q string, bitSize int, defaultValue ...V) float64 { | ||
result, err := strconv.ParseFloat(q, bitSize) | ||
if err != nil { | ||
if len(defaultValue) > 0 { | ||
return assertValueType[float64, V](defaultValue[0]) | ||
} | ||
return float64(0) | ||
} | ||
return result | ||
} | ||
|
||
func parseStringWithDefault[V QueryType](q string, defaultValue ...V) string { | ||
if q == "" && len(defaultValue) > 0 { | ||
return assertValueType[string, V](defaultValue[0]) | ||
} | ||
return q | ||
} | ||
|
||
func parseBoolWithDefault[V QueryType](q string, defaultValue ...V) bool { | ||
result, err := strconv.ParseBool(q) | ||
if err != nil { | ||
if len(defaultValue) > 0 { | ||
return assertValueType[bool, V](defaultValue[0]) | ||
} | ||
return false | ||
} | ||
return result | ||
} |