/
protobuf_codec.go
60 lines (49 loc) · 1.41 KB
/
protobuf_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
49
50
51
52
53
54
55
56
57
58
59
60
package codec
import (
"fmt"
"google.golang.org/protobuf/proto"
)
const (
ProtobufName = "protobuf"
ProtobufId = 'p'
)
func init() {
Reg(new(ProtoCodec))
}
type ProtoCodec struct{}
func (ProtoCodec) Name() string {
return ProtobufName
}
func (ProtoCodec) ID() byte {
return ProtobufId
}
// Marshal 编码
func (ProtoCodec) Marshal(v interface{}) ([]byte, error) {
return ProtoMarshal(v)
}
// Unmarshal 解码
func (ProtoCodec) Unmarshal(data []byte, v interface{}) error {
return ProtoUnmarshal(data, v)
}
var (
// PbEmptyStruct 如果需要编码nil 或者struct,则不能编码,使用空的
PbEmptyStruct = new(PbEmpty)
)
func ProtoMarshal(v interface{}) ([]byte, error) {
switch p := v.(type) {
case proto.Message:
return proto.Marshal(p)
case nil, *struct{}, struct{}:
return proto.Marshal(PbEmptyStruct)
}
return nil, fmt.Errorf("protobuf 编码失败: %T 传入的对象未实现 proto.Message,有可能的原因是你传入的是一个值,而非指针对象,可以检查是否使用了&获取地址", v)
}
func ProtoUnmarshal(data []byte, v interface{}) error {
switch p := v.(type) {
case proto.Message:
return proto.Unmarshal(data, p)
case nil, *struct{}, struct{}:
return nil
}
return fmt.Errorf("protobuf 编码失败: %T 传入的对象未实现 proto.Message,有可能的原因是你传入的是一个值,而非指针对象,可以检查是否使用了&获取地址", v)
}