forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
145 lines (127 loc) · 3.56 KB
/
unmarshal.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
package quickfix
import (
"reflect"
"time"
)
type decoder struct {
FieldMap
}
func (d decoder) decodeValue(fixTag Tag, t reflect.Type, v reflect.Value) MessageRejectError {
switch v.Kind() {
case reflect.Ptr:
v.Set(reflect.New(t.Elem()))
return d.decodeValue(fixTag, t.Elem(), v.Elem())
case reflect.Struct:
switch v.Interface().(type) {
case time.Time:
fieldValue := new(FIXUTCTimestamp)
if err := d.FieldMap.GetField(Tag(fixTag), fieldValue); err != nil {
return err
}
v.Set(reflect.ValueOf(fieldValue.Value))
}
case reflect.String:
var fieldValue FIXString
if err := d.FieldMap.GetField(Tag(fixTag), &fieldValue); err != nil {
return err
}
v.SetString(string(fieldValue))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
var fieldValue FIXInt
if err := d.FieldMap.GetField(Tag(fixTag), &fieldValue); err != nil {
return err
}
v.SetInt(int64(fieldValue))
case reflect.Bool:
var fieldValue FIXBoolean
if err := d.FieldMap.GetField(Tag(fixTag), &fieldValue); err != nil {
return err
}
v.SetBool(bool(fieldValue))
case reflect.Float32, reflect.Float64:
var fieldValue FIXFloat
if err := d.FieldMap.GetField(Tag(fixTag), &fieldValue); err != nil {
return err
}
v.SetFloat(float64(fieldValue))
case reflect.Slice:
elem := v.Type().Elem()
if elem.Kind() != reflect.Struct {
panic("repeating group must be a slice of type struct")
}
template := make(GroupTemplate, elem.NumField())
for i := 0; i < elem.NumField(); i++ {
sf := elem.Field(i)
fixTag, _, _ := parseStructTag(sf.Tag.Get("fix"))
template[i] = GroupElement(fixTag)
}
repeatingGroup := RepeatingGroup{Tag: fixTag, GroupTemplate: template}
if err := d.FieldMap.GetGroup(&repeatingGroup); err != nil {
return err
}
v.Set(reflect.MakeSlice(v.Type(), len(repeatingGroup.Groups), len(repeatingGroup.Groups)))
for i := 0; i < len(repeatingGroup.Groups); i++ {
groupDecoder := decoder{FieldMap: repeatingGroup.Groups[i].FieldMap}
for j := 0; j < v.Type().Elem().NumField(); j++ {
sf := v.Type().Elem().Field(j)
sv := v.Index(i).Field(j)
if err := groupDecoder.decodeField(sf, sf.Type, sv); err != nil {
return err
}
}
}
}
return nil
}
func (d decoder) decodeField(sf reflect.StructField, t reflect.Type, v reflect.Value) MessageRejectError {
if sf.Tag.Get("fix") != "" {
fixTag, _, _ := parseStructTag(sf.Tag.Get("fix"))
if !d.FieldMap.Has(fixTag) {
return nil
}
return d.decodeValue(Tag(fixTag), t, v)
}
switch t.Kind() {
case reflect.Ptr:
v.Set(reflect.New(t.Elem()))
return d.decodeField(sf, t.Elem(), v.Elem())
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
if err := d.decodeField(t.Field(i), t.Field(i).Type, v.Field(i)); err != nil {
return err
}
}
}
return nil
}
// Unmarshal populates v from a Message
func Unmarshal(m Message, v interface{}) MessageRejectError {
reflectValue := reflect.ValueOf(v)
switch reflectValue.Kind() {
case reflect.Ptr:
reflectValue = reflectValue.Elem()
default:
panic("v not a pointer")
}
reflectType := reflectValue.Type()
d := decoder{}
for i := 0; i < reflectType.NumField(); i++ {
sf := reflectType.Field(i)
sv := reflectValue.Field(i)
switch sf.Name {
case "FIXMsgType":
continue
case "Header":
d.FieldMap = m.Header
case "Trailer":
d.FieldMap = m.Trailer
default:
d.FieldMap = m.Body
}
if err := d.decodeField(sf, sf.Type, sv); err != nil {
return err
}
}
return nil
}