Add SortStore and fix Sort signature

This commit is contained in:
Vladimir Mihailenco 2018-02-13 16:08:11 +02:00
parent 8e5e567b90
commit e456ee7148
2 changed files with 47 additions and 14 deletions

View File

@ -70,8 +70,9 @@ type Cmdable interface {
RenameNX(key, newkey string) *BoolCmd
Restore(key string, ttl time.Duration, value string) *StatusCmd
RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
Sort(key string, sort Sort) *StringSliceCmd
SortInterfaces(key string, sort Sort) *SliceCmd
Sort(key string, sort *Sort) *StringSliceCmd
SortStore(key, store string, sort *Sort) *IntCmd
SortInterfaces(key string, sort *Sort) *SliceCmd
TTL(key string) *DurationCmd
Type(key string) *StatusCmd
Scan(cursor uint64, match string, count int64) *ScanCmd
@ -484,11 +485,10 @@ func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *S
type Sort struct {
By string
Offset, Count float64
Offset, Count int64
Get []string
Order string
IsAlpha bool
Store string
Alpha bool
}
func (sort *Sort) args(key string) []interface{} {
@ -505,22 +505,29 @@ func (sort *Sort) args(key string) []interface{} {
if sort.Order != "" {
args = append(args, sort.Order)
}
if sort.IsAlpha {
if sort.Alpha {
args = append(args, "alpha")
}
if sort.Store != "" {
args = append(args, "store", sort.Store)
}
return args
}
func (c *cmdable) Sort(key string, sort Sort) *StringSliceCmd {
func (c *cmdable) Sort(key string, sort *Sort) *StringSliceCmd {
cmd := NewStringSliceCmd(sort.args(key)...)
c.process(cmd)
return cmd
}
func (c *cmdable) SortInterfaces(key string, sort Sort) *SliceCmd {
func (c *cmdable) SortStore(key, store string, sort *Sort) *IntCmd {
args := sort.args(key)
if store != "" {
args = append(args, "store", store)
}
cmd := NewIntCmd(args...)
c.process(cmd)
return cmd
}
func (c *cmdable) SortInterfaces(key string, sort *Sort) *SliceCmd {
cmd := NewSliceCmd(sort.args(key)...)
c.process(cmd)
return cmd

View File

@ -582,7 +582,7 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
els, err := client.Sort("list", redis.Sort{
els, err := client.Sort("list", &redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
@ -608,7 +608,7 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())
{
els, err := client.Sort("list", redis.Sort{
els, err := client.Sort("list", &redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
@ -616,7 +616,7 @@ var _ = Describe("Commands", func() {
}
{
els, err := client.SortInterfaces("list", redis.Sort{
els, err := client.SortInterfaces("list", &redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
@ -624,6 +624,32 @@ var _ = Describe("Commands", func() {
}
})
It("should Sort and Store", func() {
size, err := client.LPush("list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush("list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush("list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
n, err := client.SortStore("list", "list2", &redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
els, err := client.LRange("list2", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"1", "2"}))
})
It("should TTL", func() {
ttl := client.TTL("key")
Expect(ttl.Err()).NotTo(HaveOccurred())