Skip to content

Commit

Permalink
Merge pull request #130 from howardjohn/oneof/fix
Browse files Browse the repository at this point in the history
Fix marshalling of empty oneOf messages
  • Loading branch information
vmg authored Mar 8, 2024
2 parents ec98e72 + d579c53 commit 54814e4
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 9 deletions.
16 changes: 16 additions & 0 deletions conformance/internal/conformance/oneof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package conformance

import (
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestEmptyOneoff(t *testing.T) {
// Regression test for https://github.com/planetscale/vtprotobuf/issues/61
msg := &TestAllTypesProto3{OneofField: &TestAllTypesProto3_OneofNestedMessage{}}
upstream, _ := proto.Marshal(msg)
vt, _ := msg.MarshalVTStrict()
require.Equal(t, upstream, vt)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions features/marshal/marshalto.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/planetscale/vtprotobuf/generator"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -520,7 +519,14 @@ func (p *marshal) field(oneof bool, numGen *counter, field *protogen.Field) {
default:
panic("not implemented")
}
if repeated || nullable {
// Empty protobufs should emit a message or compatibility with Golang protobuf;
// See https://github.com/planetscale/vtprotobuf/issues/61
if oneof && field.Desc.Kind() == protoreflect.MessageKind && !field.Desc.IsMap() && !field.Desc.IsList() {
p.P("} else {")
p.P("i = protohelpers.EncodeVarint(dAtA, i, 0)")
p.encodeKey(fieldNumber, wireType)
p.P("}")
} else if repeated || nullable {
p.P(`}`)
}
}
Expand Down Expand Up @@ -676,7 +682,7 @@ func (p *marshal) message(message *protogen.Message) {
p.P(`}`)
p.P()

//Generate MarshalToVT methods for oneof fields
// Generate MarshalToVT methods for oneof fields
for _, field := range message.Fields {
if field.Oneof == nil || field.Oneof.Desc.IsSynthetic() {
continue
Expand Down Expand Up @@ -709,7 +715,6 @@ func (p *marshal) marshalBackwardSize(varInt bool) {
if varInt {
p.encodeVarint(`size`)
}

}

func (p *marshal) marshalBackward(varName string, varInt bool, message *protogen.Message) {
Expand Down
12 changes: 7 additions & 5 deletions features/size/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ package size
import (
"strconv"

"github.com/planetscale/vtprotobuf/generator"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/planetscale/vtprotobuf/generator"
)

func init() {
Expand Down Expand Up @@ -266,7 +265,12 @@ func (p *size) field(oneof bool, field *protogen.Field, sizeName string) {
default:
panic("not implemented")
}
if repeated || nullable {
// Empty protobufs should emit a message or compatibility with Golang protobuf;
// See https://github.com/planetscale/vtprotobuf/issues/61
// Size is always 3 so just hardcode that here
if oneof && field.Desc.Kind() == protoreflect.MessageKind && !field.Desc.IsMap() && !field.Desc.IsList() {
p.P("} else { n += 3 }")
} else if repeated || nullable {
p.P(`}`)
}
}
Expand Down Expand Up @@ -310,8 +314,6 @@ func (p *size) message(message *protogen.Message) {
}
p.P(`}`)
} else {
//if _, ok := oneofs[fieldname]; !ok {
//oneofs[fieldname] = struct{}{}
p.P(`if vtmsg, ok := m.`, fieldname, `.(interface{ SizeVT() int }); ok {`)
p.P(`n+=vtmsg.`, sizeName, `()`)
p.P(`}`)
Expand Down
30 changes: 30 additions & 0 deletions testproto/pool/pool_with_oneof_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 54814e4

Please sign in to comment.