-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.go
106 lines (85 loc) · 2.3 KB
/
command.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
package resp
import (
"bytes"
"fmt"
)
// A Command contains a RESP array of bulk strings comprising the raw command
// and string arguments.
type Command []byte
// NewCommand takes a Redis command and arguments and returns a Command byte
// slice pointing to the RESP for the command.
func NewCommand(args ...string) Command {
var buf bytes.Buffer
fmt.Fprintf(&buf, "*%d\r\n", len(args))
for _, arg := range args {
fmt.Fprintf(&buf, "$%d\r\n%s\r\n", len(arg), arg)
}
return buf.Bytes()
}
// Raw returns the underlying bytes of this RESP object.
func (c Command) Raw() []byte { return c }
// Slices returns a slice of byte slices that point to each argument in this
// Command. It returns a ErrSyntaxError error if the command RESP bytes are
// invalid.
func (c Command) Slices() ([][]byte, error) {
// Check for basic validity
if len(c) < minCommandLen || c[0] != arrayPrefix || c[len(c)-2] != '\r' || c[len(c)-1] != '\n' {
return nil, ErrSyntaxError
}
// Find the number of args
argCount, cursor, err := parseLenLine(c)
if err != nil {
return nil, err
}
args := make([][]byte, argCount)
var end, length int
for i := range args {
cursor++
if cursor >= len(c) {
return nil, ErrSyntaxError
}
length, end, err = parseLenLine(c[cursor:])
cursor += end + 1
if err != nil {
return nil, err
}
// Null bulk strings are invalid in RESP commands
if length < 0 {
return nil, ErrSyntaxError
}
if cursor+length+2 > len(c) {
return nil, ErrSyntaxError
}
args[i] = c[cursor : cursor+length]
// Move cursor to final character of current line
cursor += length + 1
}
return args, nil
}
// Bytes is the same as Slices except that it returns slices that point to
// copies of the bytes.
func (c Command) Bytes() ([][]byte, error) {
slices, err := c.Slices()
if slices == nil {
return nil, err
}
bytes := make([][]byte, len(slices))
for i, slice := range slices {
bytes[i] = make([]byte, len(slice))
copy(bytes[i], slice)
}
return bytes, err
}
// Strings is the same as Slices except that it returns strings for each
// argument.
func (c Command) Strings() ([]string, error) {
slices, err := c.Slices()
if slices == nil {
return nil, err
}
strings := make([]string, len(slices))
for i, slice := range slices {
strings[i] = string(slice)
}
return strings, err
}