mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-25 12:26:26 +00:00
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
|
package proxy
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
"google.golang.org/protobuf/proto"
|
||
|
)
|
||
|
|
||
|
// Codec returns a proxying grpc.Codec with the default protobuf codec as parent.
|
||
|
//
|
||
|
// See CodecWithParent.
|
||
|
//
|
||
|
// Deprecated: No longer necessary.
|
||
|
func Codec() grpc.Codec {
|
||
|
return CodecWithParent(&protoCodec{})
|
||
|
}
|
||
|
|
||
|
// CodecWithParent returns a proxying grpc.Codec with a user provided codec as parent.
|
||
|
//
|
||
|
// Deprecated: No longer necessary.
|
||
|
func CodecWithParent(fallback grpc.Codec) grpc.Codec {
|
||
|
return &rawCodec{fallback}
|
||
|
}
|
||
|
|
||
|
type rawCodec struct {
|
||
|
parentCodec grpc.Codec
|
||
|
}
|
||
|
|
||
|
type frame struct {
|
||
|
payload []byte
|
||
|
}
|
||
|
|
||
|
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) String() string {
|
||
|
return fmt.Sprintf("proxy>%s", c.parentCodec.String())
|
||
|
}
|
||
|
|
||
|
// 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))
|
||
|
}
|
||
|
|
||
|
func (protoCodec) Unmarshal(data []byte, v interface{}) error {
|
||
|
return proto.Unmarshal(data, v.(proto.Message))
|
||
|
}
|
||
|
|
||
|
func (protoCodec) String() string {
|
||
|
return "proto"
|
||
|
}
|