forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_map.go
193 lines (157 loc) · 3.82 KB
/
field_map.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
package quickfix
import (
"bytes"
"math"
"sort"
)
//FieldMap is a collection of fix fields that make up a fix message.
type FieldMap struct {
tagLookup map[Tag][]tagValue
tagOrder
}
// tagOrder true if tag i should occur before tag j
type tagOrder func(i, j Tag) bool
type tagSort struct {
tags []Tag
compare tagOrder
}
func (t tagSort) Len() int { return len(t.tags) }
func (t tagSort) Swap(i, j int) { t.tags[i], t.tags[j] = t.tags[j], t.tags[i] }
func (t tagSort) Less(i, j int) bool { return t.compare(t.tags[i], t.tags[j]) }
//in the message header, the first 3 tags in the message header must be 8,9,35
func headerFieldOrder(i, j Tag) bool {
var ordering = func(t Tag) uint32 {
switch t {
case tagBeginString:
return 1
case tagBodyLength:
return 2
case tagMsgType:
return 3
}
return math.MaxUint32
}
orderi := ordering(i)
orderj := ordering(j)
switch {
case orderi < orderj:
return true
case orderi > orderj:
return false
}
return i < j
}
// In the body, ascending tags
func normalFieldOrder(i, j Tag) bool { return i < j }
// In the trailer, CheckSum (tag 10) must be last
func trailerFieldOrder(i, j Tag) bool {
switch {
case i == tagCheckSum:
return false
case j == tagCheckSum:
return true
}
return i < j
}
func (m *FieldMap) init(ordering tagOrder) {
m.tagLookup = make(map[Tag][]tagValue)
m.tagOrder = ordering
}
func (m FieldMap) Tags() []Tag {
tags := make([]Tag, 0, len(m.tagLookup))
for t := range m.tagLookup {
tags = append(tags, t)
}
return tags
}
func (m FieldMap) Get(parser Field) MessageRejectError {
return m.GetField(parser.Tag(), parser)
}
func (m FieldMap) Has(tag Tag) bool {
_, ok := m.tagLookup[tag]
return ok
}
func (m FieldMap) GetField(tag Tag, parser FieldValueReader) MessageRejectError {
tagValues, ok := m.tagLookup[tag]
if !ok {
return ConditionallyRequiredFieldMissing(tag)
}
if err := parser.Read(tagValues[0].Value); err != nil {
return incorrectDataFormatForValue(tag)
}
return nil
}
func (m FieldMap) GetGroup(parser *RepeatingGroup) MessageRejectError {
tagValues, ok := m.tagLookup[parser.Tag]
if !ok {
return ConditionallyRequiredFieldMissing(parser.Tag)
}
if _, err := parser.read(tagValues); err != nil {
return incorrectDataFormatForValue(parser.Tag)
}
return nil
}
func (m FieldMap) SetField(tag Tag, field FieldValueWriter) FieldMap {
tValues := make([]tagValue, 1)
tValues[0].init(tag, field.Write())
m.tagLookup[tag] = tValues
return m
}
//Clear purges all fields from field map
func (m *FieldMap) Clear() {
m.tagLookup = make(map[Tag][]tagValue)
}
func (m FieldMap) Set(field FieldWriter) FieldMap {
tValues := make([]tagValue, 1)
tValues[0].init(field.Tag(), field.Write())
m.tagLookup[field.Tag()] = tValues
return m
}
func (m FieldMap) SetGroup(field RepeatingGroup) FieldMap {
m.tagLookup[field.Tag] = field.tagValues()
return m
}
func (m FieldMap) sortedTags() []Tag {
sortedTags := make([]Tag, len(m.tagLookup))
for tag := range m.tagLookup {
sortedTags = append(sortedTags, tag)
}
sort.Sort(tagSort{sortedTags, m.tagOrder})
return sortedTags
}
func (m FieldMap) write(buffer *bytes.Buffer) {
tags := m.sortedTags()
for _, tag := range tags {
if fields, ok := m.tagLookup[tag]; ok {
for _, tv := range fields {
buffer.Write(tv.bytes)
}
}
}
}
func (m FieldMap) total() int {
total := 0
for _, fields := range m.tagLookup {
for _, tv := range fields {
switch tv.Tag {
case tagCheckSum: //tag does not contribute to total
default:
total += tv.total()
}
}
}
return total
}
func (m FieldMap) length() int {
length := 0
for _, fields := range m.tagLookup {
for _, tv := range fields {
switch tv.Tag {
case tagBeginString, tagBodyLength, tagCheckSum: //tags do not contribute to length
default:
length += tv.length()
}
}
}
return length
}