Skip to content

Commit

Permalink
Merge pull request #1 from dbereza-machinify/memory-store-list-empty-fix
Browse files Browse the repository at this point in the history
fix: prevent returning empty strings for list
  • Loading branch information
dbereza-machinify authored Sep 6, 2022
2 parents a702a1b + d11ac30 commit b5d027f
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions store/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,27 @@ func (m *memoryStore) delete(prefix, key string) {

func (m *memoryStore) list(prefix string, limit, offset uint) []string {
allItems := m.store.Items()
allKeys := make([]string, len(allItems))
i := 0
foundKeys := make([]string, 0, len(allItems))

for k := range allItems {
if !strings.HasPrefix(k, prefix+"/") {
continue
}
allKeys[i] = strings.TrimPrefix(k, prefix+"/")
i++
foundKeys = append(foundKeys, strings.TrimPrefix(k, prefix+"/"))
}

if limit != 0 || offset != 0 {
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
sort.Slice(foundKeys, func(i, j int) bool { return foundKeys[i] < foundKeys[j] })
min := func(i, j uint) uint {
if i < j {
return i
}
return j
}
return allKeys[offset:min(limit, uint(len(allKeys)))]
return foundKeys[offset:min(limit, uint(len(foundKeys)))]
}

return allKeys
return foundKeys
}

func (m *memoryStore) Close() error {
Expand Down

0 comments on commit b5d027f

Please sign in to comment.