Skip to content

Commit

Permalink
Merge pull request redis#247 from go-redis/fix/sort-nil-reply
Browse files Browse the repository at this point in the history
Sort can return nil reply. Fixes redis#246.
  • Loading branch information
vmihailenco committed Jan 22, 2016
2 parents dd1ac33 + 3ed364e commit 3f059e5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
14 changes: 12 additions & 2 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ type Sort struct {
Store string
}

func (c *commandable) Sort(key string, sort Sort) *StringSliceCmd {
func (sort *Sort) args(key string) []interface{} {
args := []interface{}{"SORT", key}
if sort.By != "" {
args = append(args, "BY", sort.By)
Expand All @@ -290,7 +290,17 @@ func (c *commandable) Sort(key string, sort Sort) *StringSliceCmd {
if 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)
return cmd
}
Expand Down
63 changes: 51 additions & 12 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,19 +500,58 @@ var _ = Describe("Commands", func() {
})

It("should Sort", func() {
lPush := client.LPush("list", "1")
Expect(lPush.Err()).NotTo(HaveOccurred())
Expect(lPush.Val()).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)))
size, err := client.LPush("list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))

sort := client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"})
Expect(sort.Err()).NotTo(HaveOccurred())
Expect(sort.Val()).To(Equal([]string{"1", "2"}))
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)))

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() {
Expand Down
7 changes: 5 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,13 @@ func stringSliceParser(cn *conn, n int64) (interface{}, error) {
ss := make([]string, 0, n)
for i := int64(0); i < n; i++ {
s, err := readStringReply(cn)
if err != nil {
if err == Nil {
ss = append(ss, "")
} else if err != nil {
return nil, err
} else {
ss = append(ss, s)
}
ss = append(ss, s)
}
return ss, nil
}
Expand Down

0 comments on commit 3f059e5

Please sign in to comment.