forked from liangdas/mqant
/
reply.go
294 lines (280 loc) · 6.81 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package mqrpc
import (
"errors"
"fmt"
"github.com/gogo/protobuf/proto"
"reflect"
"strconv"
)
var ErrNil = errors.New("mqrpc: nil returned")
func Int(reply interface{}, err interface{}) (int, error) {
switch e := err.(type) {
case string:
if err != "" {
return 0, fmt.Errorf(e)
}
case error:
if err != nil {
return 0, e
}
}
switch reply := reply.(type) {
case int64:
x := int(reply)
if int64(x) != reply {
return 0, strconv.ErrRange
}
return x, nil
case nil:
return 0, ErrNil
}
return 0, fmt.Errorf("mqrpc: unexpected type for Int, got type %T", reply)
}
// Int64 is a helper that converts a command reply to 64 bit integer. If err is
// not equal to nil, then Int returns 0, err. Otherwise, Int64 converts the
// reply to an int64 as follows:
//
// Reply type Result
// integer reply, nil
// bulk string parsed reply, nil
// nil 0, ErrNil
// other 0, error
func Int64(reply interface{}, err interface{}) (int64, error) {
switch e := err.(type) {
case string:
if err != "" {
return 0, fmt.Errorf(e)
}
case error:
if err != nil {
return 0, e
}
}
switch reply := reply.(type) {
case int64:
return reply, nil
case nil:
return 0, ErrNil
}
return 0, fmt.Errorf("mqrpc: unexpected type for Int64, got type %T", reply)
}
// Float64 is a helper that converts a command reply to 64 bit float. If err is
// not equal to nil, then Float64 returns 0, err. Otherwise, Float64 converts
// the reply to an int as follows:
//
// Reply type Result
// bulk string parsed reply, nil
// nil 0, ErrNil
// other 0, error
func Float64(reply interface{}, err interface{}) (float64, error) {
switch e := err.(type) {
case string:
if err != "" {
return 0, fmt.Errorf(e)
}
case error:
if err != nil {
return 0, e
}
}
switch reply := reply.(type) {
case float64:
return reply, nil
case nil:
return 0, ErrNil
}
return 0, fmt.Errorf("mqrpc: unexpected type for Float64, got type %T", reply)
}
// String is a helper that converts a command reply to a string. If err is not
// equal to nil, then String returns "", err. Otherwise String converts the
// reply to a string as follows:
//
// Reply type Result
// bulk string string(reply), nil
// simple string reply, nil
// nil "", ErrNil
// other "", error
func String(reply interface{}, err interface{}) (string, error) {
switch e := err.(type) {
case string:
if err != "" {
return "", fmt.Errorf(e)
}
case error:
if err != nil {
return "", e
}
}
switch reply := reply.(type) {
case string:
return reply, nil
case nil:
return "", ErrNil
}
return "", fmt.Errorf("mqrpc: unexpected type for String, got type %T", reply)
}
// Bytes is a helper that converts a command reply to a slice of bytes. If err
// is not equal to nil, then Bytes returns nil, err. Otherwise Bytes converts
// the reply to a slice of bytes as follows:
//
// Reply type Result
// bulk string reply, nil
// simple string []byte(reply), nil
// nil nil, ErrNil
// other nil, error
func Bytes(reply interface{}, err interface{}) ([]byte, error) {
switch e := err.(type) {
case string:
if err != "" {
return nil, fmt.Errorf(e)
}
case error:
if err != nil {
return nil, e
}
}
switch reply := reply.(type) {
case []byte:
return reply, nil
case nil:
return nil, ErrNil
}
return nil, fmt.Errorf("mqrpc: unexpected type for Bytes, got type %T", reply)
}
// Bool is a helper that converts a command reply to a boolean. If err is not
// equal to nil, then Bool returns false, err. Otherwise Bool converts the
// reply to boolean as follows:
//
// Reply type Result
// integer value != 0, nil
// bulk string strconv.ParseBool(reply)
// nil false, ErrNil
// other false, error
func Bool(reply interface{}, err interface{}) (bool, error) {
switch e := err.(type) {
case string:
if err != "" {
return false, fmt.Errorf(e)
}
case error:
if err != nil {
return false, e
}
}
switch reply := reply.(type) {
case int64:
return reply != 0, nil
case nil:
return false, ErrNil
}
return false, fmt.Errorf("mqrpc: unexpected type for Bool, got type %T", reply)
}
// StringMap is a helper that converts an array of strings (alternating key, value)
// into a map[string]string. The HGETALL and CONFIG GET commands return replies in this format.
// Requires an even number of values in result.
func StringMap(reply interface{}, err interface{}) (map[string]string, error) {
switch e := err.(type) {
case string:
if err != "" {
return nil, fmt.Errorf(e)
}
case error:
if err != nil {
return nil, e
}
}
switch reply := reply.(type) {
case map[string]string:
return reply, nil
case nil:
return nil, ErrNil
}
return nil, fmt.Errorf("mqrpc: unexpected type for Bool, got type %T", reply)
}
func InterfaceMap(reply interface{}, err interface{}) (map[string]interface{}, error) {
switch e := err.(type) {
case string:
if err != "" {
return nil, fmt.Errorf(e)
}
case error:
if err != nil {
return nil, e
}
}
switch reply := reply.(type) {
case map[string]interface{}:
return reply, nil
case nil:
return nil, ErrNil
}
return nil, fmt.Errorf("mqrpc: unexpected type for Bool, got type %T", reply)
}
func Marshal(mrsp interface{}, ff func() (reply interface{}, err interface{})) error {
reply, err := ff()
switch e := err.(type) {
case string:
if err != "" {
return fmt.Errorf(e)
}
case error:
if err != nil {
return e
}
}
rv := reflect.ValueOf(mrsp)
if rv.Kind() != reflect.Ptr {
//不是指针
return fmt.Errorf("mrsp [%v] not *mqrpc.marshaler pointer type", rv.Type())
} else {
if v2, ok := mrsp.(Marshaler); ok {
switch r := reply.(type) {
case []byte:
err := v2.Unmarshal(r)
if err != nil {
return err
}
return nil
case nil:
return ErrNil
}
} else {
return fmt.Errorf("mrsp [%v] not *mqrpc.marshaler type", rv.Type())
}
}
return fmt.Errorf("mqrpc: unexpected type for %v, got type %T", reflect.ValueOf(reply), reply)
}
func Proto(mrsp interface{}, ff func() (reply interface{}, err interface{})) error {
reply, err := ff()
switch e := err.(type) {
case string:
if err != "" {
return fmt.Errorf(e)
}
case error:
if err != nil {
return e
}
}
rv := reflect.ValueOf(mrsp)
if rv.Kind() != reflect.Ptr {
//不是指针
return fmt.Errorf("mrsp [%v] not *proto.Message pointer type", rv.Type())
} else {
if v2, ok := mrsp.(proto.Message); ok {
switch r := reply.(type) {
case []byte:
err := proto.Unmarshal(r, v2)
if err != nil {
return err
}
return nil
case nil:
return ErrNil
}
} else {
return fmt.Errorf("mrsp [%v] not *proto.Message type", rv.Type())
}
}
return fmt.Errorf("mqrpc: unexpected type for %v, got type %T", reflect.ValueOf(reply), reply)
}