From 51d7f3e889505316506588d6a5d94c100ec9de5b Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Sun, 30 Jun 2024 03:51:48 +0200 Subject: [PATCH 1/2] feat: upgrade to math/rand/v2 --- channel.go | 11 ++++------- find.go | 10 +++------- internal/rand/ordered_go118.go | 13 +++++++++++++ internal/rand/ordered_go122.go | 13 +++++++++++++ slice.go | 2 +- string.go | 7 +++---- 6 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 internal/rand/ordered_go118.go create mode 100644 internal/rand/ordered_go122.go diff --git a/channel.go b/channel.go index 2ffdb381..228705ae 100644 --- a/channel.go +++ b/channel.go @@ -1,9 +1,10 @@ package lo import ( - "math/rand" "sync" "time" + + "github.com/samber/lo/internal/rand" ) type DispatchingStrategy[T any] func(msg T, index uint64, channels []<-chan T) int @@ -86,9 +87,7 @@ func DispatchingStrategyRoundRobin[T any](msg T, index uint64, channels []<-chan // If the channel capacity is exceeded, another random channel will be selected and so on. func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T) int { for { - // @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22. - // bearer:disable go_gosec_crypto_weak_random - i := rand.Intn(len(channels)) + i := rand.IntN(len(channels)) if channelIsNotFull(channels[i]) { return i } @@ -110,9 +109,7 @@ func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy return func(msg T, index uint64, channels []<-chan T) int { for { - // @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22. - // bearer:disable go_gosec_crypto_weak_random - i := seq[rand.Intn(len(seq))] + i := seq[rand.IntN(len(seq))] if channelIsNotFull(channels[i]) { return i } diff --git a/find.go b/find.go index a9f43a4d..ff708b5e 100644 --- a/find.go +++ b/find.go @@ -2,10 +2,10 @@ package lo import ( "fmt" - "math/rand" "time" "github.com/samber/lo/internal/constraints" + "github.com/samber/lo/internal/rand" ) // IndexOf returns the index at which the first occurrence of a value is found in an array or return -1 @@ -432,9 +432,7 @@ func Sample[T any](collection []T) T { return Empty[T]() } - // @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22. - // bearer:disable go_gosec_crypto_weak_random - return collection[rand.Intn(size)] + return collection[rand.IntN(size)] } // Samples returns N random unique items from collection. @@ -448,9 +446,7 @@ func Samples[T any, Slice ~[]T](collection Slice, count int) Slice { for i := 0; i < size && i < count; i++ { copyLength := size - i - // @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22. - // bearer:disable go_gosec_crypto_weak_random - index := rand.Intn(size - i) + index := rand.IntN(size - i) results = append(results, copy[index]) // Removes element. diff --git a/internal/rand/ordered_go118.go b/internal/rand/ordered_go118.go new file mode 100644 index 00000000..07729edf --- /dev/null +++ b/internal/rand/ordered_go118.go @@ -0,0 +1,13 @@ +//go:build !go1.22 + +package rand + +import "math/rand" + +func Shuffle(n int, swap func(i, j int)) { + rand.Shuffle(n, swap) +} + +func IntN(n int) int { + return rand.Intn(n) +} diff --git a/internal/rand/ordered_go122.go b/internal/rand/ordered_go122.go new file mode 100644 index 00000000..532ed339 --- /dev/null +++ b/internal/rand/ordered_go122.go @@ -0,0 +1,13 @@ +//go:build go1.22 + +package rand + +import "math/rand/v2" + +func Shuffle(n int, swap func(i, j int)) { + rand.Shuffle(n, swap) +} + +func IntN(n int) int { + return rand.IntN(n) +} diff --git a/slice.go b/slice.go index 5e8c1555..bf69d3b0 100644 --- a/slice.go +++ b/slice.go @@ -2,9 +2,9 @@ package lo import ( "sort" - "math/rand" "github.com/samber/lo/internal/constraints" + "github.com/samber/lo/internal/rand" ) // Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for. diff --git a/string.go b/string.go index 2606ec86..6d66ca15 100644 --- a/string.go +++ b/string.go @@ -1,12 +1,13 @@ package lo import ( - "math/rand" "regexp" "strings" "unicode" "unicode/utf8" + "github.com/samber/lo/internal/rand" + "golang.org/x/text/cases" "golang.org/x/text/language" ) @@ -39,9 +40,7 @@ func RandomString(size int, charset []rune) string { b := make([]rune, size) possibleCharactersCount := len(charset) for i := range b { - // @TODO: Upgrade to math/rand/v2 as soon as we set the minimum Go version to 1.22. - // bearer:disable go_gosec_crypto_weak_random - b[i] = charset[rand.Intn(possibleCharactersCount)] + b[i] = charset[rand.IntN(possibleCharactersCount)] } return string(b) } From d4f0cd4f6b21c8b77265d257088195acd9a4c68c Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Sun, 30 Jun 2024 03:53:47 +0200 Subject: [PATCH 2/2] lint: silent warning --- internal/rand/ordered_go118.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/rand/ordered_go118.go b/internal/rand/ordered_go118.go index 07729edf..a31bb9f2 100644 --- a/internal/rand/ordered_go118.go +++ b/internal/rand/ordered_go118.go @@ -9,5 +9,6 @@ func Shuffle(n int, swap func(i, j int)) { } func IntN(n int) int { + // bearer:disable go_gosec_crypto_weak_random return rand.Intn(n) }