mq_coder/encoding/pvarbool.go

28 lines
562 B
Go
Raw Permalink Normal View History

2022-10-07 15:30:45 +08:00
package encoding
// EncodePVarBool encode value as PVarBool to buffer
func (codec *VarCodec) EncodePVarBool(buffer []byte, value bool) error {
tmp := int64(1)
if !value {
tmp = int64(0)
}
return codec.encodePVarInt(buffer, tmp)
}
// DecodePVarBool decode to value as PVarBool from buffer
func (codec *VarCodec) DecodePVarBool(buffer []byte, value *bool) error {
if len(buffer) == 0 {
*value = false
return nil
}
var tmp int64
var err = codec.decodePVarInt(buffer, &tmp)
if tmp == 1 {
*value = true
} else {
*value = false
}
return err
}