Implemented ZRangeByLex with tests.

This commit is contained in:
Jeff Pierce 2015-08-22 20:38:37 -07:00
parent 5710d68852
commit 2de07f2493
2 changed files with 49 additions and 3 deletions

View File

@ -1094,8 +1094,14 @@ type ZRangeByScore struct {
Offset, Count int64 Offset, Count int64
} }
func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd { func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores, isLex bool) *StringSliceCmd {
args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max} var zcmd string
if isLex {
zcmd = "ZRANGEBYLEX"
} else {
zcmd = "ZRANGEBYSCORE"
}
args := []interface{}{zcmd, key, opt.Min, opt.Max}
if withScores { if withScores {
args = append(args, "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 { 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 { func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {

View File

@ -2010,6 +2010,42 @@ var _ = Describe("Commands", func() {
Expect(zRangeByScore.Val()).To(Equal([]string{})) 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() { It("should ZRangeByScoreWithScoresMap", func() {
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) zAdd := client.ZAdd("zset", redis.Z{1, "one"})
Expect(zAdd.Err()).NotTo(HaveOccurred()) Expect(zAdd.Err()).NotTo(HaveOccurred())