forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
275 lines (236 loc) · 6.27 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
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
package redis
import (
"errors"
"strconv"
"strings"
)
// A CmdError implements the error interface and is what is returned when the
// server returns an error on the application level (e.g. key doesn't exist or
// is the wrong type), as opposed to a connection/transport error.
//
// You can test if a reply is a CmdError like so:
//
// r := conn.Cmd("GET", "key-which-isnt-a-string")
// if r.Err != nil {
// if cerr, ok := r.Err.(*redis.CmdError); ok {
// // Is CmdError
// } else {
// // Is other error
// }
// }
type CmdError struct {
Err error
}
func (cerr *CmdError) Error() string {
return cerr.Err.Error()
}
// Returns true if error returned was due to the redis server being read only
func (cerr *CmdError) Readonly() bool {
return strings.HasPrefix(cerr.Err.Error(), "READONLY")
}
//* Reply
/*
ReplyType describes type of a reply.
Possible values are:
StatusReply -- status reply
ErrorReply -- error reply
IntegerReply -- integer reply
NilReply -- nil reply
BulkReply -- bulk reply
MultiReply -- multi bulk reply
*/
type ReplyType uint8
const (
StatusReply ReplyType = iota
ErrorReply
IntegerReply
NilReply
BulkReply
MultiReply
)
// Reply holds a Redis reply.
type Reply struct {
Type ReplyType // Reply type
Elems []*Reply // Sub-replies
Err error // Reply error
buf []byte
int int64
}
// Bytes returns the reply value as a byte string or
// an error, if the reply type is not StatusReply or BulkReply.
func (r *Reply) Bytes() ([]byte, error) {
if r.Type == ErrorReply {
return nil, r.Err
}
if !(r.Type == StatusReply || r.Type == BulkReply) {
return nil, errors.New("string value is not available for this reply type")
}
return r.buf, nil
}
// Str is a convenience method for calling Reply.Bytes() and converting it to string
func (r *Reply) Str() (string, error) {
b, err := r.Bytes()
if err != nil {
return "", err
}
return string(b), nil
}
// Int64 returns the reply value as a int64 or an error,
// if the reply type is not IntegerReply or the reply type
// BulkReply could not be parsed to an int64.
func (r *Reply) Int64() (int64, error) {
if r.Type == ErrorReply {
return 0, r.Err
}
if r.Type != IntegerReply {
s, err := r.Str()
if err == nil {
i64, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, errors.New("failed to parse integer value from string value")
} else {
return i64, nil
}
}
return 0, errors.New("integer value is not available for this reply type")
}
return r.int, nil
}
// Int is a convenience method for calling Reply.Int64() and converting it to int.
func (r *Reply) Int() (int, error) {
i64, err := r.Int64()
if err != nil {
return 0, err
}
return int(i64), nil
}
// Bool returns false, if the reply value equals to 0 or "0", otherwise true; or
// an error, if the reply type is not IntegerReply or BulkReply.
func (r *Reply) Bool() (bool, error) {
if r.Type == ErrorReply {
return false, r.Err
}
i, err := r.Int()
if err == nil {
if i == 0 {
return false, nil
}
return true, nil
}
s, err := r.Str()
if err == nil {
if s == "0" {
return false, nil
}
return true, nil
}
return false, errors.New("boolean value is not available for this reply type")
}
// List returns a multi bulk reply as a slice of strings or an error.
// The reply type must be MultiReply and its elements' types must all be either BulkReply or NilReply.
// Nil elements are returned as empty strings.
// Useful for list commands.
func (r *Reply) List() ([]string, error) {
// Doing all this in two places instead of just calling ListBytes() so we don't have
// to iterate twice
if r.Type == ErrorReply {
return nil, r.Err
}
if r.Type != MultiReply {
return nil, errors.New("reply type is not MultiReply")
}
strings := make([]string, len(r.Elems))
for i, v := range r.Elems {
if v.Type == BulkReply {
strings[i] = string(v.buf)
} else if v.Type == NilReply {
strings[i] = ""
} else {
return nil, errors.New("element type is not BulkReply or NilReply")
}
}
return strings, nil
}
// ListBytes returns a multi bulk reply as a slice of bytes slices or an error.
// The reply type must be MultiReply and its elements' types must all be either BulkReply or NilReply.
// Nil elements are returned as nil.
// Useful for list commands.
func (r *Reply) ListBytes() ([][]byte, error) {
if r.Type == ErrorReply {
return nil, r.Err
}
if r.Type != MultiReply {
return nil, errors.New("reply type is not MultiReply")
}
bufs := make([][]byte, len(r.Elems))
for i, v := range r.Elems {
if v.Type == BulkReply {
bufs[i] = v.buf
} else if v.Type == NilReply {
bufs[i] = nil
} else {
return nil, errors.New("element type is not BulkReply or NilReply")
}
}
return bufs, nil
}
// Hash returns a multi bulk reply as a map[string]string or an error.
// The reply type must be MultiReply,
// it must have an even number of elements,
// they must be in a "key value key value..." order and
// values must all be either BulkReply or NilReply.
// Nil values are returned as empty strings.
// Useful for hash commands.
func (r *Reply) Hash() (map[string]string, error) {
if r.Type == ErrorReply {
return nil, r.Err
}
rmap := map[string]string{}
if r.Type != MultiReply {
return nil, errors.New("reply type is not MultiReply")
}
if len(r.Elems)%2 != 0 {
return nil, errors.New("reply has odd number of elements")
}
for i := 0; i < len(r.Elems)/2; i++ {
var val string
key, err := r.Elems[i*2].Str()
if err != nil {
return nil, errors.New("key element has no string reply")
}
v := r.Elems[i*2+1]
if v.Type == BulkReply {
val = string(v.buf)
rmap[key] = val
} else if v.Type == NilReply {
} else {
return nil, errors.New("value element type is not BulkReply or NilReply")
}
}
return rmap, nil
}
// String returns a string representation of the reply and its sub-replies.
// This method is for debugging.
// Use method Reply.Str() for reading string reply.
func (r *Reply) String() string {
switch r.Type {
case ErrorReply:
return r.Err.Error()
case StatusReply:
fallthrough
case BulkReply:
return string(r.buf)
case IntegerReply:
return strconv.FormatInt(r.int, 10)
case NilReply:
return "<nil>"
case MultiReply:
s := "[ "
for _, e := range r.Elems {
s = s + e.String() + " "
}
return s + "]"
}
// This should never execute
return ""
}