Sort can return nil reply. Fixes #246.

This commit is contained in:
Vladimir Mihailenco 2016-01-22 12:29:23 +02:00
parent dd1ac33826
commit 3ed364e92a
3 changed files with 68 additions and 16 deletions

View File

@ -270,7 +270,7 @@ type Sort struct {
Store string Store string
} }
func (c *commandable) Sort(key string, sort Sort) *StringSliceCmd { func (sort *Sort) args(key string) []interface{} {
args := []interface{}{"SORT", key} args := []interface{}{"SORT", key}
if sort.By != "" { if sort.By != "" {
args = append(args, "BY", sort.By) args = append(args, "BY", sort.By)
@ -290,7 +290,17 @@ func (c *commandable) Sort(key string, sort Sort) *StringSliceCmd {
if sort.Store != "" { if sort.Store != "" {
args = append(args, "STORE", sort.Store) args = append(args, "STORE", sort.Store)
} }
cmd := NewStringSliceCmd(args...) return args
}
func (c *commandable) Sort(key string, sort Sort) *StringSliceCmd {
cmd := NewStringSliceCmd(sort.args(key)...)
c.Process(cmd)
return cmd
}
func (c *commandable) SortInterfaces(key string, sort Sort) *SliceCmd {
cmd := NewSliceCmd(sort.args(key)...)
c.Process(cmd) c.Process(cmd)
return cmd return cmd
} }

View File

@ -500,19 +500,58 @@ var _ = Describe("Commands", func() {
}) })
It("should Sort", func() { It("should Sort", func() {
lPush := client.LPush("list", "1") size, err := client.LPush("list", "1").Result()
Expect(lPush.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(lPush.Val()).To(Equal(int64(1))) Expect(size).To(Equal(int64(1)))
lPush = client.LPush("list", "3")
Expect(lPush.Err()).NotTo(HaveOccurred())
Expect(lPush.Val()).To(Equal(int64(2)))
lPush = client.LPush("list", "2")
Expect(lPush.Err()).NotTo(HaveOccurred())
Expect(lPush.Val()).To(Equal(int64(3)))
sort := client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"}) size, err = client.LPush("list", "3").Result()
Expect(sort.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(sort.Val()).To(Equal([]string{"1", "2"})) Expect(size).To(Equal(int64(2)))
size, err = client.LPush("list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
els, err := client.Sort("list", redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"1", "2"}))
})
It("should Sort and Get", 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)))
err = client.Set("object_2", "value2", 0).Err()
Expect(err).NotTo(HaveOccurred())
{
els, err := client.Sort("list", redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"", "value2", ""}))
}
{
els, err := client.SortInterfaces("list", redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]interface{}{nil, "value2", nil}))
}
}) })
It("should TTL", func() { It("should TTL", func() {

View File

@ -491,10 +491,13 @@ func stringSliceParser(cn *conn, n int64) (interface{}, error) {
ss := make([]string, 0, n) ss := make([]string, 0, n)
for i := int64(0); i < n; i++ { for i := int64(0); i < n; i++ {
s, err := readStringReply(cn) s, err := readStringReply(cn)
if err != nil { if err == Nil {
ss = append(ss, "")
} else if err != nil {
return nil, err return nil, err
} else {
ss = append(ss, s)
} }
ss = append(ss, s)
} }
return ss, nil return ss, nil
} }