Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: handle non-existent keys properly in Store.SMembers #111

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pkg/redis/redisstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@ func (rs *Store[Key, Value]) SetExpirable(ctx context.Context, key Key, expires
}

// Members returns all deserialized set values from redis.
// If the key does not exist, it returns ErrKeyNotFound.
func (rs *Store[Key, Value]) Members(ctx context.Context, key Key) ([]Value, error) {
data, err := rs.client.SMembers(ctx, rs.keyString(key)).Result()
if err != nil {
if err == redis.Nil {
return nil, types.ErrKeyNotFound
}
return nil, fmt.Errorf("getting set members: %w", err)
}

// as opposed to other commands, SMembers doesn't return redis.Nil when the key doesn't exist, but an empty set
// this implementation assumes there is no need to differentiate between a non-existing key and an empty set
if len(data) == 0 {
return nil, types.ErrKeyNotFound
}

var values []Value
for _, d := range data {
v, err := rs.fromRedis(d)
Expand Down
3 changes: 2 additions & 1 deletion pkg/redis/redisstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ func (m *MockRedis) SMembers(ctx context.Context, key string) *goredis.StringSli
}
val, ok := m.data[key]
if !ok {
cmd.SetErr(goredis.Nil)
// SMembers returns an empty set for non-existing keys
cmd.SetVal([]string{})
} else {
values := slices.Collect(maps.Keys(val.data))
cmd.SetVal(values)
Expand Down