Use Go builtin min/max func

This commit is contained in:
Deluan 2024-02-16 21:48:25 -05:00
parent f7a4387d0e
commit 166eb37787
8 changed files with 18 additions and 74 deletions

View File

@ -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)

View File

@ -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 image
bounds := 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
}

View File

@ -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 in
return 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)

View File

@ -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 {

View File

@ -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")

View File

@ -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)
}

View File

@ -3,40 +3,8 @@ package number
import (
"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()

View File

@ -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))
}
})
})