1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-12 22:07:47 +02:00

Allow bytes.Frame to be set to sent just bytes

This commit is contained in:
Asim Aslam
2019-01-16 15:27:57 +00:00
parent a9c0b95603
commit 784a89b488
3 changed files with 32 additions and 13 deletions

View File

@ -13,30 +13,41 @@ type Codec struct {
Conn io.ReadWriteCloser
}
// Frame gives us the ability to define raw data to send over the pipes
type Frame struct {
Data []byte
}
func (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error {
return nil
}
func (c *Codec) ReadBody(b interface{}) error {
v, ok := b.(*[]byte)
if !ok {
return fmt.Errorf("failed to read body: %v is not type of *[]byte", b)
}
// read bytes
buf, err := ioutil.ReadAll(c.Conn)
if err != nil {
return err
}
// set bytes
*v = buf
switch b.(type) {
case *[]byte:
v := b.(*[]byte)
*v = buf
case *Frame:
v := b.(*Frame)
v.Data = buf
default:
return fmt.Errorf("failed to read body: %v is not type of *[]byte", b)
}
return nil
}
func (c *Codec) Write(m *codec.Message, b interface{}) error {
var v []byte
switch b.(type) {
case *Frame:
v = b.(*Frame).Data
case *[]byte:
ve := b.(*[]byte)
v = *ve