2022-10-01 21:09:54 +03:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-06-06 15:14:33 +03:00
|
|
|
"io"
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"github.com/vmihailenco/msgpack/v5/msgpcode"
|
2022-10-01 21:09:54 +03:00
|
|
|
)
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
type msgpackReader struct {
|
|
|
|
dec *msgpack.Decoder
|
|
|
|
containers []msgpackContainer
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
type msgpackContainer struct {
|
|
|
|
length int
|
|
|
|
assoc bool
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func NewMsgpackReader(r io.Reader) Reader {
|
|
|
|
return &msgpackReader{
|
|
|
|
dec: msgpack.NewDecoder(r),
|
|
|
|
containers: make([]msgpackContainer, 0, 2),
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) currentContainer() msgpackContainer {
|
|
|
|
if rd == nil || len(rd.containers) == 0 {
|
|
|
|
return msgpackContainer{}
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
|
|
|
|
return rd.containers[len(rd.containers)-1]
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadInt8(v *int8, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeInt8()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadInt16(v *int16, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeInt16()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadInt32(v *int32, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeInt32()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadInt64(v *int64, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeInt64()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadUint8(v *uint8, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeUint8()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadUint16(v *uint16, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeUint16()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadUint32(v *uint32, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeUint32()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadUint64(v *uint64, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeUint64()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadBool(v *bool, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeBool()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadFloat32(v *float32, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeFloat32()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadFloat64(v *float64, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeFloat64()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadString(v *string, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeString()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
|
|
|
return nil
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ReadBytes(v *[]byte, field string) error {
|
|
|
|
tmp, err := rd.dec.DecodeBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
*v = tmp
|
2022-10-01 21:09:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) BeginContainer(field string) error {
|
|
|
|
code, err := rd.dec.PeekCode()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
switch {
|
|
|
|
case msgpcode.IsFixedMap(code), code == msgpcode.Map16, code == msgpcode.Map32:
|
|
|
|
l, err := rd.dec.DecodeMapLen()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
rd.containers = append(rd.containers, msgpackContainer{
|
|
|
|
length: l,
|
|
|
|
assoc: true,
|
|
|
|
})
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
case msgpcode.IsFixedArray(code), code == msgpcode.Array16, code == msgpcode.Array32:
|
|
|
|
l, err := rd.dec.DecodeArrayLen()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
rd.containers = append(rd.containers, msgpackContainer{
|
|
|
|
length: l,
|
|
|
|
assoc: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.Errorf("there is no container for field `%s`", field)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) EndContainer() error {
|
|
|
|
if len(rd.containers) == 0 {
|
|
|
|
return errors.New("there is no open containers")
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-06-06 15:14:33 +03:00
|
|
|
rd.containers = rd.containers[:len(rd.containers)-1]
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) ContainerSize() (int, error) {
|
|
|
|
return rd.currentContainer().length, nil
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (rd *msgpackReader) IsContainerAssoc() (bool, error) {
|
|
|
|
return rd.currentContainer().assoc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rd *msgpackReader) Skip() error {
|
|
|
|
return errors.WithStack(rd.dec.Skip())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rd *msgpackReader) TryReadMask() (bool, FieldsMask, error) {
|
|
|
|
code, err := rd.dec.PeekCode()
|
2022-10-01 21:09:54 +03:00
|
|
|
if err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return false, FieldsMask{}, errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
if code != msgpcode.Nil {
|
|
|
|
return false, FieldsMask{}, nil
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
if err := rd.dec.Skip(); err != nil {
|
|
|
|
return false, FieldsMask{}, errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
var mask FieldsMask
|
|
|
|
maskLen, err := rd.dec.DecodeArrayLen()
|
|
|
|
if err != nil {
|
|
|
|
return false, FieldsMask{}, errors.WithStack(err)
|
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
for i := 0; i < maskLen; i++ {
|
|
|
|
maskPart, err := rd.dec.DecodeUint64()
|
|
|
|
if err != nil {
|
|
|
|
return false, FieldsMask{}, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
mask.SetPartFromUint64(i, maskPart)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
return true, mask, nil
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|