From f10118a5be2e74b2e92005654944abd04950f74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20DEROIDE?= <34481271+aureliar8@users.noreply.github.com> Date: Tue, 30 Jan 2024 11:31:20 +0100 Subject: [PATCH 1/3] Call Reset before UnmarshallVT to keep the default grpc semantic --- codec/grpc/grpc_codec.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/codec/grpc/grpc_codec.go b/codec/grpc/grpc_codec.go index 9b72fcd9..c87574bb 100644 --- a/codec/grpc/grpc_codec.go +++ b/codec/grpc/grpc_codec.go @@ -1,6 +1,10 @@ package grpc -import "fmt" +import ( + "fmt" + + "github.com/golang/protobuf/proto" //nolint +) // Name is the name registered for the proto compressor. const Name = "proto" @@ -25,6 +29,11 @@ func (Codec) Unmarshal(data []byte, v interface{}) error { if !ok { return fmt.Errorf("failed to unmarshal, message is %T (missing vtprotobuf helpers)", v) } + vv, ok := v.(proto.Message) + if !ok { + return fmt.Errorf("failed to unmarshal, message is %T (can't reset)", vv) + } + vv.Reset() return vt.UnmarshalVT(data) } From a5301e8b66a77fa6477f3b15418a4c5ee7edb310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20DEROIDE?= Date: Fri, 2 Feb 2024 10:01:42 +0100 Subject: [PATCH 2/3] Apply go mod tidy --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 91c37af2..717d89e4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/planetscale/vtprotobuf go 1.20 require ( + github.com/golang/protobuf v1.5.3 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.58.2 google.golang.org/protobuf v1.31.0 @@ -10,7 +11,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.14.0 // indirect golang.org/x/sys v0.11.0 // indirect From db5b5b4f2ce9dbcf29c436e081de2d3feebf3e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20DEROIDE?= Date: Sat, 18 May 2024 11:36:08 +0200 Subject: [PATCH 3/3] Don't depend on github.com/golang/protobuf/proto --- codec/grpc/grpc_codec.go | 11 +++++++---- go.mod | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/codec/grpc/grpc_codec.go b/codec/grpc/grpc_codec.go index c87574bb..3e7951c3 100644 --- a/codec/grpc/grpc_codec.go +++ b/codec/grpc/grpc_codec.go @@ -2,8 +2,6 @@ package grpc import ( "fmt" - - "github.com/golang/protobuf/proto" //nolint ) // Name is the name registered for the proto compressor. @@ -16,6 +14,10 @@ type vtprotoMessage interface { UnmarshalVT([]byte) error } +type reseter interface{ + Reset() +} + func (Codec) Marshal(v interface{}) ([]byte, error) { vt, ok := v.(vtprotoMessage) if !ok { @@ -29,9 +31,10 @@ func (Codec) Unmarshal(data []byte, v interface{}) error { if !ok { return fmt.Errorf("failed to unmarshal, message is %T (missing vtprotobuf helpers)", v) } - vv, ok := v.(proto.Message) + //All types that implement github.com/golang/protobuf/proto.Message have a Reset method + vv, ok := v.(reseter) if !ok { - return fmt.Errorf("failed to unmarshal, message is %T (can't reset)", vv) + return fmt.Errorf("failed to unmarshal: can't reset. Message type %T don't implement Reset()", vv) } vv.Reset() return vt.UnmarshalVT(data) diff --git a/go.mod b/go.mod index 717d89e4..91c37af2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/planetscale/vtprotobuf go 1.20 require ( - github.com/golang/protobuf v1.5.3 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.58.2 google.golang.org/protobuf v1.31.0 @@ -11,6 +10,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.14.0 // indirect golang.org/x/sys v0.11.0 // indirect