2020-11-23 16:18:47 +03:00
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
2021-01-29 16:07:21 +03:00
|
|
|
"encoding/json"
|
2020-11-23 16:18:47 +03:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type noopCodec struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Frame gives us the ability to define raw data to send over the pipes
|
|
|
|
type Frame struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:53:29 +03:00
|
|
|
func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error {
|
2020-11-23 16:18:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:53:29 +03:00
|
|
|
func (c *noopCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-11-23 16:18:47 +03:00
|
|
|
// read bytes
|
2021-02-17 23:38:32 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2020-11-23 16:18:47 +03:00
|
|
|
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)
|
2020-11-23 16:18:47 +03:00
|
|
|
case *[]byte:
|
|
|
|
*v = buf
|
|
|
|
case *Frame:
|
|
|
|
v.Data = buf
|
|
|
|
default:
|
2021-01-29 16:07:21 +03:00
|
|
|
return json.Unmarshal(buf, v)
|
2020-11-23 16:18:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:53:29 +03:00
|
|
|
func (c *noopCodec) Write(conn io.Writer, 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:
|
2021-01-29 16:07:21 +03:00
|
|
|
var err error
|
|
|
|
v, err = json.Marshal(vb)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-23 16:18:47 +03:00
|
|
|
}
|
|
|
|
_, err := conn.Write(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *noopCodec) String() string {
|
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewCodec returns new noop codec
|
2020-11-23 16:18:47 +03:00
|
|
|
func NewCodec() Codec {
|
|
|
|
return &noopCodec{}
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
func (c *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
|
|
|
|
}
|
2021-01-29 16:07:21 +03:00
|
|
|
|
|
|
|
return json.Marshal(v)
|
2020-11-23 16:18:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
func (c *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)
|
2021-01-29 23:18:12 +03:00
|
|
|
return nil
|
2020-11-23 16:18:47 +03:00
|
|
|
case *[]byte:
|
|
|
|
*ve = d
|
2021-01-29 23:18:12 +03:00
|
|
|
return nil
|
2020-11-25 09:59:28 +03:00
|
|
|
case *Frame:
|
|
|
|
ve.Data = d
|
2021-01-29 23:18:12 +03:00
|
|
|
return nil
|
2020-11-23 16:18:47 +03:00
|
|
|
case *Message:
|
|
|
|
ve.Body = d
|
2021-01-29 23:18:12 +03:00
|
|
|
return nil
|
2020-11-23 16:18:47 +03:00
|
|
|
}
|
2020-12-11 00:32:29 +03:00
|
|
|
|
2021-01-29 16:07:21 +03:00
|
|
|
return json.Unmarshal(d, v)
|
2020-11-23 16:18:47 +03:00
|
|
|
}
|