🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Refactor random functions
Deluan0ae294407325
diff --git a/core/external_metadata.go b/core/external_metadata.goindex 368e1cce1..6ff78dfcc 100644--- a/core/external_metadata.go+++ b/core/external_metadata.go@@ -18,6 +18,7 @@ import ("github.com/navidrome/navidrome/model""github.com/navidrome/navidrome/utils". "github.com/navidrome/navidrome/utils/gg"+ "github.com/navidrome/navidrome/utils/random""golang.org/x/sync/errgroup")@@ -266,8 +267,8 @@ func (e *externalMetadata) SimilarSongs(ctx context.Context, id string, count inreturn nil, ctx.Err()}- weightedSongs := utils.NewWeightedRandomChooser()- addArtist := func(a model.Artist, weightedSongs *utils.WeightedChooser, count, artistWeight int) error {+ weightedSongs := random.NewWeightedRandomChooser()+ addArtist := func(a model.Artist, weightedSongs *random.WeightedChooser, count, artistWeight int) error {if utils.IsCtxDone(ctx) {log.Warn(ctx, "SimilarSongs call canceled", ctx.Err())return ctx.Err()diff --git a/model/share.go b/model/share.goindex ba9e415a2..d1415a585 100644--- a/model/share.go+++ b/model/share.go@@ -4,7 +4,7 @@ import ("strings""time"- "github.com/navidrome/navidrome/utils/number"+ "github.com/navidrome/navidrome/utils/random")type Share struct {@@ -42,7 +42,7 @@ func (s Share) CoverArtID() ArtworkID {case "artist":return Artist{ID: ids[0]}.CoverArtID()}- rnd := number.RandomInt64(int64(len(s.Tracks)))+ rnd := random.Int64(int64(len(s.Tracks)))return s.Tracks[rnd].CoverArtID()}diff --git a/server/backgrounds/handler.go b/server/backgrounds/handler.goindex 611bba3f5..2d5af0bbb 100644--- a/server/backgrounds/handler.go+++ b/server/backgrounds/handler.go@@ -12,7 +12,7 @@ import ("github.com/navidrome/navidrome/consts""github.com/navidrome/navidrome/log"- "github.com/navidrome/navidrome/utils/number"+ "github.com/navidrome/navidrome/utils/random""gopkg.in/yaml.v3")@@ -51,7 +51,7 @@ func (h *Handler) getRandomImage(ctx context.Context) (string, error) {if len(list) == 0 {return "", errors.New("no images available")}- rnd := number.RandomInt64(int64(len(list)))+ rnd := random.Int64(int64(len(list)))return list[rnd], nil}diff --git a/utils/merge/merge_fs_test.go b/utils/merge/merge_fs_test.goindex e0cc18125..7241308d2 100644--- a/utils/merge/merge_fs_test.go+++ b/utils/merge/merge_fs_test.go@@ -5,12 +5,18 @@ import ("io/fs""os""path/filepath"+ "testing""github.com/navidrome/navidrome/utils/merge". "github.com/onsi/ginkgo/v2". "github.com/onsi/gomega")+func TestMergeFS(t *testing.T) {+ RegisterFailHandler(Fail)+ RunSpecs(t, "MergeFS Suite")+}+var _ = Describe("FS", func() {var baseName, overlayName stringvar mergedDir fs.FSdiff --git a/utils/number/number.go b/utils/number/number.goindex dc0bc19c4..5176a83e4 100644--- a/utils/number/number.go+++ b/utils/number/number.go@@ -1,18 +1,11 @@package numberimport (- "crypto/rand"- "math/big""strconv""golang.org/x/exp/constraints")-func RandomInt64(max int64) int64 {- rnd, _ := rand.Int(rand.Reader, big.NewInt(max))- return rnd.Int64()-}-func ParseInt[T constraints.Integer](s string) T {r, _ := strconv.ParseInt(s, 10, 64)return T(r)diff --git a/utils/number/number_test.go b/utils/number/number_test.goindex 5f94b4e90..a92f46fe8 100644--- a/utils/number/number_test.go+++ b/utils/number/number_test.go@@ -4,6 +4,7 @@ import ("testing""github.com/navidrome/navidrome/utils/number"+ "github.com/navidrome/navidrome/utils/random". "github.com/onsi/ginkgo/v2". "github.com/onsi/gomega")@@ -17,7 +18,7 @@ var _ = Describe("number package", func() {Describe("RandomInt64", func() {It("should return a random int64", func() {for i := 0; i < 10000; i++ {- Expect(number.RandomInt64(100)).To(BeNumerically("<", 100))+ Expect(random.Int64(100)).To(BeNumerically("<", 100))}})})diff --git a/utils/random/number.go b/utils/random/number.gonew file mode 100644index 000000000..68b04da79--- /dev/null+++ b/utils/random/number.go@@ -0,0 +1,11 @@+package random++import (+ "crypto/rand"+ "math/big"+)++func Int64(max int64) int64 {+ rnd, _ := rand.Int(rand.Reader, big.NewInt(max))+ return rnd.Int64()+}diff --git a/utils/random/number_test.go b/utils/random/number_test.gonew file mode 100644index 000000000..940f2934a--- /dev/null+++ b/utils/random/number_test.go@@ -0,0 +1,24 @@+package random_test++import (+ "testing"++ "github.com/navidrome/navidrome/utils/random"+ . "github.com/onsi/ginkgo/v2"+ . "github.com/onsi/gomega"+)++func TestRandom(t *testing.T) {+ RegisterFailHandler(Fail)+ RunSpecs(t, "Random Suite")+}++var _ = Describe("number package", func() {+ Describe("Int64", func() {+ It("should return a random int64", func() {+ for i := 0; i < 10000; i++ {+ Expect(random.Int64(100)).To(BeNumerically("<", 100))+ }+ })+ })+})diff --git a/utils/weighted_random_chooser.go b/utils/random/weighted_random_chooser.gosimilarity index 93%rename from utils/weighted_random_chooser.gorename to utils/random/weighted_random_chooser.goindex 0b4ab905b..f5f1a3117 100644--- a/utils/weighted_random_chooser.go+++ b/utils/random/weighted_random_chooser.go@@ -1,9 +1,7 @@-package utils+package randomimport ("errors"-- "github.com/navidrome/navidrome/utils/number")type WeightedChooser struct {@@ -41,7 +39,7 @@ func (w *WeightedChooser) weightedChoice() (int, error) {if w.totalWeight == 0 {return 0, errors.New("no choices available")}- rnd := number.RandomInt64(int64(w.totalWeight))+ rnd := Int64(int64(w.totalWeight))for i, weight := range w.weights {rnd -= int64(weight)if rnd < 0 {diff --git a/utils/weighted_random_chooser_test.go b/utils/random/weighted_random_chooser_test.gosimilarity index 98%rename from utils/weighted_random_chooser_test.gorename to utils/random/weighted_random_chooser_test.goindex 48c20ffab..c5b3b46ce 100644--- a/utils/weighted_random_chooser_test.go+++ b/utils/random/weighted_random_chooser_test.go@@ -1,4 +1,4 @@-package utils+package randomimport (. "github.com/onsi/ginkgo/v2"