diff --git a/commands.go b/commands.go index d6cb800..f8a80f5 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 f7d5502..b82b019 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 94fa597..741c02a 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 }