Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,14 @@ func (c *DefaultCtx) Get(key string, defaultValue ...string) string {

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

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

// Params is used to get the route parameters.
// This function is generic and can handle different route parameters type values.
// If the generic type cannot be matched to a supported type, the function
// returns the default value (if provided) or the zero value of type V.
//
// Example:
//
Expand All @@ -1115,8 +1122,11 @@ func (c *DefaultCtx) Params(key string, defaultValue ...string) string {
// http://example.com/id/:number -> http://example.com/id/john
// Params[int](c, "number", 0) -> returns 0 because can't parse 'john' as integer.
func Params[V GenericType](c Ctx, key string, defaultValue ...V) V {
var v V
return genericParseType(c.Params(key), v, defaultValue...)
v, err := genericParseType[V](c.Params(key))
if err != nil && len(defaultValue) > 0 {
return defaultValue[0]
Comment thread
ksw2000 marked this conversation as resolved.
}
return v
}

// Path returns the path part of the request URL.
Expand Down Expand Up @@ -1238,10 +1248,12 @@ func (c *DefaultCtx) Queries() map[string]string {
// age := Query[int](c, "age") // Returns 8
// unknown := Query[string](c, "unknown", "default") // Returns "default" since the query parameter "unknown" is not found
func Query[V GenericType](c Ctx, key string, defaultValue ...V) V {
var v V
q := c.App().getString(c.RequestCtx().QueryArgs().Peek(key))

return genericParseType[V](q, v, defaultValue...)
v, err := genericParseType[V](q)
if err != nil && len(defaultValue) > 0 {
return defaultValue[0]
}
return v
}

// Range returns a struct containing the type and a slice of ranges.
Expand Down
Loading
Loading