remove codecs

This commit is contained in:
Asim Aslam 2019-01-10 09:42:02 +00:00
parent 6e0e4a684c
commit c086c33bb3
13 changed files with 93 additions and 148 deletions

View File

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

View File

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

View File

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

View File

@ -20,7 +20,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/micro/go-log" "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" merr "github.com/micro/go-micro/errors"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-rcache" "github.com/micro/go-rcache"
@ -108,7 +108,7 @@ func newTransport(config *tls.Config) *http.Transport {
func newHttpBroker(opts ...Option) Broker { func newHttpBroker(opts ...Option) Broker {
options := Options{ options := Options{
Codec: json.NewCodec(), Codec: json.Marshaler{},
Context: context.TODO(), Context: context.TODO(),
} }

View File

@ -4,14 +4,14 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"github.com/micro/go-micro/broker/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
) )
type Options struct { type Options struct {
Addrs []string Addrs []string
Secure bool Secure bool
Codec codec.Codec Codec codec.Marshaler
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
@ -71,7 +71,7 @@ func Addrs(addrs ...string) Option {
// Codec sets the codec used for encoding/decoding used where // Codec sets the codec used for encoding/decoding used where
// a broker does not support headers // a broker does not support headers
func Codec(c codec.Codec) Option { func Codec(c codec.Marshaler) Option {
return func(o *Options) { return func(o *Options) {
o.Codec = c o.Codec = c
} }

38
codec/bytes/marshaler.go Normal file
View File

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

View File

@ -38,6 +38,15 @@ type Writer interface {
Write(*Message, interface{}) error 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 // Message represents detailed information about
// the communication, likely followed by the body. // the communication, likely followed by the body.
// In the case of an error, body may be nil. // In the case of an error, body may be nil.

19
codec/json/marshaler.go Normal file
View File

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

19
codec/proto/marshaler.go Normal file
View File

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

View File

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

View File

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

View File

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

View File

@ -5,12 +5,12 @@ import (
"crypto/tls" "crypto/tls"
"time" "time"
"github.com/micro/go-micro/transport/codec" "github.com/micro/go-micro/codec"
) )
type Options struct { type Options struct {
Addrs []string Addrs []string
Codec codec.Codec Codec codec.Marshaler
Secure bool Secure bool
TLSConfig *tls.Config TLSConfig *tls.Config
// Timeout sets the timeout for Send/Recv // 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 // Codec sets the codec used for encoding where the transport
// does not support message headers // does not support message headers
func Codec(c codec.Codec) Option { func Codec(c codec.Marshaler) Option {
return func(o *Options) { return func(o *Options) {
o.Codec = c o.Codec = c
} }