From 25dc8a8eeaa7ca9d6cf138641a4ebdaeddb16f0d Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 21 Aug 2026 14:45:48 +0300 Subject: [PATCH] added Store.Keys() --- tools/store/store.go | 16 +++++++++++++++- tools/store/store_test.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tools/store/store.go b/tools/store/store.go index 087a6c39..1905bc08 100644 --- a/tools/store/store.go +++ b/tools/store/store.go @@ -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() diff --git a/tools/store/store_test.go b/tools/store/store_test.go index 8d9b88de..8a1e2d95 100644 --- a/tools/store/store_test.go +++ b/tools/store/store_test.go @@ -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,