forked from gofinance/ib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.go
265 lines (232 loc) · 4.88 KB
/
wire.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
package trade
import (
"bufio"
"bytes"
"fmt"
"reflect"
"runtime"
"strconv"
"time"
)
//const ibTimeFormat = "20060102 15:04:05.000000 MST"
const ibTimeFormat = "20060102 15:04:05 MST"
func unpanic() (err error) {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = r.(error)
}
return nil
}
type EncodeError struct {
Name string
Value interface{}
Type reflect.Type
}
func (e *EncodeError) Error() string {
return fmt.Sprintf("trade: cannot encode field %s of type %v with value %v",
e.Name, e.Type, e.Value)
}
func failEncode(n string, v interface{}, t reflect.Type) error {
return &EncodeError{
Name: n,
Value: v,
Type: t,
}
}
func skipField(v reflect.Value, f reflect.StructField) bool {
// no tag on the structure field
name := f.Tag.Get("when")
if name == "" {
return false
}
// invalid field in tag
f1 := v.FieldByName(name)
if !f1.IsValid() {
return false
}
// target field is not a string
if f1.Type().Kind() != reflect.String {
return false
}
target := f1.String()
value := f.Tag.Get("value")
switch f.Tag.Get("cond") {
case "is":
// skip when target equals value
return (target == value)
case "not":
return (target != value)
}
return false
}
func encode(buf *bytes.Buffer, v reflect.Value) error {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
var s string
if !v.IsValid() {
v = reflect.ValueOf(0)
}
switch v.Type().Kind() {
case reflect.Int, reflect.Int64:
s = strconv.FormatInt(v.Int(), 10)
case reflect.Float64:
s = strconv.FormatFloat(v.Float(), 'g', 10, 64)
case reflect.String:
s = string(v.String())
case reflect.Bool:
if v.Bool() {
s = "1"
} else {
s = "0"
}
case reflect.Slice:
// encode size
type size struct {
N int64
}
sz := size{int64(v.Len())}
if err := encode(buf, reflect.ValueOf(&sz)); err != nil {
return err
}
// encode elements
for i := 0; i < v.Len(); i++ {
if err := encode(buf, v.Index(i)); err != nil {
return err
}
}
return nil
case reflect.Struct:
switch v.Interface().(type) {
// custom encoding for time
case time.Time:
var t time.Time = v.Interface().(time.Time)
s = t.Format(ibTimeFormat)
default:
// encode fields
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if skipField(v, v.Type().Field(i)) {
continue
}
if err := encode(buf, f); err != nil {
return err
}
}
}
if s == "" {
return nil
}
default:
return failEncode(v.Type().Name(), v.Interface(), v.Type())
}
if _, err := buf.WriteString(s + "\000"); err != nil {
return err
}
return nil
}
type DecodeError struct {
Data string
Name string
Type reflect.Type
}
func (e *DecodeError) Error() string {
return fmt.Sprintf("trade: cannot decode '%v' into field %s of type %v",
e.Data, e.Name, e.Type)
}
func failDecode(d string, n string, t reflect.Type) error {
return &DecodeError{
Data: d,
Name: n,
Type: t,
}
}
func decode(b *bufio.Reader, v reflect.Value) error {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
kind := v.Type().Kind()
// special processing for struct and slice
switch kind {
case reflect.Slice:
type size struct {
N int64
}
sz := size{}
if err := decode(b, reflect.ValueOf(&sz)); err != nil {
return err
}
n := int(sz.N)
v.Set(reflect.MakeSlice(v.Type(), n, n))
// decode elements
for i := 0; i < n; i++ {
if err := decode(b, v.Index(i)); err != nil {
return err
}
}
return nil
case reflect.Struct:
switch v.Interface().(type) {
case time.Time:
// do nothing
default:
// decode fields
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if skipField(v, v.Type().Field(i)) {
// string field we depend on is empty
continue
}
if err := decode(b, f); err != nil {
fmt.Printf("Failed to decode structure %v, error %s\n", v, err)
return err
}
}
return nil
}
}
var s string
if data, err := b.ReadString(0); err != nil {
return err
} else {
s = string(data[:len(data)-1]) // trim \000
}
switch kind {
case reflect.Int64:
if x, err := strconv.ParseInt(s, 10, 64); err != nil {
return failDecode(s, v.Type().Name(), v.Type())
} else {
v.SetInt(x)
}
case reflect.Float64:
if x, err := strconv.ParseFloat(s, 64); err != nil {
return failDecode(s, v.Type().Name(), v.Type())
} else {
v.SetFloat(x)
}
case reflect.String:
v.SetString(s)
case reflect.Bool:
if x, err := strconv.ParseInt(s, 10, 64); err != nil {
return failDecode(s, v.Type().Name(), v.Type())
} else {
v.SetBool(x > 0)
}
case reflect.Struct:
switch v.Interface().(type) {
case time.Time:
if x, err := time.Parse(ibTimeFormat, s); err != nil {
return failDecode(s, v.Type().Name(), v.Type())
} else {
v.Set(reflect.ValueOf(x))
}
default:
return failDecode(s, v.Type().Name(), v.Type())
}
default:
return failDecode(s, v.Type().Name(), v.Type())
}
return nil
}