Skip to content

Commit d4e63b4

Browse files
authored
Merge pull request #2354 from emilienkofman/feat/add_sort_ro
feat: add SORT_RO command
2 parents 86f4ea1 + ca063fd commit d4e63b4

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

commands.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type Cmdable interface {
116116
Restore(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
117117
RestoreReplace(ctx context.Context, key string, ttl time.Duration, value string) *StatusCmd
118118
Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd
119+
SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd
119120
SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd
120121
SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd
121122
Touch(ctx context.Context, keys ...string) *IntCmd
@@ -710,8 +711,9 @@ type Sort struct {
710711
Alpha bool
711712
}
712713

713-
func (sort *Sort) args(key string) []interface{} {
714-
args := []interface{}{"sort", key}
714+
func (sort *Sort) args(command, key string) []interface{} {
715+
args := []interface{}{command, key}
716+
715717
if sort.By != "" {
716718
args = append(args, "by", sort.By)
717719
}
@@ -730,14 +732,20 @@ func (sort *Sort) args(key string) []interface{} {
730732
return args
731733
}
732734

735+
func (c cmdable) SortRO(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
736+
cmd := NewStringSliceCmd(ctx, sort.args("sort_ro", key)...)
737+
_ = c(ctx, cmd)
738+
return cmd
739+
}
740+
733741
func (c cmdable) Sort(ctx context.Context, key string, sort *Sort) *StringSliceCmd {
734-
cmd := NewStringSliceCmd(ctx, sort.args(key)...)
742+
cmd := NewStringSliceCmd(ctx, sort.args("sort", key)...)
735743
_ = c(ctx, cmd)
736744
return cmd
737745
}
738746

739747
func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *IntCmd {
740-
args := sort.args(key)
748+
args := sort.args("sort", key)
741749
if store != "" {
742750
args = append(args, "store", store)
743751
}
@@ -747,7 +755,7 @@ func (c cmdable) SortStore(ctx context.Context, key, store string, sort *Sort) *
747755
}
748756

749757
func (c cmdable) SortInterfaces(ctx context.Context, key string, sort *Sort) *SliceCmd {
750-
cmd := NewSliceCmd(ctx, sort.args(key)...)
758+
cmd := NewSliceCmd(ctx, sort.args("sort", key)...)
751759
_ = c(ctx, cmd)
752760
return cmd
753761
}

commands_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,28 @@ var _ = Describe("Commands", func() {
668668
Expect(val).To(Equal("hello"))
669669
})
670670

671+
It("should Sort RO", func() {
672+
size, err := client.LPush(ctx, "list", "1").Result()
673+
Expect(err).NotTo(HaveOccurred())
674+
Expect(size).To(Equal(int64(1)))
675+
676+
size, err = client.LPush(ctx, "list", "3").Result()
677+
Expect(err).NotTo(HaveOccurred())
678+
Expect(size).To(Equal(int64(2)))
679+
680+
size, err = client.LPush(ctx, "list", "2").Result()
681+
Expect(err).NotTo(HaveOccurred())
682+
Expect(size).To(Equal(int64(3)))
683+
684+
els, err := client.SortRO(ctx, "list", &redis.Sort{
685+
Offset: 0,
686+
Count: 2,
687+
Order: "ASC",
688+
}).Result()
689+
Expect(err).NotTo(HaveOccurred())
690+
Expect(els).To(Equal([]string{"1", "2"}))
691+
})
692+
671693
It("should Sort", func() {
672694
size, err := client.LPush(ctx, "list", "1").Result()
673695
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)