@@ -7,25 +7,18 @@ import (
77)
88
99//field stores a slice of TagValues
10- type field struct {
11- tvs []TagValue
12- }
10+ type field []TagValue
1311
14- func (f * field ) Tag ( ) Tag {
15- return f . tvs [0 ].tag
12+ func fieldTag (f field ) Tag {
13+ return f [0 ].tag
1614}
1715
18- func (f * field ) init (tag Tag , value []byte ) {
19- if len (f .tvs ) == 0 {
20- f .tvs = make ([]TagValue , 1 )
21- } else {
22- f .tvs = f .tvs [:1 ]
23- }
24- f .tvs [0 ].init (tag , value )
16+ func initField (f field , tag Tag , value []byte ) {
17+ f [0 ].init (tag , value )
2518}
2619
27- func (f * field ) write ( buffer * bytes.Buffer ) {
28- for _ , tv := range f . tvs {
20+ func writeField (f field , buffer * bytes.Buffer ) {
21+ for _ , tv := range f {
2922 buffer .Write (tv .bytes )
3023 }
3124}
@@ -44,7 +37,7 @@ func (t tagSort) Less(i, j int) bool { return t.compare(t.tags[i], t.tags[j]) }
4437
4538//FieldMap is a collection of fix fields that make up a fix message.
4639type FieldMap struct {
47- tagLookup map [Tag ]* field
40+ tagLookup map [Tag ]field
4841 tagSort
4942}
5043
@@ -56,7 +49,7 @@ func (m *FieldMap) init() {
5649}
5750
5851func (m * FieldMap ) initWithOrdering (ordering tagOrder ) {
59- m .tagLookup = make (map [Tag ]* field )
52+ m .tagLookup = make (map [Tag ]field )
6053 m .compare = ordering
6154}
6255
@@ -88,7 +81,7 @@ func (m FieldMap) GetField(tag Tag, parser FieldValueReader) MessageRejectError
8881 return ConditionallyRequiredFieldMissing (tag )
8982 }
9083
91- if err := parser .Read (f . tvs [0 ].value ); err != nil {
84+ if err := parser .Read (f [0 ].value ); err != nil {
9285 return IncorrectDataFormatForValue (tag )
9386 }
9487
@@ -102,7 +95,7 @@ func (m FieldMap) GetBytes(tag Tag) ([]byte, MessageRejectError) {
10295 return nil , ConditionallyRequiredFieldMissing (tag )
10396 }
10497
105- return f . tvs [0 ].value , nil
98+ return f [0 ].value , nil
10699}
107100
108101//GetBool is a GetField wrapper for bool fields
@@ -160,7 +153,7 @@ func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
160153 return ConditionallyRequiredFieldMissing (parser .Tag ())
161154 }
162155
163- if _ , err := parser .Read (f . tvs ); err != nil {
156+ if _ , err := parser .Read (f ); err != nil {
164157 if msgRejErr , ok := err .(MessageRejectError ); ok {
165158 return msgRejErr
166159 }
@@ -178,7 +171,7 @@ func (m *FieldMap) SetField(tag Tag, field FieldValueWriter) *FieldMap {
178171//SetBytes sets bytes
179172func (m * FieldMap ) SetBytes (tag Tag , value []byte ) * FieldMap {
180173 f := m .getOrCreate (tag )
181- f . init ( tag , value )
174+ initField ( f , tag , value )
182175 return m
183176}
184177
@@ -206,20 +199,22 @@ func (m *FieldMap) Clear() {
206199 }
207200}
208201
209- func (m * FieldMap ) add (f * field ) {
210- if _ , ok := m .tagLookup [f .Tag ()]; ! ok {
211- m .tags = append (m .tags , f .Tag ())
202+ func (m * FieldMap ) add (f field ) {
203+ t := fieldTag (f )
204+ if _ , ok := m .tagLookup [t ]; ! ok {
205+ m .tags = append (m .tags , t )
212206 }
213207
214- m .tagLookup [f . Tag () ] = f
208+ m .tagLookup [t ] = f
215209}
216210
217- func (m * FieldMap ) getOrCreate (tag Tag ) * field {
211+ func (m * FieldMap ) getOrCreate (tag Tag ) field {
218212 if f , ok := m .tagLookup [tag ]; ok {
213+ f = f [:1 ]
219214 return f
220215 }
221216
222- f := new (field )
217+ f := make (field , 1 )
223218 m .tagLookup [tag ] = f
224219 m .tags = append (m .tags , tag )
225220 return f
@@ -228,14 +223,17 @@ func (m *FieldMap) getOrCreate(tag Tag) *field {
228223//Set is a setter for fields
229224func (m * FieldMap ) Set (field FieldWriter ) * FieldMap {
230225 f := m .getOrCreate (field .Tag ())
231- f . init ( field .Tag (), field .Write ())
226+ initField ( f , field .Tag (), field .Write ())
232227 return m
233228}
234229
235230//SetGroup is a setter specific to group fields
236231func (m * FieldMap ) SetGroup (field FieldGroupWriter ) * FieldMap {
237- f := m .getOrCreate (field .Tag ())
238- f .tvs = field .Write ()
232+ _ , ok := m .tagLookup [field .Tag ()]
233+ if ! ok {
234+ m .tags = append (m .tags , field .Tag ())
235+ }
236+ m .tagLookup [field .Tag ()] = field .Write ()
239237 return m
240238}
241239
@@ -247,15 +245,15 @@ func (m *FieldMap) sortedTags() []Tag {
247245func (m FieldMap ) write (buffer * bytes.Buffer ) {
248246 for _ , tag := range m .sortedTags () {
249247 if f , ok := m .tagLookup [tag ]; ok {
250- f . write ( buffer )
248+ writeField ( f , buffer )
251249 }
252250 }
253251}
254252
255253func (m FieldMap ) total () int {
256254 total := 0
257255 for _ , fields := range m .tagLookup {
258- for _ , tv := range fields . tvs {
256+ for _ , tv := range fields {
259257 switch tv .tag {
260258 case tagCheckSum : //tag does not contribute to total
261259 default :
@@ -270,7 +268,7 @@ func (m FieldMap) total() int {
270268func (m FieldMap ) length () int {
271269 length := 0
272270 for _ , fields := range m .tagLookup {
273- for _ , tv := range fields . tvs {
271+ for _ , tv := range fields {
274272 switch tv .tag {
275273 case tagBeginString , tagBodyLength , tagCheckSum : //tags do not contribute to length
276274 default :
0 commit comments