mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-09-08 15:41:18 +02:00
added Store.Keys()
This commit is contained in:
+15
-1
@@ -122,7 +122,21 @@ func (s *Store[K, T]) GetAll() map[K]T {
|
||||
return clone
|
||||
}
|
||||
|
||||
// Values returns a slice with all of the current store values.
|
||||
// Keys returns a slice with all of the store keys.
|
||||
func (s *Store[K, T]) Keys() []K {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var keys = make([]K, 0, len(s.data))
|
||||
|
||||
for k := range s.data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Values returns a slice with all of the store values.
|
||||
func (s *Store[K, T]) Values() []T {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
@@ -205,6 +205,27 @@ func TestGetAll(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
data := map[string]int{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}
|
||||
|
||||
keys := store.New(data).Keys()
|
||||
|
||||
expected := []string{"a", "b"}
|
||||
|
||||
if len(keys) != len(expected) {
|
||||
t.Fatalf("Expected %d keys, got %d", len(expected), len(keys))
|
||||
}
|
||||
|
||||
for _, k := range expected {
|
||||
if !slices.Contains(keys, k) {
|
||||
t.Fatalf("Missing key %s in\n%v", k, keys)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValues(t *testing.T) {
|
||||
data := map[string]int{
|
||||
"a": 1,
|
||||
|
||||
Reference in New Issue
Block a user