Skip to content

🎧 Your Personal Streaming Service

Make `ParamInt` generic (any int type)

Deluanecadcfb403e8
diff --git a/server/subsonic/bookmarks.go b/server/subsonic/bookmarks.go
index e2dd3daff..86d5d768b 100644
--- a/server/subsonic/bookmarks.go
+++ b/server/subsonic/bookmarks.go
@@ -42,7 +42,7 @@ func (api *Router) CreateBookmark(r *http.Request) (*responses.Subsonic, error)
}
comment := utils.ParamString(r, "comment")
- position := utils.ParamInt64(r, "position", 0)
+ position := utils.ParamInt(r, "position", int64(0))
repo := api.ds.MediaFile(r.Context())
err = repo.AddBookmark(id, comment, position)
@@ -94,7 +94,7 @@ func (api *Router) SavePlayQueue(r *http.Request) (*responses.Subsonic, error) {
}
current := utils.ParamString(r, "current")
- position := utils.ParamInt64(r, "position", 0)
+ position := utils.ParamInt(r, "position", int64(0))
user, _ := request.UserFrom(r.Context())
client, _ := request.ClientFrom(r.Context())
diff --git a/utils/request_helpers.go b/utils/request_helpers.go
index 8f372ffe1..104260de4 100644
--- a/utils/request_helpers.go
+++ b/utils/request_helpers.go
@@ -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 {
diff --git a/utils/request_helpers_test.go b/utils/request_helpers_test.go
index 6956a286d..a7a82911d 100644
--- a/utils/request_helpers_test.go
+++ b/utils/request_helpers_test.go
@@ -105,35 +105,32 @@ var _ = Describe("Request Helpers", func() {
BeforeEach(func() {
r = httptest.NewRequest("GET", "/ping?i=123&inv=123.45", nil)
})
+ Context("int", func() {
+ It("returns default value if param does not exist", func() {
+ Expect(ParamInt(r, "xx", 999)).To(Equal(999))
+ })
- It("returns default value if param does not exist", func() {
- Expect(ParamInt(r, "xx", 999)).To(Equal(999))
- })
-
- It("returns default value if param is an invalid int", func() {
- Expect(ParamInt(r, "inv", 999)).To(Equal(999))
- })
-
- It("returns parsed time", func() {
- Expect(ParamInt(r, "i", 999)).To(Equal(123))
- })
- })
+ It("returns default value if param is an invalid int", func() {
+ Expect(ParamInt(r, "inv", 999)).To(Equal(999))
+ })
- Describe("ParamInt64", func() {
- BeforeEach(func() {
- r = httptest.NewRequest("GET", "/ping?i=123&inv=123.45", nil)
+ It("returns parsed time", func() {
+ Expect(ParamInt(r, "i", 999)).To(Equal(123))
+ })
})
+ Context("int64", func() {
+ It("returns default value if param does not exist", func() {
+ Expect(ParamInt(r, "xx", int64(999))).To(Equal(int64(999)))
+ })
- It("returns default value if param does not exist", func() {
- Expect(ParamInt64(r, "xx", 999)).To(Equal(int64(999)))
- })
+ It("returns default value if param is an invalid int", func() {
+ Expect(ParamInt(r, "inv", int64(999))).To(Equal(int64(999)))
+ })
- It("returns default value if param is an invalid int", func() {
- Expect(ParamInt64(r, "inv", 999)).To(Equal(int64(999)))
- })
+ It("returns parsed time", func() {
+ Expect(ParamInt(r, "i", int64(999))).To(Equal(int64(123)))
+ })
- It("returns parsed time", func() {
- Expect(ParamInt64(r, "i", 999)).To(Equal(int64(123)))
})
})