Merge pull request from go-redis/fix/consistent-zrevrange

Use consistent API in ZRevRangeWithScores and ZRevRange. Fixes . Fixes .
This commit is contained in:
Vladimir Mihailenco 2015-04-04 14:51:05 +03:00
commit 4fefa47d6d
2 changed files with 18 additions and 35 deletions

View File

@ -10,6 +10,10 @@ func formatFloat(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
func formatInt(i int64) string {
return strconv.FormatInt(i, 10)
}
func readTimeout(sec int64) time.Duration {
if sec == 0 {
return 0
@ -919,7 +923,6 @@ func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
type ZRangeByScore struct {
Min, Max string
Offset, Count int64
}
@ -990,32 +993,20 @@ func (c *commandable) ZRemRangeByScore(key, min, max string) *IntCmd {
return cmd
}
func (c *commandable) zRevRange(key, start, stop string, withScores bool) *StringSliceCmd {
args := []string{"ZREVRANGE", key, start, stop}
if withScores {
args = append(args, "WITHSCORES")
}
cmd := NewStringSliceCmd(args...)
func (c *commandable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
cmd := NewStringSliceCmd("ZREVRANGE", key, formatInt(start), formatInt(stop))
c.Process(cmd)
return cmd
}
func (c *commandable) ZRevRange(key, start, stop string) *StringSliceCmd {
return c.zRevRange(key, start, stop, false)
}
func (c *commandable) ZRevRangeWithScores(key, start, stop string) *ZSliceCmd {
args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"}
cmd := NewZSliceCmd(args...)
func (c *commandable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
cmd := NewZSliceCmd("ZREVRANGE", key, formatInt(start), formatInt(stop), "WITHSCORES")
c.Process(cmd)
return cmd
}
func (c *commandable) zRevRangeByScore(key string, opt ZRangeByScore, withScores bool) *StringSliceCmd {
func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
if withScores {
args = append(args, "WITHSCORES")
}
if opt.Offset != 0 || opt.Count != 0 {
args = append(
args,
@ -1029,10 +1020,6 @@ func (c *commandable) zRevRangeByScore(key string, opt ZRangeByScore, withScores
return cmd
}
func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd {
return c.zRevRangeByScore(key, opt, false)
}
func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 {
@ -1060,12 +1047,8 @@ func (c *commandable) ZScore(key, member string) *FloatCmd {
return cmd
}
func (c *commandable) ZUnionStore(
destination string,
store ZStore,
keys ...string,
) *IntCmd {
args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
func (c *commandable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
args := []string{"ZUNIONSTORE", dest, strconv.FormatInt(int64(len(keys)), 10)}
args = append(args, keys...)
if len(store.Weights) > 0 {
args = append(args, "WEIGHTS")

View File

@ -2053,15 +2053,15 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred())
zRevRange := client.ZRevRange("zset", "0", "-1")
zRevRange := client.ZRevRange("zset", 0, -1)
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"}))
zRevRange = client.ZRevRange("zset", "2", "3")
zRevRange = client.ZRevRange("zset", 2, 3)
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"one"}))
zRevRange = client.ZRevRange("zset", "-2", "-1")
zRevRange = client.ZRevRange("zset", -2, -1)
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
})
@ -2074,15 +2074,15 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred())
val, err := client.ZRevRangeWithScores("zset", "0", "-1").Result()
val, err := client.ZRevRangeWithScores("zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}))
val, err = client.ZRevRangeWithScores("zset", "2", "3").Result()
val, err = client.ZRevRangeWithScores("zset", 2, 3).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{1, "one"}}))
val, err = client.ZRevRangeWithScores("zset", "-2", "-1").Result()
val, err = client.ZRevRangeWithScores("zset", -2, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{2, "two"}, {1, "one"}}))
})