libs5-go/serialize/seralizer.go

43 lines
735 B
Go
Raw Normal View History

2024-01-04 13:21:19 +00:00
package serialize
import (
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
)
func InitMarshaller(kind types.MetadataType, enc *msgpack.Encoder) error {
2024-01-04 18:49:23 +00:00
err := enc.EncodeInt(types.MetadataMagicByte)
2024-01-04 13:21:19 +00:00
if err != nil {
return err
}
2024-01-04 18:49:23 +00:00
err = enc.EncodeInt(int64(kind))
2024-01-04 13:21:19 +00:00
if err != nil {
return err
}
return nil
}
func InitUnmarshaller(kind types.MetadataType, enc *msgpack.Decoder) error {
val, err := enc.DecodeUint8()
if err != nil {
return err
}
if val != types.MetadataMagicByte {
return errors.New("Invalid magic byte")
}
val, err = enc.DecodeUint8()
if err != nil {
return err
}
if val != uint8(kind) {
return errors.New("Invalid metadata type")
}
return nil
}