Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check oneof on syntetic #111

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (p *pool) message(message *protogen.Message) {
}
p.P(fmt.Sprintf("f%d", len(saved)), ` := m.`, fieldName, `[:0]`)
saved = append(saved, field)
} else if field.Oneof != nil {
} else if field.Oneof != nil && !field.Oneof.Desc.IsSynthetic() {
if p.ShouldPool(field.Message) {
p.P(`if oneof, ok := m.`, field.Oneof.GoName, `.(*`, field.GoIdent, `); ok {`)
p.P(`oneof.`, fieldName, `.ReturnToVTPool()`)
Expand Down
106 changes: 86 additions & 20 deletions testproto/pool/pool.pb.go

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

5 changes: 5 additions & 0 deletions testproto/pool/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ option go_package = "testproto/pool";

import "github.com/planetscale/vtprotobuf/vtproto/ext.proto";

message OptionalMessage{
option (vtproto.mempool) = true;
}

message MemoryPoolExtension {
option (vtproto.mempool) = true;
string foo1 = 1;
uint64 foo2 = 2;
optional OptionalMessage foo3 = 3;
}
23 changes: 23 additions & 0 deletions testproto/pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"google.golang.org/protobuf/proto"
)

func Test_Pool_slice_data_override(t *testing.T) {
Expand Down Expand Up @@ -163,3 +165,24 @@ func Test_Pool_Oneof(t *testing.T) {
require.NoError(t, t8.UnmarshalVT(t4Bytes))
require.Equal(t, &t4, &t8)
}

func Test_Pool_Optional(t *testing.T) {
m := &MemoryPoolExtension{
Foo1: "foo1",
Foo2: 123,
Foo3: &OptionalMessage{},
}

mBytes, err := m.MarshalVT()
require.NoError(t, err)

mUnmarshal := &MemoryPoolExtension{}
err = proto.Unmarshal(mBytes, mUnmarshal)
require.NoError(t, err)

require.True(t, m.EqualVT(mUnmarshal))

m.ReturnToVTPool()
mFromPool := MemoryPoolExtensionFromVTPool()
require.True(t, mFromPool.EqualVT(&MemoryPoolExtension{}))
}
Loading
Loading