Skip to content

Commit 024b28d

Browse files
author
Chris Busbey
committed
swizzles value and pointer references to tag values, field map methods
1 parent 3e6c2a2 commit 024b28d

File tree

8 files changed

+79
-75
lines changed

8 files changed

+79
-75
lines changed

cmd/generate-fix/internal/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ quickfix.GroupTemplate{
123123
{{ if .IsGroup }}
124124
//{{ .Name }} is a repeating group element, Tag {{ .Tag }}
125125
type {{ .Name }} struct {
126-
quickfix.Group
126+
*quickfix.Group
127127
}
128128
129129
{{ template "setters" .}}

field.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ type Field interface {
3333
//FieldGroupWriter is an interface for writing a FieldGroup
3434
type FieldGroupWriter interface {
3535
Tag() Tag
36-
Write() TagValues
36+
Write() []TagValue
3737
}
3838

3939
//FieldGroupReader is an interface for reading a FieldGroup
4040
type FieldGroupReader interface {
4141
Tag() Tag
42-
Read(TagValues) (TagValues, error)
42+
Read([]TagValue) ([]TagValue, error)
4343
}
4444

4545
//FieldGroup is the interface implemented by all typed Groups in a Message
4646
type FieldGroup interface {
4747
Tag() Tag
48-
Write() TagValues
49-
Read(TagValues) (TagValues, error)
48+
Write() []TagValue
49+
Read([]TagValue) ([]TagValue, error)
5050
}

field_map.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ import (
66
"time"
77
)
88

9+
//tagValues stores a slice of TagValues
10+
type tagValues struct {
11+
tvs []TagValue
12+
}
13+
914
//FieldMap is a collection of fix fields that make up a fix message.
1015
type FieldMap struct {
11-
tagLookup map[Tag]TagValues
16+
tagLookup map[Tag]*tagValues
1217
tagOrder
1318
}
1419

@@ -32,7 +37,7 @@ func (m *FieldMap) init() {
3237
}
3338

3439
func (m *FieldMap) initWithOrdering(ordering tagOrder) {
35-
m.tagLookup = make(map[Tag]TagValues)
40+
m.tagLookup = make(map[Tag]*tagValues)
3641
m.tagOrder = ordering
3742
}
3843

@@ -64,7 +69,7 @@ func (m FieldMap) GetField(tag Tag, parser FieldValueReader) MessageRejectError
6469
return ConditionallyRequiredFieldMissing(tag)
6570
}
6671

67-
if err := parser.Read(tagValues[0].value); err != nil {
72+
if err := parser.Read(tagValues.tvs[0].value); err != nil {
6873
return IncorrectDataFormatForValue(tag)
6974
}
7075

@@ -78,7 +83,7 @@ func (m FieldMap) GetBytes(tag Tag) ([]byte, MessageRejectError) {
7883
return nil, ConditionallyRequiredFieldMissing(tag)
7984
}
8085

81-
return tagValues[0].value, nil
86+
return tagValues.tvs[0].value, nil
8287
}
8388

8489
//GetBool is a GetField wrapper for bool fields
@@ -136,7 +141,7 @@ func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
136141
return ConditionallyRequiredFieldMissing(parser.Tag())
137142
}
138143

