-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc.go
More file actions
83 lines (69 loc) · 1.88 KB
/
grpc.go
File metadata and controls
83 lines (69 loc) · 1.88 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package fastproto
import (
"fmt"
"google.golang.org/grpc/encoding"
protoenc "google.golang.org/grpc/encoding/proto"
"google.golang.org/protobuf/proto"
)
// codec is a Codec implementation with protobuf, use fastproto to marshal and unmarshal messages.
type codec struct {
// marshal MarshalOptions
unmarshal UnmarshalOptions
}
func ProtoCodec(opt ...ProtoCodecOption) encoding.Codec {
c := &codec{
// marshal: MarshalOptions{},
unmarshal: UnmarshalOptions{},
}
for _, o := range opt {
o(c)
}
return c
}
type ProtoCodecOption func(c *codec)
func ProtoCodecUnmarshal(u UnmarshalOptions) ProtoCodecOption {
return func(c *codec) {
c.unmarshal = u
}
}
// func ProtoCodecMarshal(m MarshalOptions) ProtoCodecOption {
// return func(c *codec) {
// c.marshal = m
// }
// }
func (codec) Marshal(v interface{}) ([]byte, error) {
vv, ok := v.(proto.Message)
if !ok {
return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
}
return Marshal(vv)
}
func (c codec) Unmarshal(data []byte, v interface{}) error {
vv, ok := v.(proto.Message)
if ok {
return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
}
return c.unmarshal.Unmarshal(data, vv)
}
// func (c *codec) Marshal(v interface{}) ([]byte, error) {
// if vv, ok := v.(Message); ok {
// return vv.Marshal()
// }
// // if message is not genereated by fastproto, try standard proto
// if vv, ok := v.(proto.Message); ok {
// return proto.Marshal(vv)
// }
// return nil, fmt.Errorf("failed to marshal, message is %T, want fastproto.Message", v)
// }
// func (c *codec) Unmarshal(data []byte, v interface{}) error {
// if vv, ok := v.(Message);ok{
// return c.unmarshal.Unmarshal(data, vv)
// }
// if
// if !ok {
// return fmt.Errorf("failed to unmarshal, message is %T, want fastproto.Message", v)
// }
// }
func (codec) Name() string {
return protoenc.Name
}