Repository navigation
Expand file tree
/
Copy pathresp.go
More file actions
66 lines (53 loc) · 1.44 KB
/
Copy pathresp.go
File metadata and controls
66 lines (53 loc) · 1.44 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
package resp
import (
"errors"
)
const (
// A large INFO ALL response can be over 4kb, so we set the default to 8kb.
defaultBufferLen = 8192
// Smallest valid RESP object is ":0\r\n".
minObjectLen = 4
// The minimum valid command is a single character bulk string: "*1\r\n$1\r\nX\r\n"
minCommandLen = 11
// RESP object prefixes
simpleStringPrefix = '+'
errorPrefix = '-'
integerPrefix = ':'
bulkStringPrefix = '$'
arrayPrefix = '*'
)
var (
// Common responses
OK = NewSimpleString("OK")
PONG = NewSimpleString("PONG")
// ErrSyntaxError is returned when invalid RESP is encountered.
ErrSyntaxError = errors.New("resp: syntax error")
// ErrBufferFull is returned when a RESP object is larger than the
// buffer can accommodate.
ErrBufferFull = errors.New("resp: object is larger than buffer")
lineSuffix = []byte("\r\n")
)
type Object interface {
Raw() []byte
}
type InvalidObject []byte
func (o InvalidObject) Raw() []byte { return o }
// Parse takes a slice pointing to valid a valid RESP object and returns the
// RESP as the corresponding type.
func Parse(resp []byte) Object {
switch resp[0] {
case simpleStringPrefix:
return String(resp)
case errorPrefix:
return Error(resp)
case integerPrefix:
return Integer(resp)
case bulkStringPrefix:
return String(resp)
case arrayPrefix:
return Array(resp)
default:
// This will never happen when being used with Reader
return InvalidObject(resp)
}
}