From 49e5636bcd919a4898e3061180156095998bc634 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 6 Dec 2016 18:56:57 +0000 Subject: [PATCH] add transport codec --- transport/codec/codec.go | 10 ++++++++++ transport/codec/json/json.go | 25 +++++++++++++++++++++++++ transport/codec/noop/noop.go | 35 +++++++++++++++++++++++++++++++++++ transport/options.go | 10 ++++++++++ 4 files changed, 80 insertions(+) create mode 100644 transport/codec/codec.go create mode 100644 transport/codec/json/json.go create mode 100644 transport/codec/noop/noop.go diff --git a/transport/codec/codec.go b/transport/codec/codec.go new file mode 100644 index 00000000..85e89320 --- /dev/null +++ b/transport/codec/codec.go @@ -0,0 +1,10 @@ +package codec + +// Codec is used for encoding where the transport doesn't natively support +// headers in the message type. In this case the entire message is +// encoded as the payload +type Codec interface { + Marshal(interface{}) ([]byte, error) + Unmarshal([]byte, interface{}) error + String() string +} diff --git a/transport/codec/json/json.go b/transport/codec/json/json.go new file mode 100644 index 00000000..0adaab04 --- /dev/null +++ b/transport/codec/json/json.go @@ -0,0 +1,25 @@ +package json + +import ( + "encoding/json" + + "github.com/micro/go-micro/transport/codec" +) + +type jsonCodec struct{} + +func (j jsonCodec) Marshal(v interface{}) ([]byte, error) { + return json.Marshal(v) +} + +func (j jsonCodec) Unmarshal(d []byte, v interface{}) error { + return json.Unmarshal(d, v) +} + +func (j jsonCodec) String() string { + return "json" +} + +func NewCodec() codec.Codec { + return jsonCodec{} +} diff --git a/transport/codec/noop/noop.go b/transport/codec/noop/noop.go new file mode 100644 index 00000000..44ac91a1 --- /dev/null +++ b/transport/codec/noop/noop.go @@ -0,0 +1,35 @@ +package noop + +import ( + "errors" + + "github.com/micro/go-micro/transport" + "github.com/micro/go-micro/transport/codec" +) + +type noopCodec struct{} + +func (n noopCodec) Marshal(v interface{}) ([]byte, error) { + msg, ok := v.(*transport.Message) + if !ok { + return nil, errors.New("invalid message") + } + return msg.Body, nil +} + +func (n noopCodec) Unmarshal(d []byte, v interface{}) error { + msg, ok := v.(*transport.Message) + if !ok { + return errors.New("invalid message") + } + msg.Body = d + return nil +} + +func (n noopCodec) String() string { + return "noop" +} + +func NewCodec() codec.Codec { + return noopCodec{} +} diff --git a/transport/options.go b/transport/options.go index de5ee039..36b48225 100644 --- a/transport/options.go +++ b/transport/options.go @@ -4,11 +4,13 @@ import ( "crypto/tls" "time" + "github.com/micro/go-micro/transport/codec" "golang.org/x/net/context" ) type Options struct { Addrs []string + Codec codec.Codec Secure bool TLSConfig *tls.Config // Timeout sets the timeout for Send/Recv @@ -46,6 +48,14 @@ func Addrs(addrs ...string) Option { } } +// Codec sets the codec used for encoding where the transport +// does not support message headers +func Codec(c codec.Codec) Option { + return func(o *Options) { + o.Codec = c + } +} + // Timeout sets the timeout for Send/Recv execution func Timeout(t time.Duration) Option { return func(o *Options) {