Skip to content

Commit

Permalink
Merge pull request #22 from technicallyty/main
Browse files Browse the repository at this point in the history
fix: unroll Oneofs before other fields
  • Loading branch information
vmg authored Oct 20, 2021
2 parents 27a3a9c + 88007c5 commit 088be8c
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions features/marshal/marshalto.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,22 +587,25 @@ func (p *marshal) message(proto3 bool, message *protogen.Message) {
})

oneofs := make(map[string]struct{})
for i := len(message.Oneofs) - 1; i >= 0; i-- {
field := message.Oneofs[i]
fieldname := field.GoName
if _, ok := oneofs[fieldname]; !ok {
oneofs[fieldname] = struct{}{}
p.P(`if vtmsg, ok := m.`, fieldname, `.(interface{`)
p.P(`MarshalTo([]byte) (int, error)`)
p.P(`Size() int`)
p.P(`}); ok {`)
p.marshalForward("vtmsg", false)
p.P(`}`)
}
}

for i := len(message.Fields) - 1; i >= 0; i-- {
field := message.Fields[i]
oneof := field.Oneof != nil && !field.Oneof.Desc.IsSynthetic()
if !oneof {
p.field(proto3, false, &numGen, field)
} else {
fieldname := field.Oneof.GoName
if _, ok := oneofs[fieldname]; !ok {
oneofs[fieldname] = struct{}{}
p.P(`if vtmsg, ok := m.`, fieldname, `.(interface{`)
p.P(`MarshalToVT([]byte) (int, error)`)
p.P(`SizeVT() int`)
p.P(`}); ok {`)
p.marshalForward("vtmsg", false)
p.P(`}`)
}
}
}
p.P(`return len(dAtA) - i, nil`)
Expand Down

0 comments on commit 088be8c

Please sign in to comment.