From c2b50ee081682eca4b995d2fb79e642019f78aea Mon Sep 17 00:00:00 2001 From: Arvind Bright Date: Mon, 5 Feb 2024 14:59:52 -0800 Subject: [PATCH] deps: fix backwards compatibility with encoding (#6965) --- encoding/proto/proto.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/encoding/proto/proto.go b/encoding/proto/proto.go index f34f6e65f0e3..6fff15a715e8 100644 --- a/encoding/proto/proto.go +++ b/encoding/proto/proto.go @@ -25,6 +25,7 @@ import ( "google.golang.org/grpc/encoding" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/protoadapt" ) // Name is the name registered for the proto compressor. @@ -38,21 +39,34 @@ func init() { type codec struct{} func (codec) Marshal(v any) ([]byte, error) { - vv, ok := v.(proto.Message) - if !ok { + vv := messageV2Of(v) + if vv == nil { return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v) } + return proto.Marshal(vv) } func (codec) Unmarshal(data []byte, v any) error { - vv, ok := v.(proto.Message) - if !ok { - return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v) + vv := messageV2Of(v) + if vv == nil { + return fmt.Errorf("failed to marshal, message is %T, want proto.Message", v) } + return proto.Unmarshal(data, vv) } +func messageV2Of(v any) proto.Message { + switch v := v.(type) { + case protoadapt.MessageV1: + return protoadapt.MessageV2Of(v) + case protoadapt.MessageV2: + return v + } + + return nil +} + func (codec) Name() string { return Name }