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

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