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

View File

@ -582,7 +582,7 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3))) Expect(size).To(Equal(int64(3)))
els, err := client.Sort("list", redis.Sort{ els, err := client.Sort("list", &redis.Sort{
Offset: 0, Offset: 0,
Count: 2, Count: 2,
Order: "ASC", Order: "ASC",
@ -608,7 +608,7 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
{ {
els, err := client.Sort("list", redis.Sort{ els, err := client.Sort("list", &redis.Sort{
Get: []string{"object_*"}, Get: []string{"object_*"},
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) 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_*"}, Get: []string{"object_*"},
}).Result() }).Result()
Expect(err).NotTo(HaveOccurred()) 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() { It("should TTL", func() {
ttl := client.TTL("key") ttl := client.TTL("key")
Expect(ttl.Err()).NotTo(HaveOccurred()) Expect(ttl.Err()).NotTo(HaveOccurred())