-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathresult.go
125 lines (109 loc) · 2.44 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
package tarantool
import (
"fmt"
"github.com/tinylib/msgp/msgp"
)
type Result struct {
ErrorCode uint
Error error
Data [][]interface{}
}
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) {
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
}
// 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
}
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 ""
}
}