-
Notifications
You must be signed in to change notification settings - Fork 1
/
proto.go
210 lines (185 loc) · 4.09 KB
/
proto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package redimock
import (
"bufio"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"unicode"
)
/*
This file is based on `https://github.com/alicebob/miniredis/blob/master/server/proto.go`
*/
// ErrProtocol is the general error for unexpected input
var ErrProtocol = errors.New("invalid request")
type (
// BulkString is used to handle the bulk string, normal strings are treated as simple string
BulkString string
// Error is the redis error type
Error string
)
// client always sends arrays with bulk strings
func readArray(r io.Reader) ([]string, error) {
rd := bufio.NewReader(r)
line, err := rd.ReadString('\n')
if err != nil {
return nil, err
}
if len(line) < 3 {
return nil, ErrProtocol
}
switch line[0] {
default:
return nil, ErrProtocol
case '*':
l, err := strconv.Atoi(line[1 : len(line)-2])
if err != nil {
return nil, ErrProtocol
}
// l can be -1
var fields []string
for ; l > 0; l-- {
s, err := readString(rd)
if err != nil {
return nil, err
}
fields = append(fields, s)
}
return fields, nil
}
}
func readString(rd *bufio.Reader) (string, error) {
line, err := rd.ReadString('\n')
if err != nil {
return "", err
}
if len(line) < 3 {
return "", ErrProtocol
}
switch line[0] {
default:
return "", ErrProtocol
case '+', '-', ':':
// +: simple string
// -: errors
// :: integer
// Simple line based replies.
return string(line[1 : len(line)-2]), nil
case '$':
// bulk strings are: `$5\r\nhello\r\n`
length, err := strconv.Atoi(line[1 : len(line)-2])
if err != nil {
return "", ErrProtocol
}
if length < 0 {
// -1 is a nil response
return "", nil
}
var (
buf = make([]byte, length+2)
pos = 0
)
for pos < length+2 {
n, err := rd.Read(buf[pos:])
if err != nil {
return "", err
}
pos += n
}
return string(buf[:length]), nil
}
}
func writeF(w io.Writer, s string, args ...interface{}) error {
str := fmt.Sprintf(s, args...)
_, err := fmt.Fprintf(w, str)
return err
}
// writeError try to write a redis error to output
func writeError(w io.Writer, e Error) error {
return writeF(w, "-%s\r\n", toInline(string(e)))
}
// writeSimpleString writes a redis inline string
func writeSimpleString(w io.Writer, s string) error {
return writeF(w, "+%s\r\n", toInline(s))
}
// writeBulkString writes a bulk string
func writeBulkString(w io.Writer, s BulkString) error {
return writeF(w, "$%d\r\n%s\r\n", len(s), s)
}
// writeNull writes a redis string NULL
func writeNull(w io.Writer) error {
return writeF(w, "$-1\r\n")
}
// writeLen starts an array with the given length
func writeLen(w io.Writer, n int) error {
return writeF(w, "*%d\r\n", n)
}
// writeInt writes an integer
func writeInt(w io.Writer, i int) error {
return writeF(w, ":%d\r\n", i)
}
func toInline(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return ' '
}
return r
}, s)
}
func tryWriteArray(w io.Writer, t interface{}) error {
// Now nasty reflection
v := reflect.ValueOf(t)
if v.Kind() != reflect.Slice {
return fmt.Errorf("invalid type: %T", t)
}
l := v.Len()
if err := writeLen(w, l); err != nil {
return err
}
args := make([]interface{}, l)
for i := range args {
args[i] = v.Index(i).Interface()
}
return write(w, args...)
}
func writeSingle(w io.Writer, arg interface{}) error {
// first the easy way, no reflection
switch t := arg.(type) {
case Error:
// TODO : make sure its a one-liner
return writeError(w, t)
case BulkString:
return writeBulkString(w, t)
case int:
return writeInt(w, t)
case string:
return writeSimpleString(w, t)
case nil:
return writeNull(w)
default:
return tryWriteArray(w, t)
}
}
func write(w io.Writer, args ...interface{}) error {
for i := range args {
if err := writeSingle(w, args[i]); err != nil {
return err
}
}
return nil
}
// equalArgs try to compare arguments
// TODO : add more functionality, like case insensitive or order
func equalArgs(in []string, expectd []string) bool {
if len(in) != len(expectd) {
return false
}
for i := range in {
if expectd[i] != in[i] {
return false
}
}
return true
}