Skip to content

Commit

Permalink
Add full parity for persistence of sections to keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed Jun 14, 2024
1 parent 05e0f3a commit a99424e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.22.0

require (
github.com/shimmeringbee/zcl v0.0.0-20240509210644-817a66d91348
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shimmeringbee/bytecodec v0.0.0-20201107142444-94bb5c0baaee // indirect
github.com/shimmeringbee/zigbee v0.0.0-20201027194100-4e53cafc0f7a // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 10 additions & 1 deletion impl/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ type memory struct {
sections map[string]persistence.Section
}

func (m *memory) SectionExists(key string) bool {
m.m.RLock()
defer m.m.RUnlock()

_, found := m.sections[key]

return found
}

func (m *memory) Section(key ...string) persistence.Section {
m.m.RLock()
s, ok := m.sections[key[0]]
Expand Down Expand Up @@ -48,7 +57,7 @@ func (m *memory) SectionKeys() []string {
return keys
}

func (m *memory) DeleteSection(key string) bool {
func (m *memory) SectionDelete(key string) bool {
m.m.Lock()
defer m.m.Unlock()

Expand Down
12 changes: 11 additions & 1 deletion impl/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestMemory_DeleteSection(t *testing.T) {
s.Section("one")
assert.Contains(t, s.SectionKeys(), "one")

s.DeleteSection("one")
s.SectionDelete("one")
assert.NotContains(t, s.SectionKeys(), "one")
})
}
Expand All @@ -267,6 +267,16 @@ func TestMemory_Exists(t *testing.T) {
})
}

func TestMemory_SectionExists(t *testing.T) {
t.Run("returns if a section exists", func(t *testing.T) {
s := New()

_ = s.Section("key")
assert.True(t, s.SectionExists("key"))
assert.False(t, s.SectionExists("otherKey"))
})
}

func TestMemory_SectionKeyNotClash(t *testing.T) {
t.Run("ensure that keys and sections dont shared the same name space", func(t *testing.T) {
s := New()
Expand Down
3 changes: 2 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package persistence
type Section interface {
Section(key ...string) Section
SectionKeys() []string
DeleteSection(key string) bool
SectionExists(key string) bool
SectionDelete(key string) bool

Keys() []string
Exists(key string) bool
Expand Down

0 comments on commit a99424e

Please sign in to comment.