1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 {