Add configurable codecs for broker/transport

This commit is contained in:
Asim Aslam 2016-12-06 18:37:35 +00:00
parent e10259940b
commit bd3c798fd9
6 changed files with 100 additions and 3 deletions

View File

@ -31,12 +31,22 @@ type Publication interface {
Ack() error Ack() error
} }
// Subscriber is a convenience return type for the Subscribe method
type Subscriber interface { type Subscriber interface {
Options() SubscribeOptions Options() SubscribeOptions
Topic() string Topic() string
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()
) )

19
broker/codec.go Normal file
View File

@ -0,0 +1,19 @@
package broker
import (
"encoding/json"
)
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"
}

25
broker/codec/json/json.go Normal file
View File

@ -0,0 +1,25 @@
package json
import (
"encoding/json"
"github.com/micro/go-micro/broker"
)
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() broker.Codec {
return jsonCodec{}
}

34
broker/codec/noop/noop.go Normal file
View File

@ -0,0 +1,34 @@
package noop
import (
"errors"
"github.com/micro/go-micro/broker"
)
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() broker.Codec {
return noopCodec{}
}

View File

@ -3,7 +3,6 @@ package broker
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -96,6 +95,7 @@ func newTransport(config *tls.Config) *http.Transport {
func newHttpBroker(opts ...Option) Broker { func newHttpBroker(opts ...Option) Broker {
options := Options{ options := Options{
Codec: jsonCodec{},
Context: context.TODO(), Context: context.TODO(),
} }
@ -269,7 +269,7 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
var m *Message var m *Message
if err = json.Unmarshal(b, &m); err != nil { if err = h.opts.Codec.Unmarshal(b, &m); err != nil {
errr := errors.InternalServerError("go.micro.broker", fmt.Sprintf("Error parsing request body: %v", err)) errr := errors.InternalServerError("go.micro.broker", fmt.Sprintf("Error parsing request body: %v", err))
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte(errr.Error())) w.Write([]byte(errr.Error()))
@ -352,7 +352,7 @@ func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption)
m.Header[":topic"] = topic m.Header[":topic"] = topic
b, err := json.Marshal(m) b, err := h.opts.Codec.Marshal(m)
if err != nil { if err != nil {
return err return err
} }

View File

@ -10,6 +10,7 @@ import (
type Options struct { type Options struct {
Addrs []string Addrs []string
Secure bool Secure bool
Codec Codec
TLSConfig *tls.Config TLSConfig *tls.Config
// Other options for implementations of the interface // Other options for implementations of the interface
@ -96,6 +97,14 @@ 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) {