Starting to implement permissions into frontend
This commit is contained in:
14
backend/utils/for_each.go
Normal file
14
backend/utils/for_each.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package utils
|
||||
|
||||
func Find[T comparable](arr []T, callback func(v T, index int) bool) *T {
|
||||
var result *T = nil
|
||||
|
||||
for i := 0; i < len(arr); i++ {
|
||||
if stop := callback(arr[i], i); stop {
|
||||
result = &arr[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
11
backend/utils/merge_arrays.go
Normal file
11
backend/utils/merge_arrays.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package utils
|
||||
|
||||
func MergeArrays[K comparable](arrays ...[]K) []K {
|
||||
merged := make([]K, 0)
|
||||
|
||||
for _, v := range arrays {
|
||||
merged = append(merged, v...)
|
||||
}
|
||||
|
||||
return merged
|
||||
}
|
||||
64
backend/utils/merge_arrays_test.go
Normal file
64
backend/utils/merge_arrays_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMergeArrays(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arrays interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
// Integer arrays
|
||||
{"Empty arrays", [][]int{}, []int{}},
|
||||
{"Single array", [][]int{{1, 2, 3}}, []int{1, 2, 3}},
|
||||
{"Two arrays", [][]int{{1, 2}, {3, 4}}, []int{1, 2, 3, 4}},
|
||||
{"Multiple arrays", [][]int{{1}, {2}, {3, 4}}, []int{1, 2, 3, 4}},
|
||||
{"Array with duplicates", [][]int{{1, 2}, {2, 3}}, []int{1, 2, 2, 3}},
|
||||
|
||||
// String arrays
|
||||
{"String arrays", [][]string{{"a", "b"}, {"c", "d"}}, []string{"a", "b", "c", "d"}},
|
||||
{"String arrays with duplicates", [][]string{{"hello", "world"}, {"world", "Go"}}, []string{"hello", "world", "world", "Go"}},
|
||||
|
||||
// Struct arrays
|
||||
{"Struct arrays", [][]Person{
|
||||
{{Name: "Alice", Age: 30}, {Name: "Bob", Age: 25}},
|
||||
{{Name: "Charlie", Age: 40}},
|
||||
}, []Person{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 40},
|
||||
}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
switch v := tt.arrays.(type) {
|
||||
case [][]int:
|
||||
result := MergeArrays(v...)
|
||||
if !reflect.DeepEqual(result, tt.expected.([]int)) {
|
||||
t.Errorf("MergeArrays(%v) = %v; want %v", v, result, tt.expected)
|
||||
}
|
||||
case [][]string:
|
||||
result := MergeArrays(v...)
|
||||
if !reflect.DeepEqual(result, tt.expected.([]string)) {
|
||||
t.Errorf("MergeArrays(%v) = %v; want %v", v, result, tt.expected)
|
||||
}
|
||||
case [][]Person:
|
||||
result := MergeArrays(v...)
|
||||
if !reflect.DeepEqual(result, tt.expected.([]Person)) {
|
||||
t.Errorf("MergeArrays(%v) = %v; want %v", v, result, tt.expected)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("Unsupported test case type: %T", v)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user