Skip to content

Commit

Permalink
Change Delete() to return presence bool.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed Mar 18, 2024
1 parent 71abe46 commit b8100e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
15 changes: 11 additions & 4 deletions impl/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion impl/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 1 addition & 11 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
*/

0 comments on commit b8100e7

Please sign in to comment.