Make ParamInt generic (any int type)
This commit is contained in:
@@ -42,7 +42,7 @@ func (api *Router) CreateBookmark(r *http.Request) (*responses.Subsonic, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
comment := utils.ParamString(r, "comment")
|
comment := utils.ParamString(r, "comment")
|
||||||
position := utils.ParamInt64(r, "position", 0)
|
position := utils.ParamInt(r, "position", int64(0))
|
||||||
|
|
||||||
repo := api.ds.MediaFile(r.Context())
|
repo := api.ds.MediaFile(r.Context())
|
||||||
err = repo.AddBookmark(id, comment, position)
|
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")
|
current := utils.ParamString(r, "current")
|
||||||
position := utils.ParamInt64(r, "position", 0)
|
position := utils.ParamInt(r, "position", int64(0))
|
||||||
|
|
||||||
user, _ := request.UserFrom(r.Context())
|
user, _ := request.UserFrom(r.Context())
|
||||||
client, _ := request.ClientFrom(r.Context())
|
client, _ := request.ClientFrom(r.Context())
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
|
"golang.org/x/exp/constraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParamString(r *http.Request, param string) string {
|
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
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParamInt(r *http.Request, param string, def int) int {
|
func ParamInt[T constraints.Integer](r *http.Request, param string, def T) T {
|
||||||
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 {
|
|
||||||
v := ParamString(r, param)
|
v := ParamString(r, param)
|
||||||
if v == "" {
|
if v == "" {
|
||||||
return def
|
return def
|
||||||
@@ -77,7 +66,7 @@ func ParamInt64(r *http.Request, param string, def int64) int64 {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
return value
|
return T(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParamInts(r *http.Request, param string) []int {
|
func ParamInts(r *http.Request, param string) []int {
|
||||||
|
|||||||
@@ -105,35 +105,32 @@ var _ = Describe("Request Helpers", func() {
|
|||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
r = httptest.NewRequest("GET", "/ping?i=123&inv=123.45", nil)
|
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() {
|
It("returns default value if param is an invalid int", func() {
|
||||||
Expect(ParamInt(r, "xx", 999)).To(Equal(999))
|
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() {
|
It("returns default value if param is an invalid int", func() {
|
||||||
Expect(ParamInt(r, "inv", 999)).To(Equal(999))
|
Expect(ParamInt(r, "inv", int64(999))).To(Equal(int64(999)))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns parsed time", func() {
|
It("returns parsed time", func() {
|
||||||
Expect(ParamInt(r, "i", 999)).To(Equal(123))
|
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)))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user