forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
135 lines (114 loc) · 3.04 KB
/
marshal.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
package quickfix
import (
"reflect"
"strconv"
"time"
)
type encoder struct {
FieldMap
}
func (e encoder) encodeField(f reflect.StructField, t reflect.Type, v reflect.Value) {
if f.Tag.Get("fix") != "" {
tag, omitEmpty, defaultVal := parseStructTag(f.Tag.Get("fix"))
e.encodeValue(tag, v, omitEmpty, defaultVal)
}
switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
return
}
e.encodeField(f, v.Elem().Type(), v.Elem())
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
e.encodeField(t.Field(i), t.Field(i).Type, v.Field(i))
}
}
}
func (e encoder) encodeValue(fixTag Tag, v reflect.Value, omitEmpty bool, defaultVal *string) {
if defaultVal != nil {
e.FieldMap.SetField(fixTag, FIXString(*defaultVal))
return
}
switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
return
}
e.encodeValue(fixTag, v.Elem(), omitEmpty, defaultVal)
case reflect.Struct:
switch t := v.Interface().(type) {
case time.Time:
e.FieldMap.SetField(fixTag, FIXUTCTimestamp{Value: t})
}
case reflect.String:
e.FieldMap.SetField(fixTag, FIXString(v.String()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
e.FieldMap.SetField(fixTag, FIXInt(v.Int()))
case reflect.Bool:
e.FieldMap.SetField(fixTag, FIXBoolean(v.Bool()))
case reflect.Float32, reflect.Float64:
e.FieldMap.SetField(fixTag, FIXFloat(v.Float()))
case reflect.Slice:
if v.Len() == 0 && omitEmpty {
return
}
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, err := strconv.Atoi(sf.Tag.Get("fix"))
if err != nil {
panic(err)
}
template[i] = GroupElement(Tag(fixTag))
}
repeatingGroup := RepeatingGroup{Tag: fixTag, GroupTemplate: template}
for i := 0; i < v.Len(); i++ {
group := repeatingGroup.Add()
groupEncoder := encoder{FieldMap: group.FieldMap}
sliceV := v.Index(i)
for i := 0; i < sliceV.NumField(); i++ {
sf := sliceV.Type().Field(i)
groupEncoder.encodeField(sf, sf.Type, sliceV.Field(i))
}
}
e.FieldMap.SetGroup(repeatingGroup)
}
}
// Marshal returns the Message encoding of v
func Marshal(v interface{}) Message {
m := NewMessage()
reflectValue := reflect.ValueOf(v)
if !reflectValue.IsValid() {
panic("Message value is invalid")
}
var msgType string
reflectType := reflectValue.Type()
e := encoder{}
for i := 0; i < reflectType.NumField(); i++ {
sf := reflectType.Field(i)
switch sf.Name {
case "FIXMsgType":
msgType = sf.Tag.Get("fix")
if msgType == "" {
panic("FIXMsgType has no Tag Value")
}
continue
case "Header":
e.FieldMap = m.Header
case "Trailer":
e.FieldMap = m.Trailer
default:
e.FieldMap = m.Body
}
e.encodeField(sf, sf.Type, reflectValue.Field(i))
}
if msgType != "" {
m.Header.SetField(tagMsgType, FIXString(msgType))
}
return m
}