Skip to content

Commit

Permalink
Add SortStore and fix Sort signature
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Feb 13, 2018
1 parent 8e5e567 commit e456ee7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
29 changes: 18 additions & 11 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ type Cmdable interface {
RenameNX(key, newkey string) *BoolCmd
Restore(key string, ttl time.Duration, value string) *StatusCmd
RestoreReplace(key string, ttl time.Duration, value string) *StatusCmd
Sort(key string, sort Sort) *StringSliceCmd
SortInterfaces(key string, sort Sort) *SliceCmd
Sort(key string, sort *Sort) *StringSliceCmd
SortStore(key, store string, sort *Sort) *IntCmd
SortInterfaces(key string, sort *Sort) *SliceCmd
TTL(key string) *DurationCmd
Type(key string) *StatusCmd
Scan(cursor uint64, match string, count int64) *ScanCmd
Expand Down Expand Up @@ -484,11 +485,10 @@ func (c *cmdable) RestoreReplace(key string, ttl time.Duration, value string) *S

type Sort struct {
By string
Offset, Count float64
Offset, Count int64
Get []string
Order string
IsAlpha bool
Store string
Alpha bool
}

func (sort *Sort) args(key string) []interface{} {
Expand All @@ -505,22 +505,29 @@ func (sort *Sort) args(key string) []interface{} {
if sort.Order != "" {
args = append(args, sort.Order)
}
if sort.IsAlpha {
if sort.Alpha {
args = append(args, "alpha")
}
if sort.Store != "" {
args = append(args, "store", sort.Store)
}
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)...)
c.process(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)...)
c.process(cmd)
return cmd
Expand Down
32 changes: 29 additions & 3 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))

els, err := client.Sort("list", redis.Sort{
els, err := client.Sort("list", &redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
Expand All @@ -608,22 +608,48 @@ var _ = Describe("Commands", func() {
Expect(err).NotTo(HaveOccurred())

{
els, err := client.Sort("list", redis.Sort{
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{
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 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() {
ttl := client.TTL("key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expand Down

0 comments on commit e456ee7

Please sign in to comment.