139-
if _, err := parser.Read(tagValues); err != nil {
144+
if _, err := parser.Read(tagValues.tvs); err != nil {
140145
if msgRejErr, ok := err.(MessageRejectError); ok {
141146
return msgRejErr
142147
}
@@ -147,25 +152,26 @@ func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
147152
}
148153

149154
//SetField sets the field with Tag tag
150-
func (m FieldMap) SetField(tag Tag, field FieldValueWriter) FieldMap {
151-
tValues := make(TagValues, 1)
152-
tValues[0].init(tag, field.Write())
155+
func (m *FieldMap) SetField(tag Tag, field FieldValueWriter) *FieldMap {
156+
tValues := new(tagValues)
157+
tValues.tvs = make([]TagValue, 1)
158+
tValues.tvs[0].init(tag, field.Write())
153159
m.tagLookup[tag] = tValues
154160
return m
155161
}
156162

157163
//SetBool is a SetField wrapper for bool fields
158-
func (m FieldMap) SetBool(tag Tag, value bool) FieldMap {
164+
func (m *FieldMap) SetBool(tag Tag, value bool) *FieldMap {
159165
return m.SetField(tag, FIXBoolean(value))
160166
}
161167

162168
//SetInt is a SetField wrapper for int fields
163-
func (m FieldMap) SetInt(tag Tag, value int) FieldMap {
169+
func (m *FieldMap) SetInt(tag Tag, value int) *FieldMap {
164170
return m.SetField(tag, FIXInt(value))
165171
}
166172

167173
//SetString is a SetField wrapper for string fields
168-
func (m FieldMap) SetString(tag Tag, value string) FieldMap {
174+
func (m *FieldMap) SetString(tag Tag, value string) *FieldMap {
169175
return m.SetField(tag, FIXString(value))
170176
}
171177

@@ -177,16 +183,17 @@ func (m *FieldMap) Clear() {
177183
}
178184

179185
//Set is a setter for fields
180-
func (m FieldMap) Set(field FieldWriter) FieldMap {
181-
tValues := make(TagValues, 1)
182-
tValues[0].init(field.Tag(), field.Write())
186+
func (m *FieldMap) Set(field FieldWriter) *FieldMap {
187+
tValues := new(tagValues)
188+
tValues.tvs = make([]TagValue, 1)
189+
tValues.tvs[0].init(field.Tag(), field.Write())
183190
m.tagLookup[field.Tag()] = tValues
184191
return m
185192
}
186193

187194
//SetGroup is a setter specific to group fields
188-
func (m FieldMap) SetGroup(field FieldGroupWriter) FieldMap {
189-
m.tagLookup[field.Tag()] = field.Write()
195+
func (m *FieldMap) SetGroup(field FieldGroupWriter) *FieldMap {
196+
m.tagLookup[field.Tag()] = &tagValues{field.Write()}
190197
return m
191198
}
192199

@@ -205,7 +212,7 @@ func (m FieldMap) write(buffer *bytes.Buffer) {
205212

206213
for _, tag := range tags {
207214
if fields, ok := m.tagLookup[tag]; ok {
208-
for _, tv := range fields {
215+
for _, tv := range fields.tvs {
209216
buffer.Write(tv.bytes)
210217
}
211218
}
@@ -215,7 +222,7 @@ func (m FieldMap) write(buffer *bytes.Buffer) {
215222
func (m FieldMap) total() int {
216223
total := 0
217224
for _, fields := range m.tagLookup {
218-
for _, tv := range fields {
225+
for _, tv := range fields.tvs {
219226
switch tv.tag {
220227
case tagCheckSum: //tag does not contribute to total
221228
default:
@@ -230,7 +237,7 @@ func (m FieldMap) total() int {
230237
func (m FieldMap) length() int {
231238
length := 0
232239
for _, fields := range m.tagLookup {
233-
for _, tv := range fields {
240+
for _, tv := range fields.tvs {
234241
switch tv.tag {
235242
case tagBeginString, tagBodyLength, tagCheckSum: //tags do not contribute to length
236243
default:

message.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type Message struct {
8888
bodyBytes []byte
8989

9090
//field bytes as they appear in the raw message
91-
fields TagValues
91+
fields []TagValue
9292

9393
//flag is true if this message should not be returned to pool after use
9494
keepMessage bool
@@ -132,7 +132,7 @@ func ParseMessage(msg *Message, rawMessage *bytes.Buffer) (err error) {
132132
}
133133

134134
if cap(msg.fields) < fieldCount {
135-
msg.fields = make(TagValues, fieldCount)
135+
msg.fields = make([]TagValue, fieldCount)
136136
} else {
137137
msg.fields = msg.fields[0:fieldCount]
138138
}
@@ -144,23 +144,23 @@ func ParseMessage(msg *Message, rawMessage *bytes.Buffer) (err error) {
144144
return
145145
}
146146

147-
msg.Header.tagLookup[msg.fields[fieldIndex].tag] = msg.fields[fieldIndex : fieldIndex+1]
147+
msg.Header.tagLookup[msg.fields[fieldIndex].tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
148148
fieldIndex++
149149

150150
parsedFieldBytes := &msg.fields[fieldIndex]
151151
if rawBytes, err = extractSpecificField(parsedFieldBytes, tagBodyLength, rawBytes); err != nil {
152152
return
153153
}
154154

155-
msg.Header.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
155+
msg.Header.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
156156
fieldIndex++
157157

158158
parsedFieldBytes = &msg.fields[fieldIndex]
159159
if rawBytes, err = extractSpecificField(parsedFieldBytes, tagMsgType, rawBytes); err != nil {
160160
return
161161
}
162162

163-
msg.Header.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
163+
msg.Header.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
164164
fieldIndex++
165165

166166
trailerBytes := []byte{}
@@ -174,13 +174,13 @@ func ParseMessage(msg *Message, rawMessage *bytes.Buffer) (err error) {
174174

175175
switch {
176176
case parsedFieldBytes.tag.IsHeader():
177-
msg.Header.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
177+
msg.Header.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
178178
case parsedFieldBytes.tag.IsTrailer():
179-
msg.Trailer.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
179+
msg.Trailer.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
180180
default:
181181
foundBody = true
182182
trailerBytes = rawBytes
183-
msg.Body.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
183+
msg.Body.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
184184
}
185185
if parsedFieldBytes.tag == tagCheckSum {
186186
break

repeating_group.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type GroupItem interface {
1717
//
1818
//The Read function returns the remaining tagValues not processed by the GroupItem. If there was a
1919
//problem reading the field, an error may be returned
20-
Read(TagValues) (TagValues, error)
20+
Read([]TagValue) ([]TagValue, error)
2121

2222
//Clone makes a copy of this GroupItem
2323
Clone() GroupItem
@@ -28,7 +28,7 @@ type protoGroupElement struct {
2828
}
2929

3030
func (t protoGroupElement) Tag() Tag { return t.tag }
31-
func (t protoGroupElement) Read(tv TagValues) (TagValues, error) {
31+
func (t protoGroupElement) Read(tv []TagValue) ([]TagValue, error) {
3232
if tv[0].tag == t.tag {
3333
return tv[1:], nil
3434
}
@@ -63,7 +63,7 @@ type Group struct{ FieldMap }
6363
type RepeatingGroup struct {
6464
tag Tag
6565
template GroupTemplate
66-
groups []Group
66+
groups []*Group
6767
}
6868

6969
//NewRepeatingGroup returns an initilized RepeatingGroup instance
@@ -93,13 +93,13 @@ func (f RepeatingGroup) Len() int {
9393
}
9494

9595
//Get returns the ith group in this RepeatingGroup
96-
func (f RepeatingGroup) Get(i int) Group {
96+
func (f RepeatingGroup) Get(i int) *Group {
9797
return f.groups[i]
9898
}
9999

100100
//Add appends a new group to the RepeatingGroup and returns the new Group
101-
func (f *RepeatingGroup) Add() Group {
102-
var g Group
101+
func (f *RepeatingGroup) Add() *Group {
102+
g := new(Group)
103103
g.initWithOrdering(f.groupTagOrder())
104104

105105
f.groups = append(f.groups, g)
@@ -108,16 +108,16 @@ func (f *RepeatingGroup) Add() Group {
108108

109109
//Write returns tagValues for all Items in the repeating group ordered by
110110
//Group sequence and Group template order
111-
func (f RepeatingGroup) Write() TagValues {
112-
tvs := make(TagValues, 1, 1)
111+
func (f RepeatingGroup) Write() []TagValue {
112+
tvs := make([]TagValue, 1, 1)
113113
tvs[0].init(f.tag, []byte(strconv.Itoa(len(f.groups))))
114114

115115
for _, group := range f.groups {
116116
tags := group.sortedTags()
117117

118118
for _, tag := range tags {
119119
if fields, ok := group.tagLookup[tag]; ok {
120-
tvs = append(tvs, fields...)
120+
tvs = append(tvs, fields.tvs...)
121121
}
122122
}
123123
}
@@ -167,7 +167,7 @@ func (f RepeatingGroup) isDelimiter(t Tag) bool {
167167
return t == f.delimiter()
168168
}
169169

170-
func (f *RepeatingGroup) Read(tv TagValues) (TagValues, error) {
170+
func (f *RepeatingGroup) Read(tv []TagValue) ([]TagValue, error) {
171171
expectedGroupSize, err := atoi(tv[0].value)
172172
if err != nil {
173173
return tv, err
@@ -179,7 +179,7 @@ func (f *RepeatingGroup) Read(tv TagValues) (TagValues, error) {
179179

180180
tv = tv[1:cap(tv)]
181181
tagOrdering := f.groupTagOrder()
182-
var group Group
182+
group := new(Group)
183183
group.initWithOrdering(tagOrdering)
184184
for len(tv) > 0 {
185185
field, ok := f.findItemInGroupTemplate(tv[0].tag)
@@ -193,13 +193,13 @@ func (f *RepeatingGroup) Read(tv TagValues) (TagValues, error) {
193193
}
194194

195195
if f.isDelimiter(field.Tag()) {
196-
group = Group{}
196+
group = new(Group)
197197
group.initWithOrdering(tagOrdering)
198198

199199
f.groups = append(f.groups, group)
200200
}
201201

202-
group.tagLookup[tvRange[0].tag] = tvRange
202+
group.tagLookup[tvRange[0].tag] = &tagValues{tvRange}
203203
}
204204

205205
if len(f.groups) != expectedGroupSize {

0 commit comments

Comments
 (0)