From 5056952239d58567d66c70838191b98a2956437e Mon Sep 17 00:00:00 2001 From: Jeff Howell Date: Mon, 25 Nov 2024 10:56:28 -0800 Subject: [PATCH] added support for ZRank and ZRevRank with score --- cmd_sorted_set.go | 17 ++++++++++++++++- cmd_sorted_set_test.go | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd_sorted_set.go b/cmd_sorted_set.go index e92c82a..86e4dfd 100644 --- a/cmd_sorted_set.go +++ b/cmd_sorted_set.go @@ -825,6 +825,12 @@ func (m *Miniredis) makeCmdZrank(reverse bool) server.Cmd { withTx(m, c, func(c *server.Peer, ctx *connCtx) { db := m.db(ctx.selectedDB) + withScore := false + if len(args) > 0 && strings.ToUpper(args[len(args)-1]) == "WITHSCORE" { + withScore = true + args = args[:len(args)-1] + } + if len(args) > 2 { setDirty(c) c.WriteError(msgSyntaxError) @@ -850,7 +856,16 @@ func (m *Miniredis) makeCmdZrank(reverse bool) server.Cmd { c.WriteNull() return } - c.WriteInt(rank) + + if withScore { + c.WriteLen(2) + c.WriteInt(rank) + c.WriteFloat(db.ssetScore(key, member)) + } else { + c.WriteInt(rank) + } + + }) } } diff --git a/cmd_sorted_set_test.go b/cmd_sorted_set_test.go index 670110e..a4040d6 100644 --- a/cmd_sorted_set_test.go +++ b/cmd_sorted_set_test.go @@ -30,10 +30,27 @@ func TestSortedSet(t *testing.T) { proto.Int(2), ) + mustDo(t, c, + "ZRANK", "z", "three", "withscore", + proto.Array( + proto.Int(2), + proto.String("3"), + ), + ) + mustDo(t, c, "ZREVRANK", "z", "one", proto.Int(2), ) + + mustDo(t, c, + "ZREVRANK", "z", "one", "withscore", + proto.Array( + proto.Int(2), + proto.String("1"), + ), + ) + must0(t, c, "ZREVRANK", "z", "three", )