mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-14 11:17:19 +03:00
Make ParamInt
generic (any int type)
This commit is contained in:
parent
f69c27d146
commit
ecadcfb403
@ -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())
|
||||
|
@ -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 {
|
||||
|
@ -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))
|
||||
})
|
||||
})
|
||||
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 is an invalid int", func() {
|
||||
Expect(ParamInt(r, "inv", 999)).To(Equal(999))
|
||||
})
|
||||
It("returns default value if param is an invalid int", func() {
|
||||
Expect(ParamInt(r, "inv", int64(999))).To(Equal(int64(999)))
|
||||
})
|
||||
|
||||
It("returns parsed time", func() {
|
||||
Expect(ParamInt(r, "i", 999)).To(Equal(123))
|
||||
})
|
||||
})
|
||||
It("returns parsed time", func() {
|
||||
Expect(ParamInt(r, "i", int64(999))).To(Equal(int64(123)))
|
||||
})
|
||||
|
||||
Describe("ParamInt64", func() {
|
||||
BeforeEach(func() {
|
||||
r = httptest.NewRequest("GET", "/ping?i=123&inv=123.45", nil)
|
||||
})
|
||||
|
||||
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(ParamInt64(r, "inv", 999)).To(Equal(int64(999)))
|
||||
})
|
||||
|
||||
It("returns parsed time", func() {
|
||||
Expect(ParamInt64(r, "i", 999)).To(Equal(int64(123)))
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user