🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Use Go builtin min/max func
Deluan166eb377875c
diff --git a/conf/configuration.go b/conf/configuration.goindex 46c1abb71..118557e17 100644--- a/conf/configuration.go+++ b/conf/configuration.go@@ -12,7 +12,6 @@ import ("github.com/kr/pretty""github.com/navidrome/navidrome/consts""github.com/navidrome/navidrome/log"- "github.com/navidrome/navidrome/utils/number""github.com/robfig/cron/v3""github.com/spf13/viper")@@ -358,7 +357,7 @@ func init() {viper.SetDefault("devsidebarplaylists", true)viper.SetDefault("devshowartistpage", true)viper.SetDefault("devoffsetoptimize", 50000)- viper.SetDefault("devartworkmaxrequests", number.Max(2, runtime.NumCPU()/3))+ viper.SetDefault("devartworkmaxrequests", max(2, runtime.NumCPU()/3))viper.SetDefault("devartworkthrottlebackloglimit", consts.RequestThrottleBacklogLimit)viper.SetDefault("devartworkthrottlebacklogtimeout", consts.RequestThrottleBacklogTimeout)viper.SetDefault("devartistinfotimetolive", consts.ArtistInfoTimeToLive)diff --git a/core/artwork/reader_resized.go b/core/artwork/reader_resized.goindex 32e337513..ea40b06af 100644--- a/core/artwork/reader_resized.go+++ b/core/artwork/reader_resized.go@@ -16,7 +16,6 @@ import ("github.com/navidrome/navidrome/conf""github.com/navidrome/navidrome/log""github.com/navidrome/navidrome/model"- "github.com/navidrome/navidrome/utils/number")type resizedArtworkReader struct {@@ -113,7 +112,7 @@ func resizeImage(reader io.Reader, size int) (io.Reader, int, error) {// Don't upscale the imagebounds := img.Bounds()- originalSize := number.Max(bounds.Max.X, bounds.Max.Y)+ originalSize := max(bounds.Max.X, bounds.Max.Y)if originalSize <= size {return nil, originalSize, nil}diff --git a/core/external_metadata.go b/core/external_metadata.goindex a8a17b60c..33206bdeb 100644--- a/core/external_metadata.go+++ b/core/external_metadata.go@@ -19,7 +19,6 @@ import ("github.com/navidrome/navidrome/model""github.com/navidrome/navidrome/utils". "github.com/navidrome/navidrome/utils/gg"- "github.com/navidrome/navidrome/utils/number""golang.org/x/sync/errgroup")@@ -275,7 +274,7 @@ func (e *externalMetadata) SimilarSongs(ctx context.Context, id string, count inreturn ctx.Err()}- topCount := number.Max(count, 20)+ topCount := max(count, 20)topSongs, err := e.getMatchingTopSongs(ctx, e.ag, &auxArtist{Name: a.Name, Artist: a}, topCount)if err != nil {log.Warn(ctx, "Error getting artist's top songs", "artist", a.Name, err)diff --git a/model/mediafile.go b/model/mediafile.goindex 4804a56a0..00577e208 100644--- a/model/mediafile.go+++ b/model/mediafile.go@@ -12,7 +12,6 @@ import ("github.com/navidrome/navidrome/conf""github.com/navidrome/navidrome/consts""github.com/navidrome/navidrome/utils"- "github.com/navidrome/navidrome/utils/number""github.com/navidrome/navidrome/utils/slice""golang.org/x/exp/slices")@@ -209,17 +208,16 @@ func allOrNothing(items []string) (string, int) {}func minMax(items []int) (int, int) {- var max = items[0]- var min = items[0]+ var mn, mx = items[0], items[0]for _, value := range items {- max = number.Max(max, value)- if min == 0 {- min = value+ mx = max(mx, value)+ if mn == 0 {+ mn = value} else if value > 0 {- min = number.Min(min, value)+ mn = min(mn, value)}}- return min, max+ return mn, mx}func newer(t1, t2 time.Time) time.Time {diff --git a/server/subsonic/album_lists.go b/server/subsonic/album_lists.goindex 792553d42..dc6f2094d 100644--- a/server/subsonic/album_lists.go+++ b/server/subsonic/album_lists.go@@ -10,7 +10,6 @@ import ("github.com/navidrome/navidrome/model""github.com/navidrome/navidrome/server/subsonic/filter""github.com/navidrome/navidrome/server/subsonic/responses"- "github.com/navidrome/navidrome/utils/number""github.com/navidrome/navidrome/utils/req")@@ -61,7 +60,7 @@ func (api *Router) getAlbumList(r *http.Request) (model.Albums, int64, error) {}opts.Offset = p.IntOr("offset", 0)- opts.Max = number.Min(p.IntOr("size", 10), 500)+ opts.Max = min(p.IntOr("size", 10), 500)albums, err := api.ds.Album(r.Context()).GetAllWithoutGenres(opts)if err != nil {@@ -165,7 +164,7 @@ func (api *Router) GetNowPlaying(r *http.Request) (*responses.Subsonic, error) {func (api *Router) GetRandomSongs(r *http.Request) (*responses.Subsonic, error) {p := req.Params(r)- size := number.Min(p.IntOr("size", 10), 500)+ size := min(p.IntOr("size", 10), 500)genre, _ := p.String("genre")fromYear := p.IntOr("fromYear", 0)toYear := p.IntOr("toYear", 0)@@ -184,7 +183,7 @@ func (api *Router) GetRandomSongs(r *http.Request) (*responses.Subsonic, error)func (api *Router) GetSongsByGenre(r *http.Request) (*responses.Subsonic, error) {p := req.Params(r)- count := number.Min(p.IntOr("count", 10), 500)+ count := min(p.IntOr("count", 10), 500)offset := p.IntOr("offset", 0)genre, _ := p.String("genre")diff --git a/utils/gravatar/gravatar.go b/utils/gravatar/gravatar.goindex ea18212b9..e0c7fe68e 100644--- a/utils/gravatar/gravatar.go+++ b/utils/gravatar/gravatar.go@@ -4,8 +4,6 @@ import ("crypto/md5""fmt""strings"-- "github.com/navidrome/navidrome/utils/number")const baseUrl = "https://www.gravatar.com/avatar"@@ -19,7 +17,7 @@ func Url(email string, size int) string {if size < 1 {size = defaultSize}- size = number.Min(maxSize, size)+ size = min(maxSize, size)return fmt.Sprintf("%s/%x?s=%d", baseUrl, hash, size)}diff --git a/utils/number/number.go b/utils/number/number.goindex 1ae4b9c59..0d10dfb01 100644--- a/utils/number/number.go+++ b/utils/number/number.go@@ -3,40 +3,8 @@ package numberimport ("crypto/rand""math/big"-- "golang.org/x/exp/constraints")-// TODO Remove on Go 1.22, in favor of builtin `min` function.-func Min[T constraints.Ordered](vs ...T) T {- if len(vs) == 0 {- var zero T- return zero- }- min := vs[0]- for _, v := range vs[1:] {- if v < min {- min = v- }- }- return min-}--// TODO Remove on Go 1.22, in favor of builtin `max` function.-func Max[T constraints.Ordered](vs ...T) T {- if len(vs) == 0 {- var zero T- return zero- }- max := vs[0]- for _, v := range vs[1:] {- if v > max {- max = v- }- }- return max-}-func RandomInt64(max int64) int64 {rnd, _ := rand.Int(rand.Reader, big.NewInt(max))return rnd.Int64()diff --git a/utils/number/number_test.go b/utils/number/number_test.goindex d4c34ae7b..ad597b7a8 100644--- a/utils/number/number_test.go+++ b/utils/number/number_test.go@@ -13,26 +13,10 @@ func TestNumber(t *testing.T) {RunSpecs(t, "Number Suite")}-var _ = Describe("Min", func() {- It("returns zero value if no arguments are passed", func() {- Expect(number.Min[int]()).To(BeZero())- })- It("returns the smallest int", func() {- Expect(number.Min(1, 2)).To(Equal(1))- })- It("returns the smallest float", func() {- Expect(number.Min(-4.1, -4.2, -4.0)).To(Equal(-4.2))- })-})--var _ = Describe("Max", func() {- It("returns zero value if no arguments are passed", func() {- Expect(number.Max[int]()).To(BeZero())- })- It("returns the biggest int", func() {- Expect(number.Max(1, 2)).To(Equal(2))- })- It("returns the biggest float", func() {- Expect(number.Max(-4.1, -4.2, -4.0)).To(Equal(-4.0))+var _ = Describe("RandomInt64", func() {+ It("should return a random int64", func() {+ for i := 0; i < 10000; i++ {+ Expect(number.RandomInt64(100)).To(BeNumerically("<", 100))+ }})})