Skip to content

Commit a5f76a7

Browse files
ksw2000gaby
andauthored
♻️ refactor: Make genericParseType return error (#3473)
* ♻️ refact: make genericParseType return error * 🐛 fix: return error when parsing unsupported type * 🚨 test: cover the default value for Params * 🚨 test: cover default value on parsing error * ♻️ refact: change the benchmark name * 🚨 test: remove the duplicated maxUint16 test case --------- Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
1 parent e722d82 commit a5f76a7

File tree

4 files changed

+825
-1354
lines changed

4 files changed

+825
-1354
lines changed

ctx.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,14 @@ func (c *DefaultCtx) Get(key string, defaultValue ...string) string {
644644

645645
// GetReqHeader returns the HTTP request header specified by filed.
646646
// This function is generic and can handle different headers type values.
647+
// If the generic type cannot be matched to a supported type, the function
648+
// returns the default value (if provided) or the zero value of type V.
647649
func GetReqHeader[V GenericType](c Ctx, key string, defaultValue ...V) V {
648-
var v V
649-
return genericParseType[V](c.App().getString(c.Request().Header.Peek(key)), v, defaultValue...)
650+
v, err := genericParseType[V](c.App().getString(c.Request().Header.Peek(key)))
651+
if err != nil && len(defaultValue) > 0 {
652+
return defaultValue[0]
653+
}
654+
return v
650655
}
651656

652657
// GetRespHeader returns the HTTP response header specified by field.
@@ -1103,6 +1108,8 @@ func (c *DefaultCtx) Params(key string, defaultValue ...string) string {
11031108

11041109
// Params is used to get the route parameters.
11051110
// This function is generic and can handle different route parameters type values.
1111+
// If the generic type cannot be matched to a supported type, the function
1112+
// returns the default value (if provided) or the zero value of type V.
11061113
//
11071114
// Example:
11081115
//
@@ -1115,8 +1122,11 @@ func (c *DefaultCtx) Params(key string, defaultValue ...string) string {
11151122
// http://example.com/id/:number -> http://example.com/id/john
11161123
// Params[int](c, "number", 0) -> returns 0 because can't parse 'john' as integer.
11171124
func Params[V GenericType](c Ctx, key string, defaultValue ...V) V {
1118-
var v V
1119-
return genericParseType(c.Params(key), v, defaultValue...)
1125+
v, err := genericParseType[V](c.Params(key))
1126+
if err != nil && len(defaultValue) > 0 {
1127+
return defaultValue[0]
1128+
}
1129+
return v
11201130
}
11211131

11221132
// Path returns the path part of the request URL.
@@ -1238,10 +1248,12 @@ func (c *DefaultCtx) Queries() map[string]string {
12381248
// age := Query[int](c, "age") // Returns 8
12391249
// unknown := Query[string](c, "unknown", "default") // Returns "default" since the query parameter "unknown" is not found
12401250
func Query[V GenericType](c Ctx, key string, defaultValue ...V) V {
1241-
var v V
12421251
q := c.App().getString(c.RequestCtx().QueryArgs().Peek(key))
1243-
1244-
return genericParseType[V](q, v, defaultValue...)
1252+
v, err := genericParseType[V](q)
1253+
if err != nil && len(defaultValue) > 0 {
1254+
return defaultValue[0]
1255+
}
1256+
return v
12451257
}
12461258

12471259
// Range returns a struct containing the type and a slice of ranges.

0 commit comments

Comments
 (0)