diff --git a/marshal.go b/marshal.go index af30d29cc..0078dbb72 100644 --- a/marshal.go +++ b/marshal.go @@ -3,6 +3,7 @@ package quickfix import ( "reflect" "strconv" + "strings" "time" ) @@ -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++ { diff --git a/marshal_test.go b/marshal_test.go index 21de5a9cc..3d549643c 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -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 { @@ -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) @@ -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 {