2020-11-23 16:18:47 +03:00
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type noopCodec struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Frame gives us the ability to define raw data to send over the pipes
|
|
|
|
type Frame struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *noopCodec) ReadHeader(conn io.ReadWriter, m *Message, t MessageType) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *noopCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
|
|
|
|
// read bytes
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := b.(type) {
|
2020-11-25 10:04:11 +03:00
|
|
|
case string:
|
|
|
|
v = string(buf)
|
|
|
|
case *string:
|
|
|
|
*v = string(buf)
|
2020-11-23 16:18:47 +03:00
|
|
|
case []byte:
|
|
|
|
v = buf
|
|
|
|
case *[]byte:
|
|
|
|
*v = buf
|
|
|
|
case *Frame:
|
|
|
|
v.Data = buf
|
|
|
|
default:
|
|
|
|
return ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *noopCodec) Write(conn io.ReadWriter, m *Message, b interface{}) error {
|
2020-11-25 10:04:11 +03:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
var v []byte
|
|
|
|
switch vb := b.(type) {
|
|
|
|
case *Frame:
|
|
|
|
v = vb.Data
|
2020-11-25 10:04:11 +03:00
|
|
|
case string:
|
|
|
|
v = []byte(vb)
|
|
|
|
case *string:
|
|
|
|
v = []byte(*vb)
|
2020-11-23 16:18:47 +03:00
|
|
|
case *[]byte:
|
|
|
|
v = *vb
|
|
|
|
case []byte:
|
|
|
|
v = vb
|
|
|
|
default:
|
|
|
|
return ErrInvalidMessage
|
|
|
|
}
|
|
|
|
_, err := conn.Write(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *noopCodec) String() string {
|
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec() Codec {
|
|
|
|
return &noopCodec{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopCodec) Marshal(v interface{}) ([]byte, error) {
|
2020-11-25 10:04:11 +03:00
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
switch ve := v.(type) {
|
2020-11-25 10:04:11 +03:00
|
|
|
case string:
|
|
|
|
return []byte(ve), nil
|
|
|
|
case *string:
|
|
|
|
return []byte(*ve), nil
|
2020-11-23 16:18:47 +03:00
|
|
|
case *[]byte:
|
|
|
|
return *ve, nil
|
|
|
|
case []byte:
|
|
|
|
return ve, nil
|
2020-11-25 09:59:28 +03:00
|
|
|
case *Frame:
|
|
|
|
return ve.Data, nil
|
2020-11-23 16:18:47 +03:00
|
|
|
case *Message:
|
|
|
|
return ve.Body, nil
|
|
|
|
}
|
|
|
|
return nil, ErrInvalidMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopCodec) Unmarshal(d []byte, v interface{}) error {
|
2020-11-25 10:04:11 +03:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:18:47 +03:00
|
|
|
switch ve := v.(type) {
|
2020-11-25 10:04:11 +03:00
|
|
|
case string:
|
|
|
|
ve = string(d)
|
|
|
|
case *string:
|
|
|
|
*ve = string(d)
|
2020-11-23 16:18:47 +03:00
|
|
|
case []byte:
|
|
|
|
ve = d
|
|
|
|
case *[]byte:
|
|
|
|
*ve = d
|
2020-11-25 09:59:28 +03:00
|
|
|
case *Frame:
|
|
|
|
ve.Data = d
|
2020-11-23 16:18:47 +03:00
|
|
|
case *Message:
|
|
|
|
ve.Body = d
|
|
|
|
}
|
|
|
|
return ErrInvalidMessage
|
|
|
|
}
|