@@ -25,6 +25,7 @@ import (
2525
2626 "google.golang.org/grpc/encoding"
2727 "google.golang.org/protobuf/proto"
28+ "google.golang.org/protobuf/protoadapt"
2829)
2930
3031// Name is the name registered for the proto compressor.
@@ -38,21 +39,34 @@ func init() {
3839type codec struct {}
3940
4041func (codec ) Marshal (v any ) ([]byte , error ) {
41- vv , ok := v .(proto. Message )
42- if ! ok {
42+ vv := messageV2Of ( v )
43+ if vv == nil {
4344 return nil , fmt .Errorf ("failed to marshal, message is %T, want proto.Message" , v )
4445 }
46+
4547 return proto .Marshal (vv )
4648}
4749
4850func (codec ) Unmarshal (data []byte , v any ) error {
49- vv , ok := v .(proto. Message )
50- if ! ok {
51- return fmt .Errorf ("failed to unmarshal , message is %T, want proto.Message" , v )
51+ vv := messageV2Of ( v )
52+ if vv == nil {
53+ return fmt .Errorf ("failed to marshal , message is %T, want proto.Message" , v )
5254 }
55+
5356 return proto .Unmarshal (data , vv )
5457}
5558
59+ func messageV2Of (v any ) proto.Message {
60+ switch v := v .(type ) {
61+ case protoadapt.MessageV1 :
62+ return protoadapt .MessageV2Of (v )
63+ case protoadapt.MessageV2 :
64+ return v
65+ }
66+
67+ return nil
68+ }
69+
5670func (codec ) Name () string {
5771 return Name
5872}
0 commit comments