Skip to content

Commit 9cfb335

Browse files
committed
Fix size on empty oneOf marshalling
planetscale#130 did not correctly account for the key size impacting the size. This fixes the original PR, and adds another test case to cover the issue
1 parent 0393e58 commit 9cfb335

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package conformance
22

33
import (
4-
"testing"
5-
4+
"github.com/planetscale/vtprotobuf/types/known/structpb"
65
"github.com/stretchr/testify/require"
76
"google.golang.org/protobuf/proto"
7+
upstreamstructpb "google.golang.org/protobuf/types/known/structpb"
8+
"testing"
89
)
910

10-
func TestEmptyOneoff(t *testing.T) {
11+
func TestEmptyOneof(t *testing.T) {
1112
// Regression test for https://github.com/planetscale/vtprotobuf/issues/61
12-
msg := &TestAllTypesProto3{OneofField: &TestAllTypesProto3_OneofNestedMessage{}}
13-
upstream, _ := proto.Marshal(msg)
14-
vt, _ := msg.MarshalVTStrict()
15-
require.Equal(t, upstream, vt)
13+
t.Run("all proto", func(t *testing.T) {
14+
msg := &TestAllTypesProto3{OneofField: &TestAllTypesProto3_OneofNestedMessage{}}
15+
upstream, _ := proto.Marshal(msg)
16+
vt, _ := msg.MarshalVTStrict()
17+
require.Equal(t, upstream, vt)
18+
})
19+
t.Run("structpb", func(t *testing.T) {
20+
msg := &structpb.Value{Kind: &upstreamstructpb.Value_StructValue{}}
21+
upstream, _ := proto.Marshal((*upstreamstructpb.Value)(msg))
22+
vt, _ := msg.MarshalVTStrict()
23+
require.Equal(t, upstream, vt)
24+
})
1625
}

features/size/size.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ func (p *size) field(oneof bool, field *protogen.Field, sizeName string) {
267267
}
268268
// Empty protobufs should emit a message or compatibility with Golang protobuf;
269269
// See https://github.com/planetscale/vtprotobuf/issues/61
270-
// Size is always 3 so just hardcode that here
270+
// Size is always keysize + 1 so just hardcode that here
271271
if oneof && field.Desc.Kind() == protoreflect.MessageKind && !field.Desc.IsMap() && !field.Desc.IsList() {
272-
p.P("} else { n += 3 }")
272+
p.P("} else { n += ", strconv.Itoa(key + 1), " }")
273273
} else if repeated || nullable {
274274
p.P(`}`)
275275
}

testproto/pool/pool_with_oneof_vtproto.pb.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testproto/unsafe/unsafe_vtproto.pb.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/known/structpb/struct_vtproto.pb.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)