remove codecs
This commit is contained in:
38
codec/bytes/marshaler.go
Normal file
38
codec/bytes/marshaler.go
Normal 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"
|
||||
}
|
@@ -38,6 +38,15 @@ type Writer interface {
|
||||
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
|
||||
// the communication, likely followed by the body.
|
||||
// In the case of an error, body may be nil.
|
||||
|
19
codec/json/marshaler.go
Normal file
19
codec/json/marshaler.go
Normal 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
19
codec/proto/marshaler.go
Normal 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"
|
||||
}
|
Reference in New Issue
Block a user