diff --git a/broker/broker.go b/broker/broker.go index e63ad90e..c387962b 100644 --- a/broker/broker.go +++ b/broker/broker.go @@ -38,15 +38,6 @@ type Subscriber interface { Unsubscribe() error } -// 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 -} - var ( DefaultBroker Broker = newHttpBroker() ) diff --git a/broker/codec/codec.go b/broker/codec/codec.go new file mode 100644 index 00000000..5ebd05f1 --- /dev/null +++ b/broker/codec/codec.go @@ -0,0 +1,10 @@ +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 index f0cb8736..25d2ab20 100644 --- a/broker/codec/json/json.go +++ b/broker/codec/json/json.go @@ -3,7 +3,7 @@ package json import ( "encoding/json" - "github.com/micro/go-micro/broker" + "github.com/micro/go-micro/broker/codec" ) type jsonCodec struct{} @@ -20,6 +20,6 @@ func (j jsonCodec) String() string { return "json" } -func NewCodec() broker.Codec { +func NewCodec() codec.Codec { return jsonCodec{} } diff --git a/broker/codec/noop/noop.go b/broker/codec/noop/noop.go index 81918ed3..82633a24 100644 --- a/broker/codec/noop/noop.go +++ b/broker/codec/noop/noop.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/micro/go-micro/broker" + "github.com/micro/go-micro/broker/codec" ) type noopCodec struct{} @@ -29,6 +30,6 @@ func (n noopCodec) String() string { return "noop" } -func NewCodec() broker.Codec { +func NewCodec() codec.Codec { return noopCodec{} } diff --git a/broker/options.go b/broker/options.go index 7824a97e..932c7006 100644 --- a/broker/options.go +++ b/broker/options.go @@ -3,6 +3,7 @@ package broker import ( "crypto/tls" + "github.com/micro/go-micro/broker/codec" "github.com/micro/go-micro/registry" "golang.org/x/net/context" ) @@ -10,9 +11,8 @@ import ( type Options struct { Addrs []string Secure bool - Codec Codec + Codec codec.Codec TLSConfig *tls.Config - // Other options for implementations of the interface // can be stored in a context Context context.Context @@ -69,6 +69,14 @@ 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 { + return func(o *Options) { + o.Codec = c + } +} + // DisableAutoAck will disable auto acking of messages // after they have been handled. func DisableAutoAck() SubscribeOption { @@ -97,14 +105,6 @@ func Secure(b bool) Option { } } -// Codec sets the codec used for encoding/decoding used where -// a broker does not support headers -func SetCodec(c Codec) Option { - return func(o *Options) { - o.Codec = c - } -} - // Specify TLS Config func TLSConfig(t *tls.Config) Option { return func(o *Options) {