Adding upstream version 0.28.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
88f1d47ab6
commit
e28c88ef14
933 changed files with 194711 additions and 0 deletions
163
tools/list/list.go
Normal file
163
tools/list/list.go
Normal file
|
@ -0,0 +1,163 @@
|
|||
package list
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/store"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
var cachedPatterns = store.New[string, *regexp.Regexp](nil)
|
||||
|
||||
// SubtractSlice returns a new slice with only the "base" elements
|
||||
// that don't exist in "subtract".
|
||||
func SubtractSlice[T comparable](base []T, subtract []T) []T {
|
||||
var result = make([]T, 0, len(base))
|
||||
|
||||
for _, b := range base {
|
||||
if !ExistInSlice(b, subtract) {
|
||||
result = append(result, b)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ExistInSlice checks whether a comparable element exists in a slice of the same type.
|
||||
func ExistInSlice[T comparable](item T, list []T) bool {
|
||||
for _, v := range list {
|
||||
if v == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 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!
|
||||
func ExistInSliceWithRegex(str string, list []string) bool {
|
||||
for _, field := range list {
|
||||
isRegex := strings.HasPrefix(field, "^") && strings.HasSuffix(field, "$")
|
||||
|
||||
if !isRegex {
|
||||
// check for direct match
|
||||
if str == field {
|
||||
return true
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// check for regex match
|
||||
pattern := cachedPatterns.Get(field)
|
||||
if pattern == nil {
|
||||
var err error
|
||||
pattern, err = regexp.Compile(field)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// "cache" the pattern to avoid compiling it every time
|
||||
// (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, 500)
|
||||
}
|
||||
|
||||
if pattern != nil && pattern.MatchString(str) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ToInterfaceSlice converts a generic slice to slice of interfaces.
|
||||
func ToInterfaceSlice[T any](list []T) []any {
|
||||
result := make([]any, len(list))
|
||||
|
||||
for i := range list {
|
||||
result[i] = list[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// NonzeroUniques returns only the nonzero unique values from a slice.
|
||||
func NonzeroUniques[T comparable](list []T) []T {
|
||||
result := make([]T, 0, len(list))
|
||||
existMap := make(map[T]struct{}, len(list))
|
||||
|
||||
var zeroVal T
|
||||
|
||||
for _, val := range list {
|
||||
if val == zeroVal {
|
||||
continue
|
||||
}
|
||||
if _, ok := existMap[val]; ok {
|
||||
continue
|
||||
}
|
||||
existMap[val] = struct{}{}
|
||||
result = append(result, val)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ToUniqueStringSlice casts `value` to a slice of non-zero unique strings.
|
||||
func ToUniqueStringSlice(value any) (result []string) {
|
||||
switch val := value.(type) {
|
||||
case nil:
|
||||
// nothing to cast
|
||||
case []string:
|
||||
result = val
|
||||
case string:
|
||||
if val == "" {
|
||||
break
|
||||
}
|
||||
|
||||
// check if it is a json encoded array of strings
|
||||
if strings.Contains(val, "[") {
|
||||
if err := json.Unmarshal([]byte(val), &result); err != nil {
|
||||
// not a json array, just add the string as single array element
|
||||
result = append(result, val)
|
||||
}
|
||||
} else {
|
||||
// just add the string as single array element
|
||||
result = append(result, val)
|
||||
}
|
||||
case json.Marshaler: // eg. JSONArray
|
||||
raw, _ := val.MarshalJSON()
|
||||
_ = json.Unmarshal(raw, &result)
|
||||
default:
|
||||
result = cast.ToStringSlice(value)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
310
tools/list/list_test.go
Normal file
310
tools/list/list_test.go
Normal file
|
@ -0,0 +1,310 @@
|
|||
package list_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/list"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestSubtractSliceString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
base []string
|
||||
subtract []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
[]string{},
|
||||
[]string{},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]string{},
|
||||
[]string{"1", "2", "3", "4"},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]string{"1", "2", "3", "4"},
|
||||
[]string{},
|
||||
`["1","2","3","4"]`,
|
||||
},
|
||||
{
|
||||
[]string{"1", "2", "3", "4"},
|
||||
[]string{"1", "2", "3", "4"},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]string{"1", "2", "3", "4", "7"},
|
||||
[]string{"2", "4", "5", "6"},
|
||||
`["1","3","7"]`,
|
||||
},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%s", i, s.expected), func(t *testing.T) {
|
||||
result := list.SubtractSlice(s.base, s.subtract)
|
||||
|
||||
raw, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to serialize: %v", err)
|
||||
}
|
||||
|
||||
strResult := string(raw)
|
||||
|
||||
if strResult != s.expected {
|
||||
t.Fatalf("Expected %v, got %v", s.expected, strResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubtractSliceInt(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
base []int
|
||||
subtract []int
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
[]int{},
|
||||
[]int{},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]int{},
|
||||
[]int{1, 2, 3, 4},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]int{1, 2, 3, 4},
|
||||
[]int{},
|
||||
`[1,2,3,4]`,
|
||||
},
|
||||
{
|
||||
[]int{1, 2, 3, 4},
|
||||
[]int{1, 2, 3, 4},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]int{1, 2, 3, 4, 7},
|
||||
[]int{2, 4, 5, 6},
|
||||
`[1,3,7]`,
|
||||
},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%s", i, s.expected), func(t *testing.T) {
|
||||
result := list.SubtractSlice(s.base, s.subtract)
|
||||
|
||||
raw, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to serialize: %v", err)
|
||||
}
|
||||
|
||||
strResult := string(raw)
|
||||
|
||||
if strResult != s.expected {
|
||||
t.Fatalf("Expected %v, got %v", s.expected, strResult)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistInSliceString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item string
|
||||
list []string
|
||||
expected bool
|
||||
}{
|
||||
{"", []string{""}, true},
|
||||
{"", []string{"1", "2", "test 123"}, false},
|
||||
{"test", []string{}, false},
|
||||
{"test", []string{"TEST"}, false},
|
||||
{"test", []string{"1", "2", "test 123"}, false},
|
||||
{"test", []string{"1", "2", "test"}, true},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%s", i, s.item), func(t *testing.T) {
|
||||
result := list.ExistInSlice(s.item, s.list)
|
||||
if result != s.expected {
|
||||
t.Fatalf("Expected %v, got %v", s.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistInSliceInt(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item int
|
||||
list []int
|
||||
expected bool
|
||||
}{
|
||||
{0, []int{}, false},
|
||||
{0, []int{0}, true},
|
||||
{4, []int{1, 2, 3}, false},
|
||||
{1, []int{1, 2, 3}, true},
|
||||
{-1, []int{0, 1, 2, 3}, false},
|
||||
{-1, []int{0, -1, -2, -3, -4}, true},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%d", i, s.item), func(t *testing.T) {
|
||||
result := list.ExistInSlice(s.item, s.list)
|
||||
if result != s.expected {
|
||||
t.Fatalf("Expected %v, got %v", s.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistInSliceWithRegex(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item string
|
||||
list []string
|
||||
expected bool
|
||||
}{
|
||||
{"", []string{``}, true},
|
||||
{"", []string{`^\W+$`}, false},
|
||||
{" ", []string{`^\W+$`}, true},
|
||||
{"test", []string{`^\invalid[+$`}, false}, // invalid regex
|
||||
{"test", []string{`^\W+$`, "test"}, true},
|
||||
{`^\W+$`, []string{`^\W+$`, "test"}, false}, // direct match shouldn't work for this case
|
||||
{`\W+$`, []string{`\W+$`, "test"}, true}, // direct match should work for this case because it is not an actual supported pattern format
|
||||
{"!?@", []string{`\W+$`, "test"}, false}, // the method requires the pattern elems to start with '^'
|
||||
{"!?@", []string{`^\W+`, "test"}, false}, // the method requires the pattern elems to end with '$'
|
||||
{"!?@", []string{`^\W+$`, "test"}, true},
|
||||
{"!?@test", []string{`^\W+$`, "test"}, false},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%s", i, s.item), func(t *testing.T) {
|
||||
result := list.ExistInSliceWithRegex(s.item, s.list)
|
||||
if result != s.expected {
|
||||
t.Fatalf("Expected %v, got %v", s.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInterfaceSlice(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
items []string
|
||||
}{
|
||||
{[]string{}},
|
||||
{[]string{""}},
|
||||
{[]string{"1", "test"}},
|
||||
{[]string{"test1", "test1", "test2", "test3"}},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%#v", i, s.items), func(t *testing.T) {
|
||||
result := list.ToInterfaceSlice(s.items)
|
||||
|
||||
if len(result) != len(s.items) {
|
||||
t.Fatalf("Expected length %d, got %d", len(s.items), len(result))
|
||||
}
|
||||
|
||||
for j, v := range result {
|
||||
if v != s.items[j] {
|
||||
t.Fatalf("Result list item doesn't match with the original list item, got %v VS %v", v, s.items[j])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonzeroUniquesString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
items []string
|
||||
expected []string
|
||||
}{
|
||||
{[]string{}, []string{}},
|
||||
{[]string{""}, []string{}},
|
||||
{[]string{"1", "test"}, []string{"1", "test"}},
|
||||
{[]string{"test1", "", "test2", "Test2", "test1", "test3"}, []string{"test1", "test2", "Test2", "test3"}},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%#v", i, s.items), func(t *testing.T) {
|
||||
result := list.NonzeroUniques(s.items)
|
||||
|
||||
if len(result) != len(s.expected) {
|
||||
t.Fatalf("Expected length %d, got %d", len(s.expected), len(result))
|
||||
}
|
||||
|
||||
for j, v := range result {
|
||||
if v != s.expected[j] {
|
||||
t.Fatalf("Result list item doesn't match with the expected list item, got %v VS %v", v, s.expected[j])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToUniqueStringSlice(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
value any
|
||||
expected []string
|
||||
}{
|
||||
{nil, []string{}},
|
||||
{"", []string{}},
|
||||
{[]any{}, []string{}},
|
||||
{[]int{}, []string{}},
|
||||
{"test", []string{"test"}},
|
||||
{[]int{1, 2, 3}, []string{"1", "2", "3"}},
|
||||
{[]any{0, 1, "test", ""}, []string{"0", "1", "test"}},
|
||||
{[]string{"test1", "test2", "test1"}, []string{"test1", "test2"}},
|
||||
{`["test1", "test2", "test2"]`, []string{"test1", "test2"}},
|
||||
{types.JSONArray[string]{"test1", "test2", "test1"}, []string{"test1", "test2"}},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%#v", i, s.value), func(t *testing.T) {
|
||||
result := list.ToUniqueStringSlice(s.value)
|
||||
|
||||
if len(result) != len(s.expected) {
|
||||
t.Fatalf("Expected length %d, got %d", len(s.expected), len(result))
|
||||
}
|
||||
|
||||
for j, v := range result {
|
||||
if v != s.expected[j] {
|
||||
t.Fatalf("Result list item doesn't match with the expected list item, got %v vs %v", v, s.expected[j])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToChunks(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
items []any
|
||||
chunkSize int
|
||||
expected string
|
||||
}{
|
||||
{nil, 2, "[]"},
|
||||
{[]any{}, 2, "[]"},
|
||||
{[]any{1, 2, 3, 4}, -1, "[[1],[2],[3],[4]]"},
|
||||
{[]any{1, 2, 3, 4}, 0, "[[1],[2],[3],[4]]"},
|
||||
{[]any{1, 2, 3, 4}, 2, "[[1,2],[3,4]]"},
|
||||
{[]any{1, 2, 3, 4, 5}, 2, "[[1,2],[3,4],[5]]"},
|
||||
{[]any{1, 2, 3, 4, 5}, 10, "[[1,2,3,4,5]]"},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
t.Run(fmt.Sprintf("%d_%#v", i, s.items), func(t *testing.T) {
|
||||
result := list.ToChunks(s.items, s.chunkSize)
|
||||
|
||||
raw, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rawStr := string(raw)
|
||||
|
||||
if rawStr != s.expected {
|
||||
t.Fatalf("Expected %v, got %v", s.expected, rawStr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue