Skip to content

Commit f4505ff

Browse files
committed
Merge pull request #86 from babaev/marshal-fix
Fix for #85
2 parents e88e06e + 142b3fd commit f4505ff

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

marshal.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package quickfix
33
import (
44
"reflect"
55
"strconv"
6+
"strings"
67
"time"
78
)
89

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

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

72-
if err != nil {
73-
panic(err)
74-
}
70+
var walkFunc func(t reflect.Type)
71+
72+
walkFunc = func(t reflect.Type) {
73+
for i := 0; i < t.NumField(); i++ {
74+
sf := t.Field(i)
75+
if sf.Anonymous {
76+
walkFunc(sf.Type)
77+
continue
78+
}
79+
afixTag, err := strconv.Atoi(strings.Split(sf.Tag.Get("fix"), ",")[0])
80+
81+
if err != nil {
82+
panic(err)
83+
}
7584

76-
template[i] = GroupElement(Tag(fixTag))
85+
template = append(template, GroupElement(Tag(afixTag)))
86+
}
7787
}
7888

89+
walkFunc(elem)
90+
7991
repeatingGroup := RepeatingGroup{Tag: fixTag, GroupTemplate: template}
8092

8193
for i := 0; i < v.Len(); i++ {

marshal_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,14 @@ func TestMarshal_RepeatingGroups(t *testing.T) {
179179
StringField2 *string `fix:"9"`
180180
}
181181

182+
type AnonymousGroup struct {
183+
IntField3 int `fix:"3"`
184+
}
185+
182186
type Group2 struct {
183187
IntField1 int `fix:"1"`
184-
IntField2 int `fix:"2"`
188+
IntField2 int `fix:"2, omitempty"`
189+
AnonymousGroup
185190
}
186191

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

@@ -207,6 +212,7 @@ func TestMarshal_RepeatingGroups(t *testing.T) {
207212
group2Template := quickfix.GroupTemplate{
208213
quickfix.GroupElement(quickfix.Tag(1)),
209214
quickfix.GroupElement(quickfix.Tag(2)),
215+
quickfix.GroupElement(quickfix.Tag(3)),
210216
}
211217

212218
var tests = []struct {

0 commit comments

Comments
 (0)