From 3ed364e92ae4c5a756349a48f0eba281ebe01180 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 22 Jan 2016 12:29:23 +0200 Subject: [PATCH] Sort can return nil reply. Fixes #246. --- commands.go | 14 +++++++++-- commands_test.go | 63 +++++++++++++++++++++++++++++++++++++++--------- parser.go | 7 ++++-- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/commands.go b/commands.go index d6cb800d..f8a80f56 100644 --- a/commands.go +++ b/commands.go @@ -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) @@ -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 } diff --git a/commands_test.go b/commands_test.go index f7d5502e..b82b0197 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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() { diff --git a/parser.go b/parser.go index 94fa5979..741c02a3 100644 --- a/parser.go +++ b/parser.go @@ -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 }