move broker codec

This commit is contained in:
Asim Aslam 2016-12-06 18:59:41 +00:00
parent 49e5636bcd
commit 25a6849609
5 changed files with 24 additions and 22 deletions

View File

@ -38,15 +38,6 @@ type Subscriber interface {
Unsubscribe() error 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 ( var (
DefaultBroker Broker = newHttpBroker() DefaultBroker Broker = newHttpBroker()
) )

10
broker/codec/codec.go Normal file
View File

@ -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
}

View File

@ -3,7 +3,7 @@ package json
import ( import (
"encoding/json" "encoding/json"
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker/codec"
) )
type jsonCodec struct{} type jsonCodec struct{}
@ -20,6 +20,6 @@ func (j jsonCodec) String() string {
return "json" return "json"
} }
func NewCodec() broker.Codec { func NewCodec() codec.Codec {
return jsonCodec{} return jsonCodec{}
} }

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/broker/codec"
) )
type noopCodec struct{} type noopCodec struct{}
@ -29,6 +30,6 @@ func (n noopCodec) String() string {
return "noop" return "noop"
} }
func NewCodec() broker.Codec { func NewCodec() codec.Codec {
return noopCodec{} return noopCodec{}
} }

View File

@ -3,6 +3,7 @@ package broker
import ( import (
"crypto/tls" "crypto/tls"
"github.com/micro/go-micro/broker/codec"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -10,9 +11,8 @@ import (
type Options struct { type Options struct {
Addrs []string Addrs []string
Secure bool Secure bool
Codec Codec Codec codec.Codec
TLSConfig *tls.Config TLSConfig *tls.Config
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context
Context context.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 // DisableAutoAck will disable auto acking of messages
// after they have been handled. // after they have been handled.
func DisableAutoAck() SubscribeOption { 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 // Specify TLS Config
func TLSConfig(t *tls.Config) Option { func TLSConfig(t *tls.Config) Option {
return func(o *Options) { return func(o *Options) {