diff --git a/commands.go b/commands.go index 887fd73..b07a804 100644 --- a/commands.go +++ b/commands.go @@ -1094,8 +1094,14 @@ type ZRangeByScore struct { Offset, Count int64 } -func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd { - args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max} +func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores, isLex bool) *StringSliceCmd { + var zcmd string + if isLex { + zcmd = "ZRANGEBYLEX" + } else { + zcmd = "ZRANGEBYSCORE" + } + args := []interface{}{zcmd, key, opt.Min, opt.Max} if withScores { args = append(args, "WITHSCORES") } @@ -1113,7 +1119,11 @@ func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bo } func (c *commandable) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd { - return c.zRangeByScore(key, opt, false) + return c.zRangeByScore(key, opt, false, false) +} + +func (c *commandable) ZRangeByLex(key string, opt ZRangeByScore) *StringSliceCmd { + return c.zRangeByScore(key, opt, false, true) } func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { diff --git a/commands_test.go b/commands_test.go index 6cd9fc0..d0dbe43 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2010,6 +2010,42 @@ var _ = Describe("Commands", func() { Expect(zRangeByScore.Val()).To(Equal([]string{})) }) + It("should ZRangeByLex", func() { + zAdd := client.ZAdd("zset", redis.Z{0, "a"}) + Expect(zAdd.Err()).NotTo(HaveOccurred()) + zAdd = client.ZAdd("zset", redis.Z{0, "b"}) + Expect(zAdd.Err()).NotTo(HaveOccurred()) + zAdd = client.ZAdd("zset", redis.Z{0, "c"}) + + zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeByScore{ + Min: "-", + Max: "+", + }) + Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) + Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"})) + + zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ + Min: "[a", + Max: "[b", + }) + Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) + Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"})) + + zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ + Min: "(a", + Max: "[b", + }) + Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) + Expect(zRangeByLex.Val()).To(Equal([]string{"b"})) + + zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ + Min: "(a", + Max: "(b", + }) + Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) + Expect(zRangeByLex.Val()).To(Equal([]string{})) + }) + It("should ZRangeByScoreWithScoresMap", func() { zAdd := client.ZAdd("zset", redis.Z{1, "one"}) Expect(zAdd.Err()).NotTo(HaveOccurred())