-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproto_reader.v
More file actions
373 lines (336 loc) · 7.51 KB
/
Copy pathproto_reader.v
File metadata and controls
373 lines (336 loc) · 7.51 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
module redict
import io
import math
// import math.big
import strconv
const buf_len = 4096
struct ProtoReader {
mut:
reader &io.BufferedReader
}
fn new_reader(mut io_reader io.Reader) &ProtoReader {
return &ProtoReader{
reader: io.new_buffered_reader(reader: io_reader)
}
}
fn (mut rd ProtoReader) read_line() !string {
l := rd.read()!
match l[0].ascii_str() {
resp_error {
return new_redict_error(l.trim_string_right(resp_error))
}
resp_nil {
return redict_nil
}
resp_blob_error {
blob_error := rd.read_string_reply(l)!
return new_redict_error(blob_error)
}
resp_attr {
rd.discard(l)!
return rd.read_line()
}
else {} // TODO default
}
return l
}
fn (mut rd ProtoReader) read() !string {
return rd.reader.read_line() // note: read_line trims the final `\n` and `\r\n`
}
fn (mut rd ProtoReader) read_string_reply(line string) !string {
n := reply_len(line)!
// read exactly n+2 bytes from rd.buf into b
mut b := []u8{len: n + 2}
rd.reader.read(mut b)!
return b.bytestr().trim_string_right(resp_crlf)
}
fn reply_len(line string) !int {
n := strconv.atoi(line[1..])!
if n < -1 {
return new_redict_error('Invalid reply: ${line}')
}
if line.starts_with(resp_string) || line.starts_with(resp_verbatim)
|| line.starts_with(resp_blob_error) || line.starts_with(resp_array)
|| line.starts_with(resp_set) || line.starts_with(resp_push) || line.starts_with(resp_map)
|| line.starts_with(resp_attr) {
if n == -1 {
return redict_nil // TODO is this RESP2?
}
}
return n
}
fn (mut rd ProtoReader) discard(line string) ! {
if line.len == 0 {
return new_redict_error('Invalid line')
}
match line[0].ascii_str() {
resp_status, resp_error, resp_int, resp_nil, resp_float, resp_bool, resp_big_int {
return
}
else {}
}
n := reply_len(line)!
match line[0].ascii_str() {
resp_blob_error, resp_string, resp_verbatim {
// Skip over the next n+2 bytes
mut discarded := []u8{cap: n + 2}
_ := rd.reader.read(mut discarded)!
}
resp_array, resp_set, resp_push {
for i := 0; i < n; i++ {
rd.discard_next()!
}
}
resp_map, resp_attr {
for i := 0; i < n * 2; i++ {
rd.discard_next()!
}
}
else {}
}
return error("Can't parse ${line}")
}
fn (mut rd ProtoReader) discard_next() ! {
line := rd.read()!
return rd.discard(line)
}
// read_reply parses the data returned by read_line()
fn (mut rd ProtoReader) read_reply() !Value {
line := rd.read_line()!
match line[0].ascii_str() {
resp_status {
return line.trim_string_left(resp_status)
}
resp_int {
return strconv.parse_int(line.trim_string_left(resp_int), 10, 64)!
}
resp_float {
return rd.read_float(line)!
}
resp_bool {
return rd.private_read_bool(line)!
}
// resp_big_int {
// return rd.read_big_int(line)!
// }
resp_string {
return rd.read_string_reply(line)!
}
resp_verbatim {
return rd.read_verb(line)!
}
resp_array, resp_set, resp_push {
return rd.read_slice(line)!
}
resp_map {
return rd.read_map(line)!
}
else {
return new_redict_error("ProtoReader.read_reply: Can't parse ${line}")
}
}
}
fn (mut rd ProtoReader) parse_float() !f64 {
line := rd.read_line()!
match line[0].ascii_str() {
resp_float {
return rd.read_float(line)
}
resp_status {
return strconv.atof64(line[1..])!
}
resp_string {
s := rd.read_string_reply(line)!
return strconv.atof64(s)!
}
else {
return new_redict_error("ProtoReader.read_reply: Can't parse float reply ${line}")
}
}
}
fn (rd ProtoReader) read_float(line string) !f64 {
if line[1..] == 'inf' {
return math.inf(1)
}
if line[1..] == '-inf' {
return math.inf(-1)
}
if line[1..] == 'nan' || line[1..] == '-nan' {
return math.nan()
}
return strconv.atof64(line[1..])!
}
fn (rd ProtoReader) private_read_bool(line string) !bool {
if line[1..] == 't' {
return true
}
if line[1..] == 'f' {
return false
}
return new_redict_error("Can't parse bool reply: ${line}")
}
// fn (rd ProtoReader) read_big_int(line string) !big.Integer {
// if i := big.integer_from_string(line[1..]) {
// return i
// } else {
// return error("Can't parse bigInt reply: ${line}")
// }
// }
fn (mut rd ProtoReader) read_verb(line string) !string {
s := rd.read_string_reply(line)!
if s.len < 4 || (s.len >= 4 && s[3] != `:`) {
return new_redict_error("Can't parse verbatim string reply: ${line}")
}
return s[4..]
}
fn (mut rd ProtoReader) read_slice(line string) ![]?Value {
n := reply_len(line)!
mut val := []?Value{len: n, init: none}
for i := 0; i < n; i++ {
v := rd.read_reply() or {
match err {
RedictError {
if is_nil(err) {
val[i] = none
continue
}
val[i] = err
continue
}
else {
return err
}
}
}
val[i] = v
}
return val
}
fn (mut rd ProtoReader) read_map(line string) !map[string]?Value {
n := reply_len(line)!
mut m := map[string]?Value{}
for i := 0; i < n; i++ {
k := rd.read_reply()!
match k {
string {
v := rd.read_reply() or {
match err {
RedictError {
if is_nil(err) {
m[k] = none
continue
}
m[k] = err
continue
}
else {
return err
}
}
}
m[k] = v
}
else {
return new_redict_error('ProtoReader.read_map: ProtoReader.read_reply returned unexpect type')
}
}
}
return m
}
fn (mut rd ProtoReader) read_string() !string {
l := rd.read_line()!
match l[0].ascii_str() {
resp_status, resp_int, resp_float {
return l[1..]
}
resp_string {
return rd.read_string_reply(l)
}
resp_bool {
b := rd.private_read_bool(l)!
return '${b}'
}
resp_verbatim {
return rd.read_verb(l)!
}
// resp_big_int {
// b := rd.read_big_int(line)!
// return '${b}'
// }
else {
return new_redict_error("ProtoReader.read_string: Can't parse reply ${l} reading string")
}
}
}
fn (mut rd ProtoReader) read_bool() !bool {
s := rd.read_string()!
return s == 'OK' || s == '1' || s == 'true'
}
fn (mut rd ProtoReader) read_int() !i64 {
line := rd.read_line()!
match line[0].ascii_str() {
resp_int, resp_status {
return strconv.parse_int(line[1..], 10, 64)!
}
resp_string {
i := rd.read_string_reply(line)!
return strconv.parse_int(i, 10, 64)!
}
// resp_big_int {
// b := rd.read_big_int(line)!
// return '${b}'
// }
// if b !is i64 {
// error("big_int ${b} value out of range")
// }
// return b.Int64() or ProtoNil
// }
else {
return new_redict_error("Can't parse int reply: ${line}")
}
}
}
fn (mut rd ProtoReader) read_array_len() !int {
line := rd.read_line()!
match line[0].ascii_str() {
resp_array, resp_set, resp_push {
return reply_len(line)
}
else {
return new_redict_error("can't parse array/set/push reply: ${line}")
}
}
}
fn (mut rd ProtoReader) read_fixed_array_len(fixed_len int) ! {
n := rd.read_array_len()!
if n != fixed_len {
return new_redict_error('got ${n} elements in the array, but expected ${fixed_len}')
}
}
fn (mut rd ProtoReader) read_map_len() !int {
line := rd.read_line()!
match line[0].ascii_str() {
resp_map {
return reply_len(line)!
}
resp_array, resp_set, resp_push {
// Some commands may respond to array types.
n := reply_len(line)!
if n % 2 != 0 {
return new_redict_error('The length of the array must be a multiple of 2, got: ${n}')
}
return n / 2
}
else {
return new_redict_error("Can't parse map reply: ${line}")
}
}
}
fn (mut rd ProtoReader) peek_reply_type() !u8 {
b := rd.reader.peek(1)!
if b.bytestr() == resp_attr {
rd.discard_next()!
return rd.peek_reply_type()!
}
return b[0]
}