forked from joomcode/redispipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
72 lines (65 loc) · 1.64 KB
/
response.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
package redis
import (
"fmt"
"github.com/joomcode/errorx"
)
// AsError casts interface to error (if it is error)
func AsError(v interface{}) error {
e, _ := v.(error)
return e
}
// AsErrorx casts interface to *errorx.Error.
// It panics if value is error but not *redis.Error.
func AsErrorx(v interface{}) *errorx.Error {
e, _ := v.(*errorx.Error)
if e == nil {
if _, ok := v.(error); ok {
panic(fmt.Errorf("result should be either *rediserror.Error, or not error at all, but got %#v", v))
}
}
return e
}
// ScanResponse parses response of Scan command, returns iterator and array of keys.
func ScanResponse(res interface{}) ([]byte, []string, error) {
if err := AsError(res); err != nil {
return nil, nil, err
}
var ok bool
var arr []interface{}
var it []byte
var keys []interface{}
var strs []string
if arr, ok = res.([]interface{}); !ok {
goto wrong
}
if it, ok = arr[0].([]byte); !ok {
goto wrong
}
if keys, ok = arr[1].([]interface{}); !ok {
goto wrong
}
strs = make([]string, len(keys))
for i, k := range keys {
var b []byte
if b, ok = k.([]byte); !ok {
goto wrong
}
strs[i] = string(b)
}
return it, strs, nil
wrong:
return nil, nil, ErrResponseUnexpected.NewWithNoMessage().WithProperty(EKResponse, res)
}
// TransactionResponse parses response of EXEC command, returns array of answers.
func TransactionResponse(res interface{}) ([]interface{}, error) {
if arr, ok := res.([]interface{}); ok {
return arr, nil
}
if res == nil {
res = ErrExecEmpty.NewWithNoMessage()
}
if _, ok := res.(error); !ok {
res = ErrResponseUnexpected.NewWithNoMessage().WithProperty(EKResponse, res)
}
return nil, res.(error)
}