Make ParamInt generic (any int type)

This commit is contained in:
Deluan
2023-11-23 13:40:06 -05:00
parent f69c27d146
commit ecadcfb403
3 changed files with 26 additions and 40 deletions
+3 -14
View File
@@ -7,6 +7,7 @@ import (
"time"
"github.com/navidrome/navidrome/log"
"golang.org/x/exp/constraints"
)
func ParamString(r *http.Request, param string) string {
@@ -56,19 +57,7 @@ func ParamTime(r *http.Request, param string, def time.Time) time.Time {
return t
}
func ParamInt(r *http.Request, param string, def int) int {
v := ParamString(r, param)
if v == "" {
return def
}
value, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return def
}
return int(value)
}
func ParamInt64(r *http.Request, param string, def int64) int64 {
func ParamInt[T constraints.Integer](r *http.Request, param string, def T) T {
v := ParamString(r, param)
if v == "" {
return def
@@ -77,7 +66,7 @@ func ParamInt64(r *http.Request, param string, def int64) int64 {
if err != nil {
return def
}
return value
return T(value)
}
func ParamInts(r *http.Request, param string) []int {