-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcodec.go
43 lines (35 loc) · 1.03 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package rpc
import (
"github.com/gogo/protobuf/proto"
// Used instead of gogo/protobuf/proto for the fallback case
// to match the behavior of the upstream codec in
// google.golang.org/grpc/encoding/proto that we are
// replacing:
//
// https://github.com/grpc/grpc-go/blob/7b167fd6eca1ab8f05ec14085d63197cacd41438/encoding/proto/proto.go
//
gproto "github.com/golang/protobuf/proto"
"google.golang.org/grpc/encoding"
)
const name = "proto"
type codec struct{}
var _ encoding.Codec = codec{}
func (codec) Marshal(v interface{}) ([]byte, error) {
if pm, ok := v.(proto.Marshaler); ok {
return pm.Marshal()
}
return gproto.Marshal(v.(gproto.Message))
}
func (codec) Unmarshal(data []byte, v interface{}) error {
if pm, ok := v.(proto.Unmarshaler); ok {
return pm.Unmarshal(data)
}
return gproto.Unmarshal(data, v.(gproto.Message))
}
func (codec) Name() string {
return name
}