mirror of
https://github.com/aykhans/dodo.git
synced 2025-06-01 12:13:24 +00:00
🔧 Refactor 'RandomValueCycle' and 'getKeyValueGeneratorFunc' functions
This commit is contained in:
parent
23c74bdbb1
commit
e567155eb1
@ -203,19 +203,15 @@ func getKeyValueGeneratorFunc[
|
|||||||
isRandom := false
|
isRandom := false
|
||||||
|
|
||||||
for _, kv := range keyValueSlice {
|
for _, kv := range keyValueSlice {
|
||||||
valuesLen := len(kv.Value)
|
if valuesLen := len(kv.Value); valuesLen > 1 {
|
||||||
|
|
||||||
getValueFunc := func() string { return "" }
|
|
||||||
if valuesLen == 1 {
|
|
||||||
getValueFunc = func() string { return kv.Value[0] }
|
|
||||||
} else if valuesLen > 1 {
|
|
||||||
getValueFunc = utils.RandomValueCycle(kv.Value, localRand)
|
|
||||||
isRandom = true
|
isRandom = true
|
||||||
}
|
}
|
||||||
|
|
||||||
getKeyValueSlice = append(
|
getKeyValueSlice = append(
|
||||||
getKeyValueSlice,
|
getKeyValueSlice,
|
||||||
map[string]func() string{kv.Key: getValueFunc},
|
map[string]func() string{
|
||||||
|
kv.Key: utils.RandomValueCycle(kv.Value, localRand),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,24 +18,32 @@ func Flatten[T any](nested [][]*T) []*T {
|
|||||||
// have been returned at least once. After all values have been returned, the function will
|
// have been returned at least once. After all values have been returned, the function will
|
||||||
// reset and start cycling through the values in a random order again.
|
// reset and start cycling through the values in a random order again.
|
||||||
// The returned function isn't thread-safe and should be used in a single-threaded context.
|
// The returned function isn't thread-safe and should be used in a single-threaded context.
|
||||||
func RandomValueCycle[Value any](values []Value, localRand *rand.Rand) func() Value {
|
func RandomValueCycle[T any](values []T, localRand *rand.Rand) func() T {
|
||||||
var (
|
var (
|
||||||
clientsCount = len(values)
|
valuesLen = len(values)
|
||||||
currentIndex = localRand.Intn(clientsCount)
|
currentIndex = localRand.Intn(valuesLen)
|
||||||
stopIndex = currentIndex
|
stopIndex = currentIndex
|
||||||
)
|
)
|
||||||
|
|
||||||
return func() Value {
|
switch valuesLen {
|
||||||
client := values[currentIndex]
|
case 0:
|
||||||
currentIndex++
|
var zero T
|
||||||
if currentIndex == clientsCount {
|
return func() T { return zero }
|
||||||
currentIndex = 0
|
case 1:
|
||||||
}
|
return func() T { return values[0] }
|
||||||
if currentIndex == stopIndex {
|
default:
|
||||||
currentIndex = localRand.Intn(clientsCount)
|
return func() T {
|
||||||
stopIndex = currentIndex
|
value := values[currentIndex]
|
||||||
}
|
currentIndex++
|
||||||
|
if currentIndex == valuesLen {
|
||||||
|
currentIndex = 0
|
||||||
|
}
|
||||||
|
if currentIndex == stopIndex {
|
||||||
|
currentIndex = localRand.Intn(valuesLen)
|
||||||
|
stopIndex = currentIndex
|
||||||
|
}
|
||||||
|
|
||||||
return client
|
return value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user