Skip to content

Commit

Permalink
fix: oneofs generate first
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Oct 12, 2021
1 parent 27a3a9c commit 88007c5
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 88007c5

Please sign in to comment.