Skip to content

Commit 210ae9a

Browse files
committed
Refactor with slices.Contains and simplify loops
1 parent bba5736 commit 210ae9a

4 files changed

Lines changed: 11 additions & 18 deletions

File tree

adapter/redis_compat_commands.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"log"
8+
"slices"
89
"sort"
910
"strconv"
1011
"strings"
@@ -697,11 +698,9 @@ func (r *RedisServer) sismember(conn redcon.Conn, cmd redcon.Command) {
697698
conn.WriteError(err.Error())
698699
return
699700
}
700-
for _, member := range value.Members {
701-
if member == string(cmd.Args[2]) {
702-
conn.WriteInt(1)
703-
return
704-
}
701+
if slices.Contains(value.Members, string(cmd.Args[2])) {
702+
conn.WriteInt(1)
703+
return
705704
}
706705
conn.WriteInt(0)
707706
}
@@ -2032,7 +2031,7 @@ func splitXReadStreams(args [][]byte, streamsIndex int) ([][]byte, []string, err
20322031
streamCount := remaining / redisPairWidth
20332032
keys := make([][]byte, streamCount)
20342033
afterIDs := make([]string, streamCount)
2035-
for i := 0; i < streamCount; i++ {
2034+
for i := range streamCount {
20362035
keys[i] = args[streamsIndex+i]
20372036
afterIDs[i] = string(args[streamsIndex+streamCount+i])
20382037
}

adapter/redis_compat_types.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,7 @@ func unmarshalStreamValue(raw []byte) (redisStreamValue, error) {
226226
}
227227

228228
func encodeRedisTTL(expireAt time.Time) []byte {
229-
ms := expireAt.UnixMilli()
230-
if ms < 0 {
231-
ms = 0
232-
}
229+
ms := max(expireAt.UnixMilli(), 0)
233230
buf := make([]byte, redisUint64Bytes)
234231
binary.BigEndian.PutUint64(buf, uint64(ms)) // #nosec G115 -- ms is clamped to non-negative int64 range.
235232
return buf
@@ -239,10 +236,7 @@ func decodeRedisTTL(raw []byte) (time.Time, error) {
239236
if len(raw) != redisUint64Bytes {
240237
return time.Time{}, errors.WithStack(errors.Newf("invalid ttl length %d", len(raw)))
241238
}
242-
ms := binary.BigEndian.Uint64(raw)
243-
if ms > math.MaxInt64 {
244-
ms = math.MaxInt64
245-
}
239+
ms := min(binary.BigEndian.Uint64(raw), math.MaxInt64)
246240
return time.UnixMilli(int64(ms)), nil // #nosec G115 -- ms <= MaxInt64 is guaranteed by the check above.
247241
}
248242

adapter/redis_misskey_compat_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestRedis_MisskeyAdminCompatibility(t *testing.T) {
138138

139139
ps := sub.Subscribe(ctx, "misskey.pubsub.one", "misskey.pubsub.two")
140140
defer func() { _ = ps.Close() }()
141-
for i := 0; i < 2; i++ {
141+
for range 2 {
142142
_, err = ps.ReceiveTimeout(ctx, 2*time.Second)
143143
require.NoError(t, err)
144144
}
@@ -268,7 +268,7 @@ func TestRedis_MisskeyDirectDataStructures(t *testing.T) {
268268
require.NoError(t, err)
269269
require.Len(t, xrev, 1)
270270
require.Equal(t, "1002-0", xrev[0].ID)
271-
require.Equal(t, map[string]interface{}{"data": `{"id":"n3"}`}, xrev[0].Values)
271+
require.Equal(t, map[string]any{"data": `{"id":"n3"}`}, xrev[0].Values)
272272

273273
xrange, err := rdb.XRange(ctx, "notificationTimeline:u1", "1001-0", "+").Result()
274274
require.NoError(t, err)

adapter/redis_retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ func (c *recordingConn) WriteAny(v any) {
134134
c.WriteNull()
135135
}
136136
}
137-
func (c *recordingConn) Context() interface{} { return c.ctx }
138-
func (c *recordingConn) SetContext(v interface{}) {
137+
func (c *recordingConn) Context() any { return c.ctx }
138+
func (c *recordingConn) SetContext(v any) {
139139
c.ctx = v
140140
}
141141
func (c *recordingConn) SetReadBuffer(bytes int) {}

0 commit comments

Comments
 (0)