Skip to content

Commit

Permalink
swizzles value and pointer references to tag values, field map methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Busbey committed Oct 10, 2016
1 parent 3e6c2a2 commit 024b28d
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 75 deletions.
2 changes: 1 addition & 1 deletion cmd/generate-fix/internal/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ quickfix.GroupTemplate{
{{ if .IsGroup }}
//{{ .Name }} is a repeating group element, Tag {{ .Tag }}
type {{ .Name }} struct {
quickfix.Group
*quickfix.Group
}
{{ template "setters" .}}
Expand Down
8 changes: 4 additions & 4 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type Field interface {
//FieldGroupWriter is an interface for writing a FieldGroup
type FieldGroupWriter interface {
Tag() Tag
Write() TagValues
Write() []TagValue
}

//FieldGroupReader is an interface for reading a FieldGroup
type FieldGroupReader interface {
Tag() Tag
Read(TagValues) (TagValues, error)
Read([]TagValue) ([]TagValue, error)
}

//FieldGroup is the interface implemented by all typed Groups in a Message
type FieldGroup interface {
Tag() Tag
Write() TagValues
Read(TagValues) (TagValues, error)
Write() []TagValue
Read([]TagValue) ([]TagValue, error)
}
45 changes: 26 additions & 19 deletions field_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import (
"time"
)

//tagValues stores a slice of TagValues
type tagValues struct {
tvs []TagValue
}

//FieldMap is a collection of fix fields that make up a fix message.
type FieldMap struct {
tagLookup map[Tag]TagValues
tagLookup map[Tag]*tagValues
tagOrder
}

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

func (m *FieldMap) initWithOrdering(ordering tagOrder) {
m.tagLookup = make(map[Tag]TagValues)
m.tagLookup = make(map[Tag]*tagValues)
m.tagOrder = ordering
}

Expand Down Expand Up @@ -64,7 +69,7 @@ func (m FieldMap) GetField(tag Tag, parser FieldValueReader) MessageRejectError
return ConditionallyRequiredFieldMissing(tag)
}

if err := parser.Read(tagValues[0].value); err != nil {
if err := parser.Read(tagValues.tvs[0].value); err != nil {
return IncorrectDataFormatForValue(tag)
}

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

return tagValues[0].value, nil
return tagValues.tvs[0].value, nil
}

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

if _, err := parser.Read(tagValues); err != nil {
if _, err := parser.Read(tagValues.tvs); err != nil {
if msgRejErr, ok := err.(MessageRejectError); ok {
return msgRejErr
}
Expand All @@ -147,25 +152,26 @@ func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
}

//SetField sets the field with Tag tag
func (m FieldMap) SetField(tag Tag, field FieldValueWriter) FieldMap {
tValues := make(TagValues, 1)
tValues[0].init(tag, field.Write())
func (m *FieldMap) SetField(tag Tag, field FieldValueWriter) *FieldMap {
tValues := new(tagValues)
tValues.tvs = make([]TagValue, 1)
tValues.tvs[0].init(tag, field.Write())
m.tagLookup[tag] = tValues
return m
}

//SetBool is a SetField wrapper for bool fields
func (m FieldMap) SetBool(tag Tag, value bool) FieldMap {
func (m *FieldMap) SetBool(tag Tag, value bool) *FieldMap {
return m.SetField(tag, FIXBoolean(value))
}

//SetInt is a SetField wrapper for int fields
func (m FieldMap) SetInt(tag Tag, value int) FieldMap {
func (m *FieldMap) SetInt(tag Tag, value int) *FieldMap {
return m.SetField(tag, FIXInt(value))
}

//SetString is a SetField wrapper for string fields
func (m FieldMap) SetString(tag Tag, value string) FieldMap {
func (m *FieldMap) SetString(tag Tag, value string) *FieldMap {
return m.SetField(tag, FIXString(value))
}

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

//Set is a setter for fields
func (m FieldMap) Set(field FieldWriter) FieldMap {
tValues := make(TagValues, 1)
tValues[0].init(field.Tag(), field.Write())
func (m *FieldMap) Set(field FieldWriter) *FieldMap {
tValues := new(tagValues)
tValues.tvs = make([]TagValue, 1)
tValues.tvs[0].init(field.Tag(), field.Write())
m.tagLookup[field.Tag()] = tValues
return m
}

//SetGroup is a setter specific to group fields
func (m FieldMap) SetGroup(field FieldGroupWriter) FieldMap {
m.tagLookup[field.Tag()] = field.Write()
func (m *FieldMap) SetGroup(field FieldGroupWriter) *FieldMap {
m.tagLookup[field.Tag()] = &tagValues{field.Write()}
return m
}

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

for _, tag := range tags {
if fields, ok := m.tagLookup[tag]; ok {
for _, tv := range fields {
for _, tv := range fields.tvs {
buffer.Write(tv.bytes)
}
}
Expand All @@ -215,7 +222,7 @@ func (m FieldMap) write(buffer *bytes.Buffer) {
func (m FieldMap) total() int {
total := 0
for _, fields := range m.tagLookup {
for _, tv := range fields {
for _, tv := range fields.tvs {
switch tv.tag {
case tagCheckSum: //tag does not contribute to total
default:
Expand All @@ -230,7 +237,7 @@ func (m FieldMap) total() int {
func (m FieldMap) length() int {
length := 0
for _, fields := range m.tagLookup {
for _, tv := range fields {
for _, tv := range fields.tvs {
switch tv.tag {
case tagBeginString, tagBodyLength, tagCheckSum: //tags do not contribute to length
default:
Expand Down
16 changes: 8 additions & 8 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Message struct {
bodyBytes []byte

//field bytes as they appear in the raw message
fields TagValues
fields []TagValue

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

if cap(msg.fields) < fieldCount {
msg.fields = make(TagValues, fieldCount)
msg.fields = make([]TagValue, fieldCount)
} else {
msg.fields = msg.fields[0:fieldCount]
}
Expand All @@ -144,23 +144,23 @@ func ParseMessage(msg *Message, rawMessage *bytes.Buffer) (err error) {
return
}

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

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

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

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

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

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

switch {
case parsedFieldBytes.tag.IsHeader():
msg.Header.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
msg.Header.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
case parsedFieldBytes.tag.IsTrailer():
msg.Trailer.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
msg.Trailer.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
default:
foundBody = true
trailerBytes = rawBytes
msg.Body.tagLookup[parsedFieldBytes.tag] = msg.fields[fieldIndex : fieldIndex+1]
msg.Body.tagLookup[parsedFieldBytes.tag] = &tagValues{msg.fields[fieldIndex : fieldIndex+1]}
}
if parsedFieldBytes.tag == tagCheckSum {
break
Expand Down
26 changes: 13 additions & 13 deletions repeating_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GroupItem interface {
//
//The Read function returns the remaining tagValues not processed by the GroupItem. If there was a
//problem reading the field, an error may be returned
Read(TagValues) (TagValues, error)
Read([]TagValue) ([]TagValue, error)

//Clone makes a copy of this GroupItem
Clone() GroupItem
Expand All @@ -28,7 +28,7 @@ type protoGroupElement struct {
}

func (t protoGroupElement) Tag() Tag { return t.tag }
func (t protoGroupElement) Read(tv TagValues) (TagValues, error) {
func (t protoGroupElement) Read(tv []TagValue) ([]TagValue, error) {
if tv[0].tag == t.tag {
return tv[1:], nil
}
Expand Down Expand Up @@ -63,7 +63,7 @@ type Group struct{ FieldMap }
type RepeatingGroup struct {
tag Tag
template GroupTemplate
groups []Group
groups []*Group
}

//NewRepeatingGroup returns an initilized RepeatingGroup instance
Expand Down Expand Up @@ -93,13 +93,13 @@ func (f RepeatingGroup) Len() int {
}

//Get returns the ith group in this RepeatingGroup
func (f RepeatingGroup) Get(i int) Group {
func (f RepeatingGroup) Get(i int) *Group {
return f.groups[i]
}

//Add appends a new group to the RepeatingGroup and returns the new Group
func (f *RepeatingGroup) Add() Group {
var g Group
func (f *RepeatingGroup) Add() *Group {
g := new(Group)
g.initWithOrdering(f.groupTagOrder())

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

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

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

for _, tag := range tags {
if fields, ok := group.tagLookup[tag]; ok {
tvs = append(tvs, fields...)
tvs = append(tvs, fields.tvs...)
}
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (f RepeatingGroup) isDelimiter(t Tag) bool {
return t == f.delimiter()
}

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

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

if f.isDelimiter(field.Tag()) {
group = Group{}
group = new(Group)
group.initWithOrdering(tagOrdering)

f.groups = append(f.groups, group)
}

group.tagLookup[tvRange[0].tag] = tvRange
group.tagLookup[tvRange[0].tag] = &tagValues{tvRange}
}

if len(f.groups) != expectedGroupSize {
Expand Down
Loading

0 comments on commit 024b28d

Please sign in to comment.