codec

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package codec defines the business communication protocol of packing and unpacking.

Index

Constants

View Source
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

View Source
var (
	GCtx context.Context
)

Functions

func Deserialize

func Deserialize(ctx context.Context, in []byte, body interface{}) error

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

func Serialize

func Serialize(ctx context.Context, body interface{}) ([]byte, error)

Serialize the body to output byte The specific serialization mode is defined by type, json is default mode.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL