Read an array, with all elements are the raw redis commands Also reads sets and maps.
(b string)
| 28 | // Read an array, with all elements are the raw redis commands |
| 29 | // Also reads sets and maps. |
| 30 | func ReadArray(b string) ([]string, error) { |
| 31 | r := bufio.NewReader(strings.NewReader(b)) |
| 32 | line, err := readLine(r) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | elems := 0 |
| 38 | switch line[0] { |
| 39 | default: |
| 40 | return nil, ErrUnexpected |
| 41 | case '*', '>', '~': |
| 42 | // *: array |
| 43 | // >: push data |
| 44 | // ~: set |
| 45 | length, err := strconv.Atoi(line[1 : len(line)-2]) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | elems = length |
| 50 | case '%': |
| 51 | // we also read maps. |
| 52 | length, err := strconv.Atoi(line[1 : len(line)-2]) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | elems = length * 2 |
| 57 | } |
| 58 | |
| 59 | var res []string |
| 60 | for i := 0; i < elems; i++ { |
| 61 | next, err := Read(r) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | next, err = ReadString(next) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | res = append(res, next) |
| 70 | } |
| 71 | return res, nil |
| 72 | } |
| 73 | |
| 74 | func ReadString(b string) (string, error) { |
| 75 | r := bufio.NewReader(strings.NewReader(b)) |