From 2abf5c5f146e7ce1740eb1ed942596459b71ce1e Mon Sep 17 00:00:00 2001 From: Jeff Pierce Date: Mon, 24 Aug 2015 14:52:00 -0700 Subject: [PATCH] Refactored zRangeByScore into zRangeBy. --- commands.go | 26 ++++++++++---------------- commands_test.go | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/commands.go b/commands.go index b07a8046..a289cd32 100644 --- a/commands.go +++ b/commands.go @@ -1089,19 +1089,13 @@ func (c *commandable) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd return cmd } -type ZRangeByScore struct { +type ZRangeBy struct { Min, Max string Offset, Count int64 } -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} +func (c *commandable) zRangeBy(zRangeType, key string, opt ZRangeBy, withScores bool) *StringSliceCmd { + args := []interface{}{zRangeType, key, opt.Min, opt.Max} if withScores { args = append(args, "WITHSCORES") } @@ -1118,15 +1112,15 @@ func (c *commandable) zRangeByScore(key string, opt ZRangeByScore, withScores, i return cmd } -func (c *commandable) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd { - return c.zRangeByScore(key, opt, false, false) +func (c *commandable) ZRangeByScore(key string, opt ZRangeBy) *StringSliceCmd { + return c.zRangeBy("ZRANGEBYSCORE", key, opt, false) } -func (c *commandable) ZRangeByLex(key string, opt ZRangeByScore) *StringSliceCmd { - return c.zRangeByScore(key, opt, false, true) +func (c *commandable) ZRangeByLex(key string, opt ZRangeBy) *StringSliceCmd { + return c.zRangeBy("ZRANGEBYLEX", key, opt, false) } -func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { +func (c *commandable) ZRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd { args := []interface{}{"ZRANGEBYSCORE", key, opt.Min, opt.Max, "WITHSCORES"} if opt.Offset != 0 || opt.Count != 0 { args = append( @@ -1188,7 +1182,7 @@ func (c *commandable) ZRevRangeWithScores(key string, start, stop int64) *ZSlice return cmd } -func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd { +func (c *commandable) ZRevRangeByScore(key string, opt ZRangeBy) *StringSliceCmd { args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min} if opt.Offset != 0 || opt.Count != 0 { args = append( @@ -1203,7 +1197,7 @@ func (c *commandable) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSli return cmd } -func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd { +func (c *commandable) ZRevRangeByScoreWithScores(key string, opt ZRangeBy) *ZSliceCmd { args := []interface{}{"ZREVRANGEBYSCORE", key, opt.Max, opt.Min, "WITHSCORES"} if opt.Offset != 0 || opt.Count != 0 { args = append( diff --git a/commands_test.go b/commands_test.go index d0dbe43c..4ef419e8 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1981,28 +1981,28 @@ var _ = Describe("Commands", func() { zAdd = client.ZAdd("zset", redis.Z{3, "three"}) Expect(zAdd.Err()).NotTo(HaveOccurred()) - zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeByScore{ + zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{ Min: "-inf", Max: "+inf", }) Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"})) - zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{ + zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{ Min: "1", Max: "2", }) Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"})) - zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{ + zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{ Min: "(1", Max: "2", }) Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) Expect(zRangeByScore.Val()).To(Equal([]string{"two"})) - zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeByScore{ + zRangeByScore = client.ZRangeByScore("ZRANGEBYSCORE", "zset", redis.ZRangeBy{ Min: "(1", Max: "(2", }) @@ -2017,28 +2017,28 @@ var _ = Describe("Commands", func() { Expect(zAdd.Err()).NotTo(HaveOccurred()) zAdd = client.ZAdd("zset", redis.Z{0, "c"}) - zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeByScore{ + zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{ Min: "-", Max: "+", }) Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"})) - zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ + zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{ Min: "[a", Max: "[b", }) Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"})) - zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ + zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{ Min: "(a", Max: "[b", }) Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) Expect(zRangeByLex.Val()).To(Equal([]string{"b"})) - zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeByScore{ + zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{ Min: "(a", Max: "(b", }) @@ -2054,28 +2054,28 @@ var _ = Describe("Commands", func() { zAdd = client.ZAdd("zset", redis.Z{3, "three"}) Expect(zAdd.Err()).NotTo(HaveOccurred()) - val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ Min: "-inf", Max: "+inf", }).Result() Expect(err).NotTo(HaveOccurred()) Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}})) - val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ Min: "1", Max: "2", }).Result() Expect(err).NotTo(HaveOccurred()) Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}})) - val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ Min: "(1", Max: "2", }).Result() Expect(err).NotTo(HaveOccurred()) Expect(val).To(Equal([]redis.Z{{2, "two"}})) - val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{ + val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ Min: "(1", Max: "(2", }).Result()