-
Notifications
You must be signed in to change notification settings - Fork 4
/
response.go
148 lines (140 loc) · 2.68 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
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
package tarantool
import (
"fmt"
"github.com/vmihailenco/msgpack/v5"
)
type response struct {
requestID uint32
code uint32
error string
buf smallBuf
// Deserialized data for untyped requests.
data []interface{}
}
func (resp *response) smallInt(d *msgpack.Decoder) (i int, err error) {
b, err := resp.buf.ReadByte()
if err != nil {
return
}
if b <= 127 {
return int(b), nil
}
err = resp.buf.UnreadByte()
if err != nil {
return
}
return d.DecodeInt()
}
func (resp *response) decodeHeader(d *msgpack.Decoder) (err error) {
var l int
d.Reset(&resp.buf)
if l, err = d.DecodeMapLen(); err != nil {
return
}
for ; l > 0; l-- {
var cd int
if cd, err = resp.smallInt(d); err != nil {
return
}
switch cd {
case KeySync:
var rid uint64
if rid, err = d.DecodeUint64(); err != nil {
return
}
resp.requestID = uint32(rid)
case KeyCode:
var respCode uint64
if respCode, err = d.DecodeUint64(); err != nil {
return
}
resp.code = uint32(respCode)
default:
if err = d.Skip(); err != nil {
return
}
}
}
return nil
}
func (resp *response) decodeBody() (err error) {
if resp.buf.Len() > 2 {
var l int
d := getDecoder(&resp.buf)
defer putDecoder(d)
if l, err = d.DecodeMapLen(); err != nil {
return err
}
for ; l > 0; l-- {
var cd int
if cd, err = resp.smallInt(d); err != nil {
return err
}
switch cd {
case KeyData:
var res interface{}
var ok bool
if res, err = d.DecodeInterface(); err != nil {
return err
}
if resp.data, ok = res.([]interface{}); !ok {
return fmt.Errorf("result is not array: %v", res)
}
case KeyError:
if resp.error, err = d.DecodeString(); err != nil {
return err
}
default:
if err = d.Skip(); err != nil {
return err
}
}
}
if resp.code == KeyPush {
return
}
if resp.code != okCode {
resp.code &^= errorCodeBit
err = Error{resp.code, resp.error}
}
}
return
}
func (resp *response) decodeBodyTyped(res interface{}) (err error) {
if resp.buf.Len() > 0 {
var l int
d := getDecoder(&resp.buf)
defer putDecoder(d)
if l, err = d.DecodeMapLen(); err != nil {
return err
}
for ; l > 0; l-- {
var cd int
if cd, err = resp.smallInt(d); err != nil {
return err
}
switch cd {
case KeyData:
if err = d.Decode(res); err != nil {
return err
}
case KeyError:
if resp.error, err = d.DecodeString(); err != nil {
return err
}
default:
if err = d.Skip(); err != nil {
return err
}
}
}
if resp.code == KeyPush {
return
}
if resp.code != okCode {
resp.code &^= errorCodeBit
err = Error{resp.code, resp.error}
}
}
return
}