micro-server-grpc/codec.go

50 lines
1011 B
Go
Raw Permalink Normal View History

2019-06-03 20:44:43 +03:00
package grpc
import (
"go.unistack.org/micro/v3/codec"
2019-06-03 20:44:43 +03:00
"google.golang.org/grpc/encoding"
)
var (
_ codec.Codec = &wrapGrpcCodec{}
_ encoding.Codec = &wrapMicroCodec{}
)
type wrapMicroCodec struct{ codec.Codec }
2019-06-17 22:05:58 +03:00
func (w *wrapMicroCodec) Name() string {
return w.Codec.String()
2019-06-03 20:44:43 +03:00
}
func (w *wrapMicroCodec) Marshal(v interface{}) ([]byte, error) {
return w.Codec.Marshal(v)
}
func (w *wrapMicroCodec) Unmarshal(d []byte, v interface{}) error {
return w.Codec.Unmarshal(d, v)
}
type wrapGrpcCodec struct{ encoding.Codec }
2019-06-03 20:44:43 +03:00
func (w *wrapGrpcCodec) String() string {
return w.Codec.Name()
2019-06-03 20:44:43 +03:00
}
func (w *wrapGrpcCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
if m, ok := v.(*codec.Frame); ok {
return m.Data, nil
2019-06-03 20:44:43 +03:00
}
return w.Codec.Marshal(v)
2019-06-03 20:44:43 +03:00
}
func (w *wrapGrpcCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
if d == nil || v == nil {
return nil
}
if m, ok := v.(*codec.Frame); ok {
m.Data = d
return nil
}
return w.Codec.Unmarshal(d, v)
2019-06-03 20:44:43 +03:00
}