diff --git a/conformance/internal/conformance/oneof_test.go b/conformance/internal/conformance/oneof_test.go index af30965..048d9a7 100644 --- a/conformance/internal/conformance/oneof_test.go +++ b/conformance/internal/conformance/oneof_test.go @@ -1,16 +1,25 @@ package conformance import ( - "testing" - + "github.com/planetscale/vtprotobuf/types/known/structpb" "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" + upstreamstructpb "google.golang.org/protobuf/types/known/structpb" + "testing" ) -func TestEmptyOneoff(t *testing.T) { +func TestEmptyOneof(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) + t.Run("all proto", func(t *testing.T) { + msg := &TestAllTypesProto3{OneofField: &TestAllTypesProto3_OneofNestedMessage{}} + upstream, _ := proto.Marshal(msg) + vt, _ := msg.MarshalVTStrict() + require.Equal(t, upstream, vt) + }) + t.Run("structpb", func(t *testing.T) { + msg := &structpb.Value{Kind: &upstreamstructpb.Value_StructValue{}} + upstream, _ := proto.Marshal((*upstreamstructpb.Value)(msg)) + vt, _ := msg.MarshalVTStrict() + require.Equal(t, upstream, vt) + }) } diff --git a/features/size/size.go b/features/size/size.go index 54d856a..23560a2 100644 --- a/features/size/size.go +++ b/features/size/size.go @@ -267,9 +267,9 @@ func (p *size) field(oneof bool, field *protogen.Field, sizeName string) { } // 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 + // Size is always keysize + 1 so just hardcode that here if oneof && field.Desc.Kind() == protoreflect.MessageKind && !field.Desc.IsMap() && !field.Desc.IsList() { - p.P("} else { n += 3 }") + p.P("} else { n += ", strconv.Itoa(key + 1), " }") } else if repeated || nullable { p.P(`}`) } diff --git a/testproto/pool/pool_with_oneof_vtproto.pb.go b/testproto/pool/pool_with_oneof_vtproto.pb.go index d781b2a..1c75bbb 100644 --- a/testproto/pool/pool_with_oneof_vtproto.pb.go +++ b/testproto/pool/pool_with_oneof_vtproto.pb.go @@ -1139,7 +1139,7 @@ func (m *OneofTest_Test1_) SizeVT() (n int) { l = m.Test1.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1153,7 +1153,7 @@ func (m *OneofTest_Test2_) SizeVT() (n int) { l = m.Test2.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1167,7 +1167,7 @@ func (m *OneofTest_Test3_) SizeVT() (n int) { l = m.Test3.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } diff --git a/testproto/unsafe/unsafe_vtproto.pb.go b/testproto/unsafe/unsafe_vtproto.pb.go index 835f096..dd00973 100644 --- a/testproto/unsafe/unsafe_vtproto.pb.go +++ b/testproto/unsafe/unsafe_vtproto.pb.go @@ -1572,7 +1572,7 @@ func (m *UnsafeTest_Sub1_) SizeVT() (n int) { l = m.Sub1.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1586,7 +1586,7 @@ func (m *UnsafeTest_Sub2_) SizeVT() (n int) { l = m.Sub2.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1600,7 +1600,7 @@ func (m *UnsafeTest_Sub3_) SizeVT() (n int) { l = m.Sub3.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1614,7 +1614,7 @@ func (m *UnsafeTest_Sub4_) SizeVT() (n int) { l = m.Sub4.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1628,7 +1628,7 @@ func (m *UnsafeTest_Sub5_) SizeVT() (n int) { l = m.Sub5.SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } diff --git a/types/known/structpb/struct_vtproto.pb.go b/types/known/structpb/struct_vtproto.pb.go index be8b40e..0699c09 100644 --- a/types/known/structpb/struct_vtproto.pb.go +++ b/types/known/structpb/struct_vtproto.pb.go @@ -1003,7 +1003,7 @@ func (m *Value_StructValue) SizeVT() (n int) { l = (*Struct)(m.StructValue).SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n } @@ -1017,7 +1017,7 @@ func (m *Value_ListValue) SizeVT() (n int) { l = (*ListValue)(m.ListValue).SizeVT() n += 1 + l + protohelpers.SizeOfVarint(uint64(l)) } else { - n += 3 + n += 2 } return n }