serde

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2022 License: Apache-2.0 Imports: 6 Imported by: 4

Documentation

Overview

This library provides utilities for length-prefixed de/serialization for messages with user-specific serialization format.

Index

Constants

This section is empty.

Variables

View Source
var ErrMsgTooBig = fmt.Errorf("serde: message exceeds max allowed size")

ErrMsgTooBig is signaled in case user tries to de/serialize message with size bigger than MaxMessageSize.

View Source
var MaxMessageSize uint64 = 1 << 20

TODO: 1mb is reasonable, but it still makes sense to make it configurable per use-case, rather than globally MaxMessageSize limits allowed size for messages that can de/serialized.

Functions

func Marshal

func Marshal(msg Message, buf []byte) (n int, err error)

Marshal serializes the given Message into the given buffer with prefixed-length.

func Read

func Read(r io.Reader, msg Message) (n int, err error)

Read deserializes the given msg from the given Reader. It efficiently reuses allocated buffer with buffer pooling.

func Unmarshal

func Unmarshal(msg Message, data []byte) (n int, err error)

Unmarshal deserializes the given Message from the given data buffer. The data buffer should be filled using Marshal.

func Write

func Write(w io.Writer, msg Message) (n int, err error)

Write marshals the given msg and writes it to the given Writer. It efficiently reuses allocated buffer with buffer pooling.

Types

type ByteReader

type ByteReader struct {
	io.Reader
	// contains filtered or unexported fields
}

func NewByteReader

func NewByteReader(r io.Reader) *ByteReader

func (*ByteReader) ReadByte

func (b *ByteReader) ReadByte() (byte, error)

type Message

type Message interface {
	// Size reports the size of a Message.
	Size() int

	// MarshalTo serializes a Message to the given slice.
	// It also reports amount of bytes written.
	MarshalTo([]byte) (int, error)

	// Unmarshal deserialized a Message from a given slice.
	Unmarshal([]byte) error
}

Message defines an abstract Message to be de/serialized using arbitrary format. NOTE: Currently, Message is aligned and tested for protobuf(gogo/proto) support in the first place.

type PlainMessage

type PlainMessage struct {
	Data []byte
}

PlainMessage is the simplest implementation for Message which only copies bytes as a serialization.

func (*PlainMessage) Equals

func (f *PlainMessage) Equals(to *PlainMessage) bool

func (*PlainMessage) MarshalTo

func (f *PlainMessage) MarshalTo(buf []byte) (int, error)

func (*PlainMessage) Size

func (f *PlainMessage) Size() int

func (*PlainMessage) Unmarshal

func (f *PlainMessage) Unmarshal(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL