tijl.dev-core/internal/apps/flags/util.go
tijl d0e045974f
Some checks failed
build / build (push) Successful in 27s
release-tag / release-image (push) Has been cancelled
flags implementation
2024-08-26 16:57:28 +02:00

69 lines
1.3 KiB
Go

package flags
import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"math/rand"
)
func shuffleSliceRandom(slice []string) []string {
shuffled := make([]string, len(slice))
copy(shuffled, slice)
for i := len(shuffled) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
}
return shuffled
}
func shuffleSlice(slice []string, seed string) []string {
hasher := sha256.New()
hasher.Write([]byte(seed))
hash := hasher.Sum(nil)
seedInt := int64(binary.LittleEndian.Uint64(hash[:8]))
r := rand.New(rand.NewSource(seedInt))
shuffled := make([]string, len(slice))
copy(shuffled, slice)
r.Shuffle(len(shuffled), func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
return shuffled
}
func filterCountriesByTags(tags []string) (error, []string) {
var result []string
for _, tag := range tags {
isSupported := false
for _, supportedTag := range supportedTags {
if tag == supportedTag {
isSupported = true
}
}
if isSupported == false {
return errors.New(fmt.Sprintf("unsupported tag %v", tag)), result
}
}
for _, country := range countryCodes {
for _, tag := range tags {
for _, cTag := range country.Tags {
if cTag == tag {
result = append(result, country.Code)
}
}
}
}
return nil, result
}