meta/msgpack_writer.go

168 lines
4.0 KiB
Go
Raw Permalink Normal View History

2022-10-01 21:09:54 +03:00
package meta
import (
"io"
"github.com/pkg/errors"
"github.com/vmihailenco/msgpack/v5"
2022-10-01 21:09:54 +03:00
)
type msgpackWriter struct {
enc *msgpack.Encoder
2023-06-16 13:52:56 +03:00
containers []writeContainer
}
type writeContainer struct {
length int
assoc bool
2022-10-01 21:09:54 +03:00
}
func NewMsgpackWriter(w io.Writer) Writer {
return &msgpackWriter{
enc: msgpack.NewEncoder(w),
2023-06-16 13:52:56 +03:00
containers: make([]writeContainer, 0, 1),
}
2022-10-01 21:09:54 +03:00
}
2023-06-16 13:52:56 +03:00
func (wr *msgpackWriter) currentContainer() writeContainer {
if wr == nil || len(wr.containers) == 0 {
2023-06-16 13:52:56 +03:00
return writeContainer{}
}
return wr.containers[len(wr.containers)-1]
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) writeFieldName(field string) error {
if !wr.currentContainer().assoc {
return nil
}
2022-10-01 21:09:54 +03:00
return errors.WithStack(wr.enc.EncodeString(field))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteInt8(v int8, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeInt(int64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteInt16(v int16, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeInt(int64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteInt32(v int32, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeInt(int64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteInt64(v int64, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeInt(v))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteUint8(v uint8, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteUint16(v uint16, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteUint32(v uint32, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteUint64(v uint64, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeUint(uint64(v)))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteBool(v bool, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeBool(v))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteFloat32(v float32, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeFloat32(v))
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) WriteFloat64(v float64, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
2022-10-01 21:09:54 +03:00
}
return errors.WithStack(wr.enc.EncodeFloat64(v))
}
2022-10-01 21:09:54 +03:00
func (wr *msgpackWriter) WriteString(v string, field string) error {
if err := wr.writeFieldName(field); err != nil {
2022-10-01 21:09:54 +03:00
return err
}
return errors.WithStack(wr.enc.EncodeString(v))
}
2022-10-01 21:09:54 +03:00
func (wr *msgpackWriter) WriteBytes(v []byte, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
return errors.WithStack(wr.enc.EncodeBytes(v))
}
2022-10-01 21:09:54 +03:00
func (wr *msgpackWriter) BeginContainer(length int, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
}
if err := wr.enc.EncodeArrayLen(length); err != nil {
return errors.WithStack(err)
}
2023-06-16 13:52:56 +03:00
wr.containers = append(wr.containers, writeContainer{
length: length,
assoc: false,
})
2022-10-01 21:09:54 +03:00
return nil
}
func (wr *msgpackWriter) BeginAssocContainer(length int, field string) error {
if err := wr.writeFieldName(field); err != nil {
return err
2022-10-01 21:09:54 +03:00
}
if err := wr.enc.EncodeMapLen(length); err != nil {
return errors.WithStack(err)
2022-10-01 21:09:54 +03:00
}
2023-06-16 13:52:56 +03:00
wr.containers = append(wr.containers, writeContainer{
length: length,
assoc: true,
})
return nil
2022-10-01 21:09:54 +03:00
}
func (wr *msgpackWriter) EndContainer() error {
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
}