merge v0.23.0-rc changes
This commit is contained in:
+26
-3
@@ -39,7 +39,7 @@ func ExistInSlice[T comparable](item T, list []T) bool {
|
||||
// ExistInSliceWithRegex checks whether a string exists in a slice
|
||||
// either by direct match, or by a regular expression (eg. `^\w+$`).
|
||||
//
|
||||
// _Note: Only list items starting with '^' and ending with '$' are treated as regular expressions!_
|
||||
// Note: Only list items starting with '^' and ending with '$' are treated as regular expressions!
|
||||
func ExistInSliceWithRegex(str string, list []string) bool {
|
||||
for _, field := range list {
|
||||
isRegex := strings.HasPrefix(field, "^") && strings.HasSuffix(field, "$")
|
||||
@@ -64,7 +64,7 @@ func ExistInSliceWithRegex(str string, list []string) bool {
|
||||
// (the limit size is arbitrary and it is there to prevent the cache growing too big)
|
||||
//
|
||||
// @todo consider replacing with TTL or LRU type cache
|
||||
cachedPatterns.SetIfLessThanLimit(field, pattern, 5000)
|
||||
cachedPatterns.SetIfLessThanLimit(field, pattern, 500)
|
||||
}
|
||||
|
||||
if pattern != nil && pattern.MatchString(str) {
|
||||
@@ -129,7 +129,7 @@ func ToUniqueStringSlice(value any) (result []string) {
|
||||
// just add the string as single array element
|
||||
result = append(result, val)
|
||||
}
|
||||
case json.Marshaler: // eg. JsonArray
|
||||
case json.Marshaler: // eg. JSONArray
|
||||
raw, _ := val.MarshalJSON()
|
||||
_ = json.Unmarshal(raw, &result)
|
||||
default:
|
||||
@@ -138,3 +138,26 @@ func ToUniqueStringSlice(value any) (result []string) {
|
||||
|
||||
return NonzeroUniques(result)
|
||||
}
|
||||
|
||||
// ToChunks splits list into chunks.
|
||||
//
|
||||
// Zero or negative chunkSize argument is normalized to 1.
|
||||
//
|
||||
// See https://go.dev/wiki/SliceTricks#batching-with-minimal-allocation.
|
||||
func ToChunks[T any](list []T, chunkSize int) [][]T {
|
||||
if chunkSize <= 0 {
|
||||
chunkSize = 1
|
||||
}
|
||||
|
||||
chunks := make([][]T, 0, (len(list)+chunkSize-1)/chunkSize)
|
||||
|
||||
if len(list) == 0 {
|
||||
return chunks
|
||||
}
|
||||
|
||||
for chunkSize < len(list) {
|
||||
list, chunks = list[chunkSize:], append(chunks, list[0:chunkSize:chunkSize])
|
||||
}
|
||||
|
||||
return append(chunks, list)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user