forked from cockroachdb/cockroach
/
codec.go
48 lines (40 loc) · 1.25 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
44
45
46
47
48
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
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
}