forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
166 lines (137 loc) · 3.17 KB
/
result.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
package tarantool
import (
"fmt"
"github.com/tinylib/msgp/msgp"
)
type Result struct {
RawBytes []byte
ErrorCode uint
Error error
Data [][]interface{}
marshaller msgp.Marshaler
}
func (r *Result) GetCommandID() uint {
if r.Error != nil {
return r.ErrorCode | ErrorFlag
}
return r.ErrorCode
}
// MarshalMsg implements msgp.Marshaler
func (r *Result) MarshalMsg(b []byte) (o []byte, err error) {
if r.marshaller == nil {
r.marshaller = defaultResultMarshaller{Result: r}
}
return r.marshaller.MarshalMsg(b)
}
func (r *Result) WithBytesMarshaller() *Result {
r.marshaller = bytesResultMarshaller{Result: r}
return r
}
type defaultResultMarshaller struct {
*Result
}
func (r defaultResultMarshaller) MarshalMsg(b []byte) (o []byte, err error) {
o = b
if r.Error != nil {
o = msgp.AppendMapHeader(o, 1)
o = msgp.AppendUint(o, KeyError)
o = msgp.AppendString(o, r.Error.Error())
} else {
o = msgp.AppendMapHeader(o, 1)
o = msgp.AppendUint(o, KeyData)
if r.Data != nil {
if o, err = msgp.AppendIntf(o, r.Data); err != nil {
return nil, err
}
} else {
o = msgp.AppendArrayHeader(o, 0)
}
}
return o, nil
}
type bytesResultMarshaller struct {
*Result
}
func (r bytesResultMarshaller) MarshalMsg(b []byte) (o []byte, err error) {
return append(b, r.RawBytes...), nil
}
// UnmarshalMsg implements msgp.Unmarshaler
func (r *Result) UnmarshalMsg(data []byte) (buf []byte, err error) {
var l uint32
var dl, tl uint32
var errorMessage string
var val interface{}
buf = data
// Tarantool >= 1.7.7 sends periodic heartbeat messages without body
if len(buf) == 0 && r.ErrorCode == OKCommand {
return buf, nil
}
defer func() {
if err == nil {
rawPacketLength := len(data) - len(buf)
r.RawBytes = make([]byte, rawPacketLength)
copy(r.RawBytes, data[:rawPacketLength])
}
}()
l, buf, err = msgp.ReadMapHeaderBytes(buf)
if err != nil {
return
}
for ; l > 0; l-- {
var cd uint
if cd, buf, err = msgp.ReadUintBytes(buf); err != nil {
return
}
switch cd {
case KeyData:
var i, j uint32
if dl, buf, err = msgp.ReadArrayHeaderBytes(buf); err != nil {
return
}
r.Data = make([][]interface{}, dl)
for i = 0; i < dl; i++ {
obuf := buf
if tl, buf, err = msgp.ReadArrayHeaderBytes(buf); err != nil {
buf = obuf
if _, ok := err.(msgp.TypeError); ok {
if val, buf, err = msgp.ReadIntfBytes(buf); err != nil {
return
}
r.Data[i] = []interface{}{val}
continue
}
return
}
r.Data[i] = make([]interface{}, tl)
for j = 0; j < tl; j++ {
if r.Data[i][j], buf, err = msgp.ReadIntfBytes(buf); err != nil {
return
}
}
}
case KeyError:
errorMessage, buf, err = msgp.ReadStringBytes(buf)
if err != nil {
return
}
r.Error = NewQueryError(r.ErrorCode, errorMessage)
default:
if buf, err = msgp.Skip(buf); err != nil {
return
}
}
}
return
}
func (r *Result) String() string {
switch {
case r == nil:
return "Result <nil>"
case r.Error != nil:
return fmt.Sprintf("Result ErrCode:%v, Err: %v", r.ErrorCode, r.Error)
case r.Data != nil:
return fmt.Sprintf("Result Data:%#v", r.Data)
default:
return ""
}
}