diff --git a/broker/codec/codec.go b/broker/codec/codec.go deleted file mode 100644 index 5ebd05f1..00000000 --- a/broker/codec/codec.go +++ /dev/null @@ -1,10 +0,0 @@ -package codec - -// Codec is used for encoding where the broker 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/broker/codec/json/json.go b/broker/codec/json/json.go deleted file mode 100644 index 25d2ab20..00000000 --- a/broker/codec/json/json.go +++ /dev/null @@ -1,25 +0,0 @@ -package json - -import ( - "encoding/json" - - "github.com/micro/go-micro/broker/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/broker/codec/noop/noop.go b/broker/codec/noop/noop.go deleted file mode 100644 index 82633a24..00000000 --- a/broker/codec/noop/noop.go +++ /dev/null @@ -1,35 +0,0 @@ -package noop - -import ( - "errors" - - "github.com/micro/go-micro/broker" - "github.com/micro/go-micro/broker/codec" -) - -type noopCodec struct{} - -func (n noopCodec) Marshal(v interface{}) ([]byte, error) { - msg, ok := v.(*broker.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.(*broker.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/broker/http_broker.go b/broker/http_broker.go index 3e842b3c..c04f47eb 100644 --- a/broker/http_broker.go +++ b/broker/http_broker.go @@ -20,7 +20,7 @@ import ( "github.com/google/uuid" "github.com/micro/go-log" - "github.com/micro/go-micro/broker/codec/json" + "github.com/micro/go-micro/codec/json" merr "github.com/micro/go-micro/errors" "github.com/micro/go-micro/registry" "github.com/micro/go-rcache" @@ -108,7 +108,7 @@ func newTransport(config *tls.Config) *http.Transport { func newHttpBroker(opts ...Option) Broker { options := Options{ - Codec: json.NewCodec(), + Codec: json.Marshaler{}, Context: context.TODO(), } diff --git a/broker/options.go b/broker/options.go index ae55ede1..0a909f79 100644 --- a/broker/options.go +++ b/broker/options.go @@ -4,14 +4,14 @@ import ( "context" "crypto/tls" - "github.com/micro/go-micro/broker/codec" + "github.com/micro/go-micro/codec" "github.com/micro/go-micro/registry" ) type Options struct { Addrs []string Secure bool - Codec codec.Codec + Codec codec.Marshaler TLSConfig *tls.Config // Other options for implementations of the interface // can be stored in a context @@ -71,7 +71,7 @@ func Addrs(addrs ...string) Option { // Codec sets the codec used for encoding/decoding used where // a broker does not support headers -func Codec(c codec.Codec) Option { +func Codec(c codec.Marshaler) Option { return func(o *Options) { o.Codec = c } diff --git a/codec/bytes/marshaler.go b/codec/bytes/marshaler.go new file mode 100644 index 00000000..8f8d188f --- /dev/null +++ b/codec/bytes/marshaler.go @@ -0,0 +1,38 @@ +package bytes + +import ( + "errors" +) + +type Marshaler struct{} + +type Message struct { + Header map[string]string + Body []byte +} + +func (n Marshaler) Marshal(v interface{}) ([]byte, error) { + switch v.(type) { + case []byte: + return v.([]byte), nil + case *Message: + return v.(*Message).Body, nil + } + return nil, errors.New("invalid message") +} + +func (n Marshaler) Unmarshal(d []byte, v interface{}) error { + switch v.(type) { + case *[]byte: + ve := v.(*[]byte) + *ve = d + case *Message: + ve := v.(*Message) + ve.Body = d + } + return errors.New("invalid message") +} + +func (n Marshaler) String() string { + return "bytes" +} diff --git a/codec/codec.go b/codec/codec.go index 8277cf51..9f4cbde6 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -38,6 +38,15 @@ type Writer interface { Write(*Message, interface{}) error } + +// Marshaler is a simple encoding interface used for the broker/transport +// where headers are not supported by the underlying implementation. +type Marshaler interface { + Marshal(interface{}) ([]byte, error) + Unmarshal([]byte, interface{}) error + String() string +} + // Message represents detailed information about // the communication, likely followed by the body. // In the case of an error, body may be nil. diff --git a/codec/json/marshaler.go b/codec/json/marshaler.go new file mode 100644 index 00000000..b9d0be28 --- /dev/null +++ b/codec/json/marshaler.go @@ -0,0 +1,19 @@ +package json + +import ( + "encoding/json" +) + +type Marshaler struct{} + +func (j Marshaler) Marshal(v interface{}) ([]byte, error) { + return json.Marshal(v) +} + +func (j Marshaler) Unmarshal(d []byte, v interface{}) error { + return json.Unmarshal(d, v) +} + +func (j Marshaler) String() string { + return "json" +} diff --git a/codec/proto/marshaler.go b/codec/proto/marshaler.go new file mode 100644 index 00000000..e2889fa7 --- /dev/null +++ b/codec/proto/marshaler.go @@ -0,0 +1,19 @@ +package proto + +import ( + "github.com/golang/protobuf/proto" +) + +type Marshaler struct{} + +func (Marshaler) Marshal(v interface{}) ([]byte, error) { + return proto.Marshal(v.(proto.Message)) +} + +func (Marshaler) Unmarshal(data []byte, v interface{}) error { + return proto.Unmarshal(data, v.(proto.Message)) +} + +func (Marshaler) Name() string { + return "proto" +} diff --git a/transport/codec/codec.go b/transport/codec/codec.go deleted file mode 100644 index 85e89320..00000000 --- a/transport/codec/codec.go +++ /dev/null @@ -1,10 +0,0 @@ -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 deleted file mode 100644 index 0adaab04..00000000 --- a/transport/codec/json/json.go +++ /dev/null @@ -1,25 +0,0 @@ -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 deleted file mode 100644 index 44ac91a1..00000000 --- a/transport/codec/noop/noop.go +++ /dev/null @@ -1,35 +0,0 @@ -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 a62d1194..5aaebe4b 100644 --- a/transport/options.go +++ b/transport/options.go @@ -5,12 +5,12 @@ import ( "crypto/tls" "time" - "github.com/micro/go-micro/transport/codec" + "github.com/micro/go-micro/codec" ) type Options struct { Addrs []string - Codec codec.Codec + Codec codec.Marshaler Secure bool TLSConfig *tls.Config // Timeout sets the timeout for Send/Recv @@ -50,7 +50,7 @@ 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 { +func Codec(c codec.Marshaler) Option { return func(o *Options) { o.Codec = c }