Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package quickfix
import (
"reflect"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -64,18 +65,29 @@ func (e encoder) encodeValue(fixTag Tag, v reflect.Value, omitEmpty bool, defaul
panic("repeating group must be a slice of type struct")
}

template := make(GroupTemplate, elem.NumField())
for i := 0; i < elem.NumField(); i++ {
sf := elem.Field(i)
fixTag, err := strconv.Atoi(sf.Tag.Get("fix"))
template := make(GroupTemplate, 0)

if err != nil {
panic(err)
}
var walkFunc func(t reflect.Type)

walkFunc = func(t reflect.Type) {
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
if sf.Anonymous {
walkFunc(sf.Type)
continue
}
afixTag, err := strconv.Atoi(strings.Split(sf.Tag.Get("fix"), ",")[0])

if err != nil {
panic(err)
}

template[i] = GroupElement(Tag(fixTag))
template = append(template, GroupElement(Tag(afixTag)))
}
}

walkFunc(elem)

repeatingGroup := RepeatingGroup{Tag: fixTag, GroupTemplate: template}

for i := 0; i < v.Len(); i++ {
Expand Down
10 changes: 8 additions & 2 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ func TestMarshal_RepeatingGroups(t *testing.T) {
StringField2 *string `fix:"9"`
}

type AnonymousGroup struct {
IntField3 int `fix:"3"`
}

type Group2 struct {
IntField1 int `fix:"1"`
IntField2 int `fix:"2"`
IntField2 int `fix:"2, omitempty"`
AnonymousGroup
}

type Message struct {
Expand All @@ -195,7 +200,7 @@ func TestMarshal_RepeatingGroups(t *testing.T) {
m := Message{
GroupField1: []Group1{{StringField1: "hello", StringField2: &s}, {StringField1: "goodbye"}, {StringField1: "OHHAI", StringField2: &s}},
StringField: "world",
GroupField2: []Group2{{IntField1: 1, IntField2: 42}},
GroupField2: []Group2{{IntField1: 1, IntField2: 42, AnonymousGroup: AnonymousGroup{IntField3: 44}}},
}
fixMsg := quickfix.Marshal(m)

Expand All @@ -207,6 +212,7 @@ func TestMarshal_RepeatingGroups(t *testing.T) {
group2Template := quickfix.GroupTemplate{
quickfix.GroupElement(quickfix.Tag(1)),
quickfix.GroupElement(quickfix.Tag(2)),
quickfix.GroupElement(quickfix.Tag(3)),
}

var tests = []struct {
Expand Down