added Store.Keys()

This commit is contained in:
Gani Georgiev
2026-08-21 14:45:48 +03:00
parent bf1f164014
commit 25dc8a8eea
2 changed files with 36 additions and 1 deletions
+15 -1
View File
@@ -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()
+21
View File
@@ -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,