feat: add SORT_RO command

This commit is contained in:
Emilien Kofman 2023-01-18 12:13:55 +01:00
parent 86f4ea13a9
commit ca063fd0ad
2 changed files with 35 additions and 5 deletions

View File

@ -116,6 +116,7 @@ type Cmdable interface {
Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd
SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd
SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd
SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd
Touch(ctx context.Context, keys ...string) *IntCmd Touch(ctx context.Context, keys ...string) *IntCmd
@ -710,8 +711,9 @@ type Sort struct {
Alpha bool Alpha bool
} }
func (sort *Sort) args(key string) []interface{} { func (sort *Sort) args(command, key string) []interface{} {
args := []interface{}{"sort", key} args := []interface{}{command, key}
if sort.By != "" { if sort.By != "" {
args = append(args, "by", sort.By) args = append(args, "by", sort.By)
} }
@ -730,14 +732,20 @@ func (sort *Sort) args(key string) []interface{} {
return args return args
} }
func (c cmdable) SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
cmd := NewStringSliceCmd(ctx, sort.args("sort_ro", key)...)
_ = c(ctx, cmd)
return cmd
}
func (c cmdable) Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd { func (c cmdable) Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
cmd := NewStringSliceCmd(ctx, sort.args(key)...) cmd := NewStringSliceCmd(ctx, sort.args("sort", key)...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }
func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd { func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd {
args := sort.args(key) args := sort.args("sort", key)
if store != "" { if store != "" {
args = append(args, "store", store) args = append(args, "store", store)
} }
@ -747,7 +755,7 @@ func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *
} }
func (c cmdable) SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd { func (c cmdable) SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd {
cmd := NewSliceCmd(ctx, sort.args(key)...) cmd := NewSliceCmd(ctx, sort.args("sort", key)...)
_ = c(ctx, cmd) _ = c(ctx, cmd)
return cmd return cmd
} }

View File

@ -668,6 +668,28 @@ var _ = Describe("Commands", func() {
Expect(val).To(Equal("hello")) Expect(val).To(Equal("hello"))
}) })
It("should Sort RO", func() {
size, err := client.LPush(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
els, err := client.SortRO(ctx, "list", &redis.Sort{
Offset: 0,
Count: 2,
Order: "ASC",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"1", "2"}))
})
It("should Sort", func() { It("should Sort", func() {
size, err := client.LPush(ctx, "list", "1").Result() size, err := client.LPush(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())