Skip to content

Commit

Permalink
deps: fix backwards compatibility with encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindbr8 committed Feb 5, 2024
1 parent 03e76b3 commit 24902d8
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions encoding/proto/proto.go
Expand Up @@ -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.
Expand All @@ -38,18 +39,30 @@ func init() {
type codec struct{}

func (codec) Marshal(v any) ([]byte, error) {
vv, ok := v.(proto.Message)
if !ok {
var vv proto.Message
switch v := v.(type) {
case protoadapt.MessageV1:
vv = protoadapt.MessageV2Of(v)
case protoadapt.MessageV2:
vv = v
default:

Check warning on line 48 in encoding/proto/proto.go

View check run for this annotation

Codecov / codecov/patch

encoding/proto/proto.go#L46-L48

Added lines #L46 - L48 were not covered by tests
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 {
var vv proto.Message
switch v := v.(type) {
case protoadapt.MessageV1:
vv = protoadapt.MessageV2Of(v)
case protoadapt.MessageV2:
vv = v
default:

Check warning on line 62 in encoding/proto/proto.go

View check run for this annotation

Codecov / codecov/patch

encoding/proto/proto.go#L60-L62

Added lines #L60 - L62 were not covered by tests
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
}

return proto.Unmarshal(data, vv)
}

Expand Down

0 comments on commit 24902d8

Please sign in to comment.