-
Notifications
You must be signed in to change notification settings - Fork 556
/
reply.go
166 lines (135 loc) · 3.3 KB
/
reply.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
package protocol
import (
"bytes"
"github.com/hdt3213/godis/interface/redis"
"strconv"
)
var (
// CRLF is the line separator of redis serialization protocol
CRLF = "\r\n"
)
/* ---- Bulk Reply ---- */
// BulkReply stores a binary-safe string
type BulkReply struct {
Arg []byte
}
// MakeBulkReply creates BulkReply
func MakeBulkReply(arg []byte) *BulkReply {
return &BulkReply{
Arg: arg,
}
}
// ToBytes marshal redis.Reply
func (r *BulkReply) ToBytes() []byte {
if r.Arg == nil {
return nullBulkBytes
}
return []byte("$" + strconv.Itoa(len(r.Arg)) + CRLF + string(r.Arg) + CRLF)
}
/* ---- Multi Bulk Reply ---- */
// MultiBulkReply stores a list of string
type MultiBulkReply struct {
Args [][]byte
}
// MakeMultiBulkReply creates MultiBulkReply
func MakeMultiBulkReply(args [][]byte) *MultiBulkReply {
return &MultiBulkReply{
Args: args,
}
}
// ToBytes marshal redis.Reply
func (r *MultiBulkReply) ToBytes() []byte {
argLen := len(r.Args)
var buf bytes.Buffer
buf.WriteString("*" + strconv.Itoa(argLen) + CRLF)
for _, arg := range r.Args {
if arg == nil {
buf.WriteString("$-1" + CRLF)
} else {
buf.WriteString("$" + strconv.Itoa(len(arg)) + CRLF + string(arg) + CRLF)
}
}
return buf.Bytes()
}
/* ---- Multi Raw Reply ---- */
// MultiRawReply store complex list structure, for example GeoPos command
type MultiRawReply struct {
Replies []redis.Reply
}
// MakeMultiRawReply creates MultiRawReply
func MakeMultiRawReply(replies []redis.Reply) *MultiRawReply {
return &MultiRawReply{
Replies: replies,
}
}
// ToBytes marshal redis.Reply
func (r *MultiRawReply) ToBytes() []byte {
argLen := len(r.Replies)
var buf bytes.Buffer
buf.WriteString("*" + strconv.Itoa(argLen) + CRLF)
for _, arg := range r.Replies {
buf.Write(arg.ToBytes())
}
return buf.Bytes()
}
/* ---- Status Reply ---- */
// StatusReply stores a simple status string
type StatusReply struct {
Status string
}
// MakeStatusReply creates StatusReply
func MakeStatusReply(status string) *StatusReply {
return &StatusReply{
Status: status,
}
}
// ToBytes marshal redis.Reply
func (r *StatusReply) ToBytes() []byte {
return []byte("+" + r.Status + CRLF)
}
// IsOKReply returns true if the given protocol is +OK
func IsOKReply(reply redis.Reply) bool {
return string(reply.ToBytes()) == "+OK\r\n"
}
/* ---- Int Reply ---- */
// IntReply stores an int64 number
type IntReply struct {
Code int64
}
// MakeIntReply creates int protocol
func MakeIntReply(code int64) *IntReply {
return &IntReply{
Code: code,
}
}
// ToBytes marshal redis.Reply
func (r *IntReply) ToBytes() []byte {
return []byte(":" + strconv.FormatInt(r.Code, 10) + CRLF)
}
/* ---- Error Reply ---- */
// ErrorReply is an error and redis.Reply
type ErrorReply interface {
Error() string
ToBytes() []byte
}
// StandardErrReply represents server error
type StandardErrReply struct {
Status string
}
// MakeErrReply creates StandardErrReply
func MakeErrReply(status string) *StandardErrReply {
return &StandardErrReply{
Status: status,
}
}
// IsErrorReply returns true if the given protocol is error
func IsErrorReply(reply redis.Reply) bool {
return reply.ToBytes()[0] == '-'
}
// ToBytes marshal redis.Reply
func (r *StandardErrReply) ToBytes() []byte {
return []byte("-" + r.Status + CRLF)
}
func (r *StandardErrReply) Error() string {
return r.Status
}