feat: upgrade to math/rand/v2 (#483)

* feat: upgrade to math/rand/v2

* lint: silent warning
This commit is contained in:
Samuel Berthe
2024-06-30 03:54:55 +02:00
committed by GitHub
parent 0a145c7cc7
commit 3b29b7d918
6 changed files with 38 additions and 19 deletions

View File

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

10
find.go
View File

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

View File

@@ -0,0 +1,14 @@
//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 {
// bearer:disable go_gosec_crypto_weak_random
return rand.Intn(n)
}

View File

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

View File

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

View File

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