github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/sort.go (about) 1 package client 2 3 // SortCommand represents a redis Sort command 4 type SortCommand struct { 5 redis *Redis 6 key string 7 by string 8 limit bool 9 offset int 10 count int 11 get []string 12 order string 13 alpha bool 14 store string 15 } 16 17 // Sort doc: http://redis.io/commands/sort 18 // SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination] 19 func (r *Redis) Sort(key string) *SortCommand { 20 return &SortCommand{redis: r, key: key} 21 } 22 23 // By can also take a non-existent key, which causes SORT to skip the sorting operation. 24 func (s *SortCommand) By(pattern string) *SortCommand { 25 s.by = pattern 26 return s 27 } 28 29 // Limit takes the offset and count argument, 30 // specifying the number of elements to skip and the count argument, 31 // specifying the number of elements to return from starting at offset. 32 func (s *SortCommand) Limit(offset, count int) *SortCommand { 33 s.limit = true 34 s.offset = offset 35 s.count = count 36 return s 37 } 38 39 // Get sets sort GET arguments. 40 func (s *SortCommand) Get(patterns ...string) *SortCommand { 41 s.get = patterns 42 return s 43 } 44 45 // ASC sets sort order to ASC. 46 func (s *SortCommand) ASC() *SortCommand { 47 s.order = "ASC" 48 return s 49 } 50 51 // DESC sets sort order to DESC. 52 func (s *SortCommand) DESC() *SortCommand { 53 s.order = "DESC" 54 return s 55 } 56 57 // Alpha sets ALPHA to sort command. 58 func (s *SortCommand) Alpha(b bool) *SortCommand { 59 s.alpha = b 60 return s 61 } 62 63 // Store sets the sorted result store to. 64 func (s *SortCommand) Store(destination string) *SortCommand { 65 s.store = destination 66 return s 67 } 68 69 // Run performs redis sort command. 70 func (s *SortCommand) Run() (*Reply, error) { 71 args := packArgs("SORT", s.key) 72 if s.by != "" { 73 args = append(args, "BY", s.by) 74 } 75 if s.limit { 76 args = append(args, "LIMIT", s.offset, s.count) 77 } 78 if s.get != nil && len(s.get) > 0 { 79 for _, pattern := range s.get { 80 args = append(args, "GET", pattern) 81 } 82 } 83 if s.order != "" { 84 args = append(args, s.order) 85 } 86 if s.alpha { 87 args = append(args, "ALPHA") 88 } 89 if s.store != "" { 90 args = append(args, "STORE", s.store) 91 } 92 return s.redis.ExecuteCommand(args...) 93 }