From 2de07f24930aaf7f4f0d6c34656fcf5229675bbd Mon Sep 17 00:00:00 2001 From: Jeff Pierce Date: Sat, 22 Aug 2015 20:38:37 -0700 Subject: [PATCH] Implemented ZRangeByLex with tests. --- commands.go | 16 +++++++++++++--- commands_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) 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())