Use consistent API in ZRevRangeWithScores and ZRevRange. Fixes #56. Fixes #78.

This commit is contained in:
Vladimir Mihailenco 2015-03-30 16:14:21 +03:00
parent 2dc0bd1c0a
commit 026823f334
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) return strconv.FormatFloat(f, 'f', -1, 64)
} }
func formatInt(i int64) string {
return strconv.FormatInt(i, 10)
}
func readTimeout(sec int64) time.Duration { func readTimeout(sec int64) time.Duration {
if sec == 0 { if sec == 0 {
return 0 return 0
@ -883,7 +887,6 @@ func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd
type ZRangeByScore struct { type ZRangeByScore struct {
Min, Max string Min, Max string
Offset, Count int64 Offset, Count int64
} }
@ -954,32 +957,20 @@ func (c *commandable) ZRemRangeByScore(key, min, max string) *IntCmd {
return cmd return cmd
} }
func (c *commandable) zRevRange(key, start, stop string, withScores bool) *StringSliceCmd { func (c *commandable) ZRevRange(key string, start, stop int64) *StringSliceCmd {
args := []string{"ZREVRANGE", key, start, stop} cmd := NewStringSliceCmd("ZREVRANGE", key, formatInt(start), formatInt(stop))
if withScores {
args = append(args, "WITHSCORES")
}
cmd := NewStringSliceCmd(args...)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
} }
func (c *commandable) ZRevRange(key, start, stop string) *StringSliceCmd { func (c *commandable) ZRevRangeWithScores(key string, start, stop int64) *ZSliceCmd {
return c.zRevRange(key, start, stop, false) cmd := NewZSliceCmd("ZREVRANGE", key, formatInt(start), formatInt(stop), "WITHSCORES")
}
func (c *commandable) ZRevRangeWithScores(key, start, stop string) *ZSliceCmd {
args := []string{"ZREVRANGE", key, start, stop, "WITHSCORES"}
cmd := NewZSliceCmd(args...)
c.Process(cmd) c.Process(cmd)
return 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} args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min}
if withScores {
args = append(args, "WITHSCORES")
}
if opt.Offset != 0 || opt.Count != 0 { if opt.Offset != 0 || opt.Count != 0 {
args = append( args = append(
args, args,
@ -993,10 +984,6 @@ func (c *commandable) zRevRangeByScore(key string, opt ZRangeByScore, withScores
return cmd 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 { func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd {
args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"} args := []string{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"}
if opt.Offset != 0 || opt.Count != 0 { if opt.Offset != 0 || opt.Count != 0 {
@ -1024,12 +1011,8 @@ func (c *commandable) ZScore(key, member string) *FloatCmd {
return cmd return cmd
} }
func (c *commandable) ZUnionStore( func (c *commandable) ZUnionStore(dest string, store ZStore, keys ...string) *IntCmd {
destination string, args := []string{"ZUNIONSTORE", dest, strconv.FormatInt(int64(len(keys)), 10)}
store ZStore,
keys ...string,
) *IntCmd {
args := []string{"ZUNIONSTORE", destination, strconv.FormatInt(int64(len(keys)), 10)}
args = append(args, keys...) args = append(args, keys...)
if len(store.Weights) > 0 { if len(store.Weights) > 0 {
args = append(args, "WEIGHTS") args = append(args, "WEIGHTS")

View File

@ -2009,15 +2009,15 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred()) Expect(zAdd.Err()).NotTo(HaveOccurred())
zRevRange := client.ZRevRange("zset", "0", "-1") zRevRange := client.ZRevRange("zset", 0, -1)
Expect(zRevRange.Err()).NotTo(HaveOccurred()) Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"})) 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.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"one"})) 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.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"two", "one"})) Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
}) })
@ -2030,15 +2030,15 @@ var _ = Describe("Commands", func() {
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) zAdd = client.ZAdd("zset", redis.Z{3, "three"})
Expect(zAdd.Err()).NotTo(HaveOccurred()) 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(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}})) 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(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{1, "one"}})) 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(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{2, "two"}, {1, "one"}})) Expect(val).To(Equal([]redis.Z{{2, "two"}, {1, "one"}}))
}) })