@@ -6,15 +6,30 @@ import (
66 "time"
77)
88
9- //tagValues stores a slice of TagValues
10- type tagValues struct {
9+ //field stores a slice of TagValues
10+ type field struct {
1111 tvs []TagValue
1212}
1313
14- func (f * tagValues ) Tag () Tag {
14+ func (f * field ) Tag () Tag {
1515 return f .tvs [0 ].tag
1616}
1717
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 )
25+ }
26+
27+ func (f * field ) write (buffer * bytes.Buffer ) {
28+ for _ , tv := range f .tvs {
29+ buffer .Write (tv .bytes )
30+ }
31+ }
32+
1833// tagOrder true if tag i should occur before tag j
1934type tagOrder func (i , j Tag ) bool
2035
@@ -29,7 +44,7 @@ func (t tagSort) Less(i, j int) bool { return t.compare(t.tags[i], t.tags[j]) }
2944
3045//FieldMap is a collection of fix fields that make up a fix message.
3146type FieldMap struct {
32- tagLookup map [Tag ]* tagValues
47+ tagLookup map [Tag ]* field
3348 tagSort
3449}
3550
@@ -41,7 +56,7 @@ func (m *FieldMap) init() {
4156}
4257
4358func (m * FieldMap ) initWithOrdering (ordering tagOrder ) {
44- m .tagLookup = make (map [Tag ]* tagValues )
59+ m .tagLookup = make (map [Tag ]* field )
4560 m .compare = ordering
4661}
4762
@@ -68,12 +83,12 @@ func (m FieldMap) Has(tag Tag) bool {
6883
6984//GetField parses of a field with Tag tag. Returned reject may indicate the field is not present, or the field value is invalid.
7085func (m FieldMap ) GetField (tag Tag , parser FieldValueReader ) MessageRejectError {
71- tagValues , ok := m .tagLookup [tag ]
86+ f , ok := m .tagLookup [tag ]
7287 if ! ok {
7388 return ConditionallyRequiredFieldMissing (tag )
7489 }
7590
76- if err := parser .Read (tagValues .tvs [0 ].value ); err != nil {
91+ if err := parser .Read (f .tvs [0 ].value ); err != nil {
7792 return IncorrectDataFormatForValue (tag )
7893 }
7994
@@ -82,12 +97,12 @@ func (m FieldMap) GetField(tag Tag, parser FieldValueReader) MessageRejectError
8297
8398//GetBytes is a zero-copy GetField wrapper for []bytes fields
8499func (m FieldMap ) GetBytes (tag Tag ) ([]byte , MessageRejectError ) {
85- tagValues , ok := m .tagLookup [tag ]
100+ f , ok := m .tagLookup [tag ]
86101 if ! ok {
87102 return nil , ConditionallyRequiredFieldMissing (tag )
88103 }
89104
90- return tagValues .tvs [0 ].value , nil
105+ return f .tvs [0 ].value , nil
91106}
92107
93108//GetBool is a GetField wrapper for bool fields
@@ -140,12 +155,12 @@ func (m FieldMap) GetString(tag Tag) (string, MessageRejectError) {
140155
141156//GetGroup is a Get function specific to Group Fields.
142157func (m FieldMap ) GetGroup (parser FieldGroupReader ) MessageRejectError {
143- tagValues , ok := m .tagLookup [parser .Tag ()]
158+ f , ok := m .tagLookup [parser .Tag ()]
144159 if ! ok {
145160 return ConditionallyRequiredFieldMissing (parser .Tag ())
146161 }
147162
148- if _ , err := parser .Read (tagValues .tvs ); err != nil {
163+ if _ , err := parser .Read (f .tvs ); err != nil {
149164 if msgRejErr , ok := err .(MessageRejectError ); ok {
150165 return msgRejErr
151166 }
@@ -157,9 +172,13 @@ func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
157172
158173//SetField sets the field with Tag tag
159174func (m * FieldMap ) SetField (tag Tag , field FieldValueWriter ) * FieldMap {
160- tValues := m .getOrCreate (tag )
161- tValues .tvs = make ([]TagValue , 1 )
162- tValues .tvs [0 ].init (tag , field .Write ())
175+ return m .SetBytes (tag , field .Write ())
176+ }
177+
178+ //SetBytes sets bytes
179+ func (m * FieldMap ) SetBytes (tag Tag , value []byte ) * FieldMap {
180+ f := m .getOrCreate (tag )
181+ f .init (tag , value )
163182 return m
164183}
165184
@@ -170,12 +189,13 @@ func (m *FieldMap) SetBool(tag Tag, value bool) *FieldMap {
170189
171190//SetInt is a SetField wrapper for int fields
172191func (m * FieldMap ) SetInt (tag Tag , value int ) * FieldMap {
173- return m .SetField (tag , FIXInt (value ))
192+ v := FIXInt (value )
193+ return m .SetBytes (tag , v .Write ())
174194}
175195
176196//SetString is a SetField wrapper for string fields
177197func (m * FieldMap ) SetString (tag Tag , value string ) * FieldMap {
178- return m .SetField (tag , FIXString (value ))
198+ return m .SetBytes (tag , [] byte (value ))
179199}
180200
181201//Clear purges all fields from field map
@@ -186,31 +206,29 @@ func (m *FieldMap) Clear() {
186206 }
187207}
188208
189- func (m * FieldMap ) add (f * tagValues ) {
209+ func (m * FieldMap ) add (f * field ) {
190210 if _ , ok := m .tagLookup [f .Tag ()]; ! ok {
191211 m .tags = append (m .tags , f .Tag ())
192212 }
193213
194214 m .tagLookup [f .Tag ()] = f
195215}
196216
197- func (m * FieldMap ) getOrCreate (tag Tag ) * tagValues {
217+ func (m * FieldMap ) getOrCreate (tag Tag ) * field {
198218 if f , ok := m .tagLookup [tag ]; ok {
199219 return f
200220 }
201221
202- f := new (tagValues )
222+ f := new (field )
203223 m .tagLookup [tag ] = f
204224 m .tags = append (m .tags , tag )
205225 return f
206226}
207227
208228//Set is a setter for fields
209229func (m * FieldMap ) Set (field FieldWriter ) * FieldMap {
210- tValues := m .getOrCreate (field .Tag ())
211- tValues .tvs = make ([]TagValue , 1 )
212- tValues .tvs [0 ].init (field .Tag (), field .Write ())
213- m .tagLookup [field .Tag ()] = tValues
230+ f := m .getOrCreate (field .Tag ())
231+ f .init (field .Tag (), field .Write ())
214232 return m
215233}
216234
@@ -228,10 +246,8 @@ func (m *FieldMap) sortedTags() []Tag {
228246
229247func (m FieldMap ) write (buffer * bytes.Buffer ) {
230248 for _ , tag := range m .sortedTags () {
231- if fields , ok := m .tagLookup [tag ]; ok {
232- for _ , tv := range fields .tvs {
233- buffer .Write (tv .bytes )
234- }
249+ if f , ok := m .tagLookup [tag ]; ok {
250+ f .write (buffer )
235251 }
236252 }
237253}
0 commit comments