mq_coder/primitive_packet.go

101 lines
2.2 KiB
Go
Raw Normal View History

2022-10-10 14:47:49 +08:00
package mq_coder
import (
"git.hpds.cc/Component/mq_coder/encoding"
)
// 最小长度为2个字节
const primitivePacketBufferMinimalLength = 2
// PrimitivePacket describes primitive value type,
type PrimitivePacket struct {
*packet
}
// ToInt32 parse raw as int32 value
func (p *PrimitivePacket) ToInt32() (int32, error) {
var val int32
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodeNVarInt32(p.packet.valBuf, &val)
if err != nil {
return 0, err
}
return val, nil
}
// ToUInt32 parse raw as uint32 value
func (p *PrimitivePacket) ToUInt32() (uint32, error) {
var val uint32
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodeNVarUInt32(p.valBuf, &val)
if err != nil {
return 0, err
}
return val, nil
}
// ToInt64 parse raw as int64 value
func (p *PrimitivePacket) ToInt64() (int64, error) {
var val int64
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodeNVarInt64(p.valBuf, &val)
if err != nil {
return 0, err
}
return val, nil
}
// ToUInt64 parse raw as uint64 value
func (p *PrimitivePacket) ToUInt64() (uint64, error) {
var val uint64
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodeNVarUInt64(p.valBuf, &val)
if err != nil {
return 0, err
}
return val, nil
}
// ToFloat32 parse raw as float32 value
func (p *PrimitivePacket) ToFloat32() (float32, error) {
var val float32
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodeVarFloat32(p.valBuf, &val)
if err != nil {
return 0, err
}
return val, nil
}
// ToFloat64 parse raw as float64 value
func (p *PrimitivePacket) ToFloat64() (float64, error) {
var val float64
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodeVarFloat64(p.valBuf, &val)
if err != nil {
return 0, err
}
return val, nil
}
// ToBool parse raw as bool value
func (p *PrimitivePacket) ToBool() (bool, error) {
var val bool
codec := encoding.VarCodec{Size: len(p.valBuf)}
err := codec.DecodePVarBool(p.valBuf, &val)
if err != nil {
return false, err
}
return val, nil
}
// ToUTF8String parse raw data as string value
func (p *PrimitivePacket) ToUTF8String() (string, error) {
return string(p.valBuf), nil
}
// ToBytes returns raw buffer data
func (p *PrimitivePacket) ToBytes() []byte {
return p.valBuf
}