add string support to noop codec

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-25 10:04:11 +03:00
parent 2101e994d9
commit 458388359a

View File

@ -29,6 +29,10 @@ func (c *noopCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
} }
switch v := b.(type) { switch v := b.(type) {
case string:
v = string(buf)
case *string:
*v = string(buf)
case []byte: case []byte:
v = buf v = buf
case *[]byte: case *[]byte:
@ -43,12 +47,18 @@ func (c *noopCodec) ReadBody(conn io.ReadWriter, b interface{}) error {
} }
func (c *noopCodec) Write(conn io.ReadWriter, m *Message, b interface{}) error { func (c *noopCodec) Write(conn io.ReadWriter, m *Message, b interface{}) error {
if b == nil {
return nil
}
var v []byte var v []byte
switch vb := b.(type) { switch vb := b.(type) {
case nil:
return nil
case *Frame: case *Frame:
v = vb.Data v = vb.Data
case string:
v = []byte(vb)
case *string:
v = []byte(*vb)
case *[]byte: case *[]byte:
v = *vb v = *vb
case []byte: case []byte:
@ -69,7 +79,15 @@ func NewCodec() Codec {
} }
func (n *noopCodec) Marshal(v interface{}) ([]byte, error) { func (n *noopCodec) Marshal(v interface{}) ([]byte, error) {
if v == nil {
return nil, nil
}
switch ve := v.(type) { switch ve := v.(type) {
case string:
return []byte(ve), nil
case *string:
return []byte(*ve), nil
case *[]byte: case *[]byte:
return *ve, nil return *ve, nil
case []byte: case []byte:
@ -83,7 +101,15 @@ func (n *noopCodec) Marshal(v interface{}) ([]byte, error) {
} }
func (n *noopCodec) Unmarshal(d []byte, v interface{}) error { func (n *noopCodec) Unmarshal(d []byte, v interface{}) error {
if v == nil {
return nil
}
switch ve := v.(type) { switch ve := v.(type) {
case string:
ve = string(d)
case *string:
*ve = string(d)
case []byte: case []byte:
ve = d ve = d
case *[]byte: case *[]byte: