From 52fab9ef36d06e7cde3c6ea09d748c34eba5519d Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Mon, 18 Mar 2024 20:50:09 +0000 Subject: [PATCH] Change Delete() to return presence bool. --- impl/memory/memory.go | 15 +++++++++++---- impl/memory/memory_test.go | 8 +++++++- interface.go | 12 +----------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/impl/memory/memory.go b/impl/memory/memory.go index 6fa008d..63c404a 100644 --- a/impl/memory/memory.go +++ b/impl/memory/memory.go @@ -19,9 +19,9 @@ func (m *memory) Exists(key string) bool { m.m.RLock() defer m.m.RUnlock() - _, ok := m.kv[key] + _, found := m.kv[key] - return ok + return found } func (m *memory) Keys() []string { @@ -156,10 +156,17 @@ func (m *memory) Set(key string, value interface{}) error { return nil } -func (m *memory) Delete(key string) { +func (m *memory) Delete(key string) bool { m.m.Lock() defer m.m.Unlock() - delete(m.kv, key) + + _, found := m.kv[key] + + if found { + delete(m.kv, key) + } + + return found } var _ persistence.Section = (*memory)(nil) diff --git a/impl/memory/memory_test.go b/impl/memory/memory_test.go index 7075a0a..a1f285f 100644 --- a/impl/memory/memory_test.go +++ b/impl/memory/memory_test.go @@ -27,10 +27,16 @@ func TestMemory_Delete(t *testing.T) { assert.Contains(t, s.Keys(), "a") - s.Delete("a") + assert.True(t, s.Delete("a")) assert.NotContains(t, s.Keys(), "a") }) + + t.Run("returns false if key not present", func(t *testing.T) { + s := New() + + assert.False(t, s.Delete("a")) + }) } func TestMemory_Bool(t *testing.T) { diff --git a/interface.go b/interface.go index f54e5a4..0a08492 100644 --- a/interface.go +++ b/interface.go @@ -15,7 +15,7 @@ type Section interface { Set(key string, value interface{}) error - Delete(key string) + Delete(key string) bool } func StoreComplex[T any](section Section, key string, val T, enc func(Section, string, T) error) error { @@ -34,13 +34,3 @@ func RetrieveComplex[T any](section Section, key string, dec func(Section, strin } } } - -/* - - IEEEAddress(key string, defValue ...zigbee.IEEEAddress) (zigbee.IEEEAddress, bool) - ClusterID(key string, defValue ...zigbee.ClusterID) (zigbee.ClusterID, bool) - Endpoint(key string, defValue ...zigbee.Endpoint) (zigbee.Endpoint, bool) - AttributeID(key string, defValue ...zcl.AttributeID) (zcl.AttributeID, bool) - - As(key string, destValue any) bool -*/