github.com/profzone/eden-framework@v1.0.10/pkg/courier/transport_grpc/msgpack_codec.go (about)

     1  package transport_grpc
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/vmihailenco/msgpack"
     7  )
     8  
     9  type MsgPackCodec struct {
    10  }
    11  
    12  func (c *MsgPackCodec) Marshal(v interface{}) ([]byte, error) {
    13  	return msgpack.Marshal(v)
    14  }
    15  
    16  func MsgPackUnmarshal(data []byte, v interface{}) error {
    17  	return msgpack.Unmarshal(data, v)
    18  }
    19  
    20  func (c *MsgPackCodec) Unmarshal(data []byte, v interface{}) error {
    21  	// delay Unmarshal
    22  	rv := reflect.ValueOf(v)
    23  	for rv.Kind() == reflect.Ptr {
    24  		rv = rv.Elem()
    25  	}
    26  	if _, ok := rv.Interface().([]byte); ok {
    27  		rv.SetBytes(data)
    28  	}
    29  	return nil
    30  }
    31  
    32  func (c *MsgPackCodec) String() string {
    33  	return "MsgPackCodec"
    34  }