1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-12 22:07:47 +02:00

fix: prevent returning empty strings for list (#2553)

This commit is contained in:
David Bereza
2022-09-06 12:03:23 -07:00
committed by GitHub
parent a702a1b097
commit 34b6434791

View File

@ -121,29 +121,27 @@ func (m *memoryStore) delete(prefix, key string) {
func (m *memoryStore) list(prefix string, limit, offset uint) []string { func (m *memoryStore) list(prefix string, limit, offset uint) []string {
allItems := m.store.Items() allItems := m.store.Items()
allKeys := make([]string, len(allItems)) foundKeys := make([]string, 0, len(allItems))
i := 0
for k := range allItems { for k := range allItems {
if !strings.HasPrefix(k, prefix+"/") { if !strings.HasPrefix(k, prefix+"/") {
continue continue
} }
allKeys[i] = strings.TrimPrefix(k, prefix+"/") foundKeys = append(foundKeys, strings.TrimPrefix(k, prefix+"/"))
i++
} }
if limit != 0 || offset != 0 { 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 { min := func(i, j uint) uint {
if i < j { if i < j {
return i return i
} }
return j 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 { func (m *memoryStore) Close() error {