2022-10-01 21:09:54 +03:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-06-06 15:14:33 +03:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2022-10-01 21:09:54 +03:00
|
|
|
)
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
type msgpackWriter struct {
|
|
|
|
enc *msgpack.Encoder
|
2023-07-03 22:09:39 +03:00
|
|
|
containers []struct{}
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func NewMsgpackWriter(w io.Writer) Writer {
|
|
|
|
return &msgpackWriter{
|
|
|
|
enc: msgpack.NewEncoder(w),
|
2023-07-03 22:09:39 +03:00
|
|
|
containers: make([]struct{}, 0, 1),
|
2023-06-06 15:14:33 +03:00
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteInt8(v int8, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeInt(int64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteInt16(v int16, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeInt(int64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteInt32(v int32, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeInt(int64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteInt64(v int64, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeInt(v))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteUint8(v uint8, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteUint16(v uint16, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteUint32(v uint32, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteUint64(v uint64, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteBool(v bool, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeBool(v))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteFloat32(v float32, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeFloat32(v))
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteFloat64(v float64, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeFloat64(v))
|
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteString(v string, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeString(v))
|
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) WriteBytes(v []byte, field string) error {
|
|
|
|
return errors.WithStack(wr.enc.EncodeBytes(v))
|
|
|
|
}
|
2022-10-01 21:09:54 +03:00
|
|
|
|
2023-06-06 15:14:33 +03:00
|
|
|
func (wr *msgpackWriter) BeginContainer(length int, field string) error {
|
2023-07-03 22:09:39 +03:00
|
|
|
return wr.BeginCollection(length, field)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 22:09:39 +03:00
|
|
|
func (wr *msgpackWriter) EndContainer() error {
|
|
|
|
return wr.EndCollection()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wr *msgpackWriter) BeginCollection(length int, field string) error {
|
|
|
|
if err := wr.enc.EncodeArrayLen(length); err != nil {
|
2023-06-06 15:14:33 +03:00
|
|
|
return errors.WithStack(err)
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
2023-07-03 22:09:39 +03:00
|
|
|
wr.containers = append(wr.containers, struct{}{})
|
2023-06-06 15:14:33 +03:00
|
|
|
return nil
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 22:09:39 +03:00
|
|
|
func (wr *msgpackWriter) EndCollection() error {
|
2023-06-06 15:14:33 +03:00
|
|
|
if len(wr.containers) == 0 {
|
|
|
|
return errors.New("there is no open containers")
|
|
|
|
}
|
|
|
|
wr.containers = wr.containers[:len(wr.containers)-1]
|
|
|
|
return nil
|
2022-10-01 21:09:54 +03:00
|
|
|
}
|