-
Notifications
You must be signed in to change notification settings - Fork 15
/
codec.go
77 lines (60 loc) · 1.89 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
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
package proxy
import (
"fmt"
"google.golang.org/grpc/encoding"
"google.golang.org/protobuf/proto"
)
// Codec returns a proxying [encoding.Coder] with the default protobuf codec as parent.
//
// See CodecWithParent.
func Codec() encoding.Codec {
return CodecWithParent(&protoCodec{})
}
// CodecWithParent returns a proxying [encoding.Codec] with a user provided codec as parent.
//
// This codec is *crucial* to the functioning of the proxy. It allows the proxy server to be oblivious
// to the schema of the forwarded messages. It basically treats a gRPC message frame as raw bytes.
// However, if the server handler, or the client caller are not proxy-internal functions it will fall back
// to trying to decode the message using a fallback codec.
func CodecWithParent(fallback encoding.Codec) encoding.Codec {
return &rawCodec{fallback}
}
type rawCodec struct {
parentCodec encoding.Codec
}
type frame struct {
payload []byte
}
// NewFrame constructs a frame for raw codec.
func NewFrame(payload []byte) interface{} {
return &frame{payload: payload}
}
func (c *rawCodec) Marshal(v interface{}) ([]byte, error) {
out, ok := v.(*frame)
if !ok {
return c.parentCodec.Marshal(v)
}
return out.payload, nil
}
func (c *rawCodec) Unmarshal(data []byte, v interface{}) error {
dst, ok := v.(*frame)
if !ok {
return c.parentCodec.Unmarshal(data, v)
}
dst.payload = data
return nil
}
func (c *rawCodec) Name() string {
return fmt.Sprintf("proxy>%s", c.parentCodec.Name())
}
// protoCodec is a Codec implementation with protobuf. It is the default rawCodec for gRPC.
type protoCodec struct{}
func (protoCodec) Marshal(v interface{}) ([]byte, error) {
return proto.Marshal(v.(proto.Message)) //nolint:forcetypeassert
}
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
return proto.Unmarshal(data, v.(proto.Message)) //nolint:forcetypeassert
}
func (protoCodec) Name() string {
return "proto"
}