9 "github.com/go-redis/redis/internal/proto"
12 // Implements proto.MultiBulkParse
13 func sliceParser(rd *proto.Reader, n int64) (interface{}, error) {
14 vals := make([]interface{}, 0, n)
15 for i := int64(0); i < n; i++ {
16 v, err := rd.ReadReply(sliceParser)
19 vals = append(vals, nil)
22 if err, ok := err.(proto.RedisError); ok {
23 vals = append(vals, err)
29 switch v := v.(type) {
31 vals = append(vals, string(v))
33 vals = append(vals, v)
39 // Implements proto.MultiBulkParse
40 func boolSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
41 bools := make([]bool, 0, n)
42 for i := int64(0); i < n; i++ {
43 n, err := rd.ReadIntReply()
47 bools = append(bools, n == 1)
52 // Implements proto.MultiBulkParse
53 func stringSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
54 ss := make([]string, 0, n)
55 for i := int64(0); i < n; i++ {
56 s, err := rd.ReadStringReply()
59 } else if err != nil {
68 // Implements proto.MultiBulkParse
69 func stringStringMapParser(rd *proto.Reader, n int64) (interface{}, error) {
70 m := make(map[string]string, n/2)
71 for i := int64(0); i < n; i += 2 {
72 key, err := rd.ReadStringReply()
77 value, err := rd.ReadStringReply()
87 // Implements proto.MultiBulkParse
88 func stringIntMapParser(rd *proto.Reader, n int64) (interface{}, error) {
89 m := make(map[string]int64, n/2)
90 for i := int64(0); i < n; i += 2 {
91 key, err := rd.ReadStringReply()
96 n, err := rd.ReadIntReply()
106 // Implements proto.MultiBulkParse
107 func stringStructMapParser(rd *proto.Reader, n int64) (interface{}, error) {
108 m := make(map[string]struct{}, n)
109 for i := int64(0); i < n; i++ {
110 key, err := rd.ReadStringReply()
120 // Implements proto.MultiBulkParse
121 func zSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
123 for i := int64(0); i < n; i += 2 {
128 z.Member, err = rd.ReadStringReply()
133 z.Score, err = rd.ReadFloatReply()
141 // Implements proto.MultiBulkParse
142 func clusterSlotsParser(rd *proto.Reader, n int64) (interface{}, error) {
143 slots := make([]ClusterSlot, n)
144 for i := 0; i < len(slots); i++ {
145 n, err := rd.ReadArrayLen()
150 err := fmt.Errorf("redis: got %d elements in cluster info, expected at least 2", n)
154 start, err := rd.ReadIntReply()
159 end, err := rd.ReadIntReply()
164 nodes := make([]ClusterNode, n-2)
165 for j := 0; j < len(nodes); j++ {
166 n, err := rd.ReadArrayLen()
170 if n != 2 && n != 3 {
171 err := fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", n)
175 ip, err := rd.ReadStringReply()
180 port, err := rd.ReadIntReply()
184 nodes[j].Addr = net.JoinHostPort(ip, strconv.FormatInt(port, 10))
187 id, err := rd.ReadStringReply()
195 slots[i] = ClusterSlot{
204 func newGeoLocationParser(q *GeoRadiusQuery) proto.MultiBulkParse {
205 return func(rd *proto.Reader, n int64) (interface{}, error) {
209 loc.Name, err = rd.ReadStringReply()
214 loc.Dist, err = rd.ReadFloatReply()
220 loc.GeoHash, err = rd.ReadIntReply()
226 n, err := rd.ReadArrayLen()
231 return nil, fmt.Errorf("got %d coordinates, expected 2", n)
234 loc.Longitude, err = rd.ReadFloatReply()
238 loc.Latitude, err = rd.ReadFloatReply()
248 func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
249 return func(rd *proto.Reader, n int64) (interface{}, error) {
250 locs := make([]GeoLocation, 0, n)
251 for i := int64(0); i < n; i++ {
252 v, err := rd.ReadReply(newGeoLocationParser(q))
256 switch vv := v.(type) {
258 locs = append(locs, GeoLocation{
262 locs = append(locs, *vv)
264 return nil, fmt.Errorf("got %T, expected string or *GeoLocation", v)
271 func geoPosParser(rd *proto.Reader, n int64) (interface{}, error) {
275 pos.Longitude, err = rd.ReadFloatReply()
280 pos.Latitude, err = rd.ReadFloatReply()
288 func geoPosSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
289 positions := make([]*GeoPos, 0, n)
290 for i := int64(0); i < n; i++ {
291 v, err := rd.ReadReply(geoPosParser)
294 positions = append(positions, nil)
299 switch v := v.(type) {
301 positions = append(positions, v)
303 return nil, fmt.Errorf("got %T, expected *GeoPos", v)
306 return positions, nil
309 func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
314 return nil, fmt.Errorf("redis: got %d elements in COMMAND reply, wanted 6", n)
317 cmd.Name, err = rd.ReadStringReply()
322 arity, err := rd.ReadIntReply()
326 cmd.Arity = int8(arity)
328 flags, err := rd.ReadReply(stringSliceParser)
332 cmd.Flags = flags.([]string)
334 firstKeyPos, err := rd.ReadIntReply()
338 cmd.FirstKeyPos = int8(firstKeyPos)
340 lastKeyPos, err := rd.ReadIntReply()
344 cmd.LastKeyPos = int8(lastKeyPos)
346 stepCount, err := rd.ReadIntReply()
350 cmd.StepCount = int8(stepCount)
352 for _, flag := range cmd.Flags {
353 if flag == "readonly" {
362 // Implements proto.MultiBulkParse
363 func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
364 m := make(map[string]*CommandInfo, n)
365 for i := int64(0); i < n; i++ {
366 v, err := rd.ReadReply(commandInfoParser)
370 vv := v.(*CommandInfo)
377 // Implements proto.MultiBulkParse
378 func timeParser(rd *proto.Reader, n int64) (interface{}, error) {
380 return nil, fmt.Errorf("got %d elements, expected 2", n)
383 sec, err := rd.ReadInt()
388 microsec, err := rd.ReadInt()
393 return time.Unix(sec, microsec*1000), nil