From 63f1a142d8bc885f8e96a0b891485db524b85a4b Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 30 Mar 2026 23:49:06 +0300 Subject: [PATCH] Fix couple nasty bugs in collection.go, safer type assertions. --- internal/lib/collection.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/lib/collection.go b/internal/lib/collection.go index 6a9cba3..adc366b 100644 --- a/internal/lib/collection.go +++ b/internal/lib/collection.go @@ -7,7 +7,6 @@ import ( // Collection is sync.Map, that stores arbitrary data. type Collection struct { items sync.Map - close chan struct{} } // Item just interface for stoing any data. @@ -17,9 +16,7 @@ type item struct { // NewCollection creates new instance of "collection". func NewCollection() *Collection { - c := &Collection{ //nolint:exhaustruct - close: make(chan struct{}), - } + c := &Collection{} return c } @@ -32,9 +29,12 @@ func (collection *Collection) Get(key any) (any, bool) { return nil, false } - item := obj.(item) + it, ok := obj.(item) + if !ok { + return nil, false + } - return item.data, true + return it.data, true } // Set saves value to given "collection". @@ -47,9 +47,12 @@ func (collection *Collection) Set(key any, value any) { // Range apply function f to all keys/values in collection. Retuns immediatly on first error. func (collection *Collection) Range(f func(key, value any) bool) { fn := func(key, value any) bool { - item := value.(item) + it, ok := value.(item) + if !ok { + return true + } - return f(key, item.data) + return f(key, it.data) } collection.items.Range(fn) @@ -62,7 +65,6 @@ func (collection *Collection) Delete(key any) { // Close frees memory occupied by collection and deletes collection itself. func (collection *Collection) Close() { - collection.close <- struct{}{} collection.items = sync.Map{} }