forked from bpowers/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
239 lines (202 loc) · 5.15 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
package redis
import (
"errors"
"strconv"
)
//* Reply
/*
ReplyType describes type of a reply.
Possbile values are:
ReplyStatus -- status reply
ReplyError -- error reply
ReplyInteger -- integer reply
ReplyNil -- nil reply
ReplyString -- string reply (bulk reply)
ReplyMulti -- multi-bulk or multicall reply
*/
type ReplyType uint8
const (
ReplyStatus ReplyType = iota
ReplyError
ReplyInteger
ReplyNil
ReplyString
ReplyMulti
)
// Reply holds a Redis reply.
type Reply struct {
Type ReplyType // Reply type
Elems []*Reply // Sub-replies
Err *Error // Reply error
str string
int int64
}
// Str returns the reply value as a string or
// an error, if the reply type is not ReplyStatus or ReplyString.
func (r *Reply) Str() (string, error) {
if r.Type == ReplyError {
return "", r.Err
}
if !(r.Type == ReplyStatus || r.Type == ReplyString) {
return "", errors.New("string value is not available for this reply type")
}
return r.str, nil
}
// Bytes is a convenience method for calling Reply.Str() and converting it to []byte.
func (r *Reply) Bytes() ([]byte, error) {
if r.Type == ReplyError {
return nil, r.Err
}
s, err := r.Str()
if err != nil {
return nil, err
}
return []byte(s), nil
}
// Int64 returns the reply value as a int64 or an error,
// if the reply type is not ReplyInteger or the reply type
// ReplyString could not be parsed to an int64.
func (r *Reply) Int64() (int64, error) {
if r.Type == ReplyError {
return 0, r.Err
}
if r.Type != ReplyInteger {
if r.Type == ReplyString {
i64, err := strconv.ParseInt(r.str, 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) {
if r.Type == ReplyError {
return 0, r.Err
}
i64, err := r.Int64()
if err != nil {
return 0, err
}
return int(i64), nil
}
// Bool returns true, if the reply value equals to 1 or "1", otherwise false; or
// an error, if the reply type is not ReplyInteger or ReplyString.
func (r *Reply) Bool() (bool, error) {
if r.Type == ReplyError {
return false, r.Err
}
i, err := r.Int()
if err == nil {
if i == 1 {
return true, nil
}
return false, nil
}
s, err := r.Str()
if err == nil {
if s == "1" {
return true, nil
}
return false, 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 ReplyMulti and its elements' types must all be either ReplyString or ReplyNil.
// Nil elements are returned as empty strings.
// Useful for list commands.
func (r *Reply) List() ([]string, error) {
if r.Type == ReplyError {
return nil, r.Err
}
if r.Type != ReplyMulti {
return nil, errors.New("reply type is not ReplyMulti")
}
strings := make([]string, len(r.Elems))
for i, v := range r.Elems {
if v.Type == ReplyString {
strings[i] = v.str
} else if v.Type == ReplyNil {
strings[i] = ""
} else {
return nil, errors.New("element type is not ReplyString or ReplyNil")
}
}
return strings, nil
}
// Map returns a multi-bulk reply as a map[string]string or an error.
// The reply type must be ReplyMulti,
// it must have an even number of elements,
// they must be in a "key value key value..." order and
// values must all be either ReplyString or ReplyNil.
// Nil values are returned as empty strings.
// Useful for hash commands.
func (r *Reply) Hash() (map[string]string, error) {
if r.Type == ReplyError {
return nil, r.Err
}
rmap := map[string]string{}
if r.Type != ReplyMulti {
return nil, errors.New("reply type is not ReplyMulti")
}
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 == ReplyString {
val = v.str
rmap[key] = val
} else if v.Type == ReplyNil {
} else {
return nil, errors.New("value element type is not ReplyString or ReplyNil")
}
}
return rmap, nil
}
// String returns a string representation of the reply and its sub-replies.
// This method is mainly used for debugging.
// Use method Reply.Str() for fetching a string reply.
func (r *Reply) String() string {
switch r.Type {
case ReplyError:
return r.Err.Error()
case ReplyStatus:
fallthrough
case ReplyString:
return r.str
case ReplyInteger:
return strconv.FormatInt(r.int, 10)
case ReplyNil:
return "<nil>"
case ReplyMulti:
s := "[ "
for _, e := range r.Elems {
s = s + e.String() + " "
}
return s + "]"
}
// This should never execute
return ""
}
//* Future
// Future is a channel for fetching the reply of an asynchronous call.
type Future chan *Reply
func newFuture() Future {
return make(chan *Reply, 1)
}
// Reply returns the reply of the Future.
// It blocks until the reply is available.
func (f Future) Reply() *Reply {
return <-f
}