Documentation
¶
Overview ¶
Package codec defines the business communication protocol of packing and unpacking.
Index ¶
- Constants
- Variables
- func Deserialize(ctx context.Context, in []byte, body interface{}) error
- func GetRespFromReqHeader(reqHeader *proto.RequestHeader) *proto.ResponseHeader
- func InitGlobalContext(env, container, callee string)
- func RegisterSerializer(serializationType int, s Serializer)
- func Serialize(ctx context.Context, body interface{}) ([]byte, error)
- type Codec
- type JSONSerialization
- type Serializer
- type XMLSerialization
Constants ¶
const ( // SerializationTypeJSON is json serialization code. SerializationTypeJSON = 0 // SerializationTypeXML is used to export xml body SerializationTypeXML = 1 )
SerializationType defines the type of different serializers, such as json, xml
Variables ¶
var (
GCtx context.Context
)
Functions ¶
func Deserialize ¶
Deserialize the input bytes into body. The specific serialization mode is defined by type, json is default mode.
func GetRespFromReqHeader ¶
func GetRespFromReqHeader(reqHeader *proto.RequestHeader) *proto.ResponseHeader
func InitGlobalContext ¶
func InitGlobalContext(env, container, callee string)
InitGlobalContext 获取初始化的 context、msg
func RegisterSerializer ¶
func RegisterSerializer(serializationType int, s Serializer)
RegisterSerializer registers serializer
Types ¶
type Codec ¶
type Codec interface {
// Encode pack the body into binary buffer.
Encode(msg *codec.Msg, body []byte) (respBody []byte, err error)
// Decode unpack the body from binary buffer
Decode(message *codec.Msg, buf []byte) (reqBody []byte, err error)
}
Codec defines the interface of business communication protocol, which contains head and body. It only parses the body in binary
type JSONSerialization ¶
type JSONSerialization struct{}
JSONSerialization provides json serialization mode.
func (*JSONSerialization) Deserialize ¶
func (s *JSONSerialization) Deserialize(in []byte, body interface{}) error
Deserialize json unmarshal the input bytes into body.
func (*JSONSerialization) Serialize ¶
func (s *JSONSerialization) Serialize(body interface{}) ([]byte, error)
Serialize json marshal the body into output bytes
type Serializer ¶
type Serializer interface {
// Deserialize the input bytes into body
Deserialize(in []byte, body interface{}) error
// Serialize the body to output byte
Serialize(body interface{}) (out []byte, err error)
}
Serializer defines body serialization interface.
func GetSerializer ¶
func GetSerializer(serializationType int) Serializer
GetSerializer returns the serializer by type.
type XMLSerialization ¶
type XMLSerialization struct{}
XMLSerialization export xml body
func (*XMLSerialization) Deserialize ¶
func (j *XMLSerialization) Deserialize(in []byte, body interface{}) error
Deserialize the input xml into body
func (*XMLSerialization) Serialize ¶
func (j *XMLSerialization) Serialize(body interface{}) ([]byte, error)
Serialize the body to output xml.