msgpack

package module
v0.0.0-...-782817a Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 10 Imported by: 5

README

msgpack-go

msgpack-go is a MessagePack implementation for the Go programming language.

Encoding

To encode values into the binary MessagePack format, an Encode function is provided:

func Encode(w io.Writer, v Encoder) error

Encoder describes an encodable type which fulfills the interface

type Encoder interface {
	EncodeMsgpack(w *Writer) error
}

and writes its value to the given writer. The Writer type provides a Write* method for each supported data type. For a complete overview of the Writer type, see the documentation.

Decoding

To decode binary MessagePack data into values, a Decode function is provided:

func Decode(r io.Reader, v Decoder) error

Decoder describes a decodable type which fulfills the interface

type Decoder interface {
	DecodeMsgpack(r *Reader) error
}

and reads the necessary values from the given reader. The Reader type provides a Read* method for each supported data type. Furthermore, a reader provides the following extra methods:

  • Peek for looking up the next type in the stream without moving the read pointer, and
  • Skip for skipping any value which comes next in the stream.

For a complete overview of the Reader type, see the documentation.

Example

package main

import (
	"bytes"
	"log"

	msgpack "github.com/mprot/msgpack-go"
)

type Custom struct {
	Number  int
	Boolean bool
}

func (c *Custom) EncodeMsgpack(w *msgpack.Writer) error {
	if err := w.WriteInt(c.Number); err != nil {
		return err
	}
	return w.WriteBool(c.Boolean)
}

func (c *Custom) DecodeMsgpack(r *msgpack.Reader) (err error) {
	if c.Number, err = r.ReadInt(); err != nil {
		return err
	}
	c.Boolean, err = r.ReadBool()
	return err
}

func main() {
	var buf bytes.Buffer

	encoded := &Custom{Number: 7, Boolean: true}
	if err := msgpack.Encode(&buf, encoded); err != nil {
		log.Fatal(err)
	}

	decoded := &Custom{}
	if err := msgpack.Decode(&buf, decoded); err != nil {
		log.Fatal(err)
	}

	if decoded.Number != encoded.Number || decoded.Boolean != encoded.Boolean {
		log.Fatal("something went wrong")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendMarshal

func AppendMarshal(v Encoder, buf []byte) ([]byte, error)

AppendMarshal encodes v into the MessagePack encoding and appends it to buf.

func CopyToJSON

func CopyToJSON(w io.Writer, r io.Reader) (written int, err error)

CopyToJSON is a helper function for reading MessagePack encoded data from r, transforming it to JSON, and writing it to w.

func Decode

func Decode(r io.Reader, v Decoder) error

Decode decodes the MessagePack encoding provided by r into v.

func Encode

func Encode(w io.Writer, v Encoder) error

Encode encodes v into the MessagePack encoding and writes it to w.

func Marshal

func Marshal(v Encoder) ([]byte, error)

Marshal encodes v into the MessagePack encoding and returns its encoding.

func Unmarshal

func Unmarshal(p []byte, v Decoder) error

Unmarshal unmarshals the MessagePack encoding provided by data into v.

Types

type Decoder

type Decoder interface {
	DecodeMsgpack(r *Reader) error
}

Decoder defines an interface for types which are able to decode themselves from an MessagePack encoding.

type Encoder

type Encoder interface {
	EncodeMsgpack(w *Writer) error
}

Encoder defines an interface for types which are able to encode themselves into an MessagePack encoding.

type Raw

type Raw []byte

Raw is a raw encoded MessagePack value. It can be used to delay MessagePack decoding.

func (*Raw) DecodeMsgpack

func (raw *Raw) DecodeMsgpack(r *Reader) error

DecodeMsgpack reads the raw MessagePack value from r.

func (Raw) EncodeMsgpack

func (raw Raw) EncodeMsgpack(w *Writer) error

EncodeMsgpack writes the raw MessagePack value into w.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader defines a reader for MessagePack encoded data.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader creates a reader for MessagePack encoded data read from r.

func NewReaderBytes

func NewReaderBytes(p []byte) *Reader

NewReaderBytes creates a reader for MessagePack encoded data. The reader reads all data from p.

func (*Reader) Peek

func (r *Reader) Peek() (Type, error)

Peek returns the type for the next element without moving the read pointer.

func (*Reader) ReadArrayHeader

func (r *Reader) ReadArrayHeader() (int, error)

ReadArrayHeader reads the header of an array value from the MessagePack stream and returns the number of elements.

func (*Reader) ReadArrayHeaderWithSize

func (r *Reader) ReadArrayHeaderWithSize(size int) error

ReadArrayHeaderWithSize reads the header of an array value from the MessagePack stream and compares it to the given size. If the size of the array does not equal the given size, an error will be returned.

func (*Reader) ReadBool

func (r *Reader) ReadBool() (bool, error)

ReadBool reads a boolean value from the MessagePack stream.

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(p []byte) ([]byte, error)

ReadBytes reads a binary value from the MessagePack stream. The data will be copied to p if it fits. Otherwise a new slice will be allocated.

func (*Reader) ReadBytesNoCopy

func (r *Reader) ReadBytesNoCopy() ([]byte, error)

ReadBytesNoCopy reads a binary value from the MessagePack stream. The data is directly passed from the underlying buffer. This could result in a buffer reallocation.

func (*Reader) ReadExt

func (r *Reader) ReadExt(typ int8, v encoding.BinaryUnmarshaler) error

ReadExt reads an extension value from the MessagePack stream. The given extension type must match the extension type found in the stream.

func (*Reader) ReadFloat32

func (r *Reader) ReadFloat32() (float32, error)

ReadFloat32 reads a 32-bit floating-point value from the MessagePack stream.

func (*Reader) ReadFloat64

func (r *Reader) ReadFloat64() (float64, error)

ReadFloat64 reads a 64-bit floating-point value from the MessagePack stream.

func (*Reader) ReadInt

func (r *Reader) ReadInt() (int, error)

ReadInt reads an integer value from the MessagePack stream.

func (*Reader) ReadInt16

func (r *Reader) ReadInt16() (int16, error)

ReadInt16 reads a 16-bit integer value from the MessagePack stream.

func (*Reader) ReadInt32

func (r *Reader) ReadInt32() (int32, error)

ReadInt32 reads a 32-bit integer value from the MessagePack stream.

func (*Reader) ReadInt64

func (r *Reader) ReadInt64() (int64, error)

ReadInt64 reads a 64-bit integer value from the MessagePack stream.

func (*Reader) ReadInt8

func (r *Reader) ReadInt8() (int8, error)

ReadInt8 reads an 8-bit integer value from the MessagePack stream.

func (*Reader) ReadMapHeader

func (r *Reader) ReadMapHeader() (int, error)

ReadMapHeader reads the header of an map value from the MessagePack stream and returns the number of elements.

func (*Reader) ReadNil

func (r *Reader) ReadNil() error

ReadNil reads a nil value from the MessagePack stream.

func (*Reader) ReadRaw

func (r *Reader) ReadRaw(raw Raw) (Raw, error)

ReadRaw reads the next value from the MessagePack stream into raw.

func (*Reader) ReadString

func (r *Reader) ReadString() (string, error)

ReadString reads a string value from the MessagePack stream.

func (*Reader) ReadTime

func (r *Reader) ReadTime() (time.Time, error)

ReadTime reads a time value from the MessagePack stream.

func (*Reader) ReadUint

func (r *Reader) ReadUint() (uint, error)

ReadUint reads an unsigned integer value from the MessagePack stream.

func (*Reader) ReadUint16

func (r *Reader) ReadUint16() (uint16, error)

ReadUint16 reads a 16-bit unsigned integer value from the MessagePack stream.

func (*Reader) ReadUint32

func (r *Reader) ReadUint32() (uint32, error)

ReadUint32 reads a 32-bit unsigned integer value from the MessagePack stream.

func (*Reader) ReadUint64

func (r *Reader) ReadUint64() (uint64, error)

ReadUint64 reads a 64-bit unsigned integer value from the MessagePack stream.

func (*Reader) ReadUint8

func (r *Reader) ReadUint8() (uint8, error)

ReadUint8 reads an 8-bit unsigned integer value from the MessagePack stream.

func (*Reader) Skip

func (r *Reader) Skip() error

Skip skips the next value in the MessagePack stream.

type Type

type Type string

Type represents an MessagePack data type.

const (
	Nil    Type = "nil"
	Bool   Type = "bool"
	Int    Type = "int"
	Uint   Type = "uint"
	Float  Type = "float"
	String Type = "string"
	Bytes  Type = "bytes"
	Array  Type = "array"
	Map    Type = "map"
	Ext    Type = "ext"

	// extension types
	Time Type = "time"
)

All supported MessagePack types.

type TypeError

type TypeError struct {
	Actual   Type
	Expected Type
}

TypeError specifies an error type for unexpected wire types.

func (TypeError) Error

func (e TypeError) Error() string

Error returns the error message of the error.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer defines a writer for MessagePack encoded data.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a writer for MessagePack encoded data which writes to w.

func (*Writer) WriteArrayHeader

func (w *Writer) WriteArrayHeader(length int) error

WriteArrayHeader writes the header of an array value to the MessagePack stream.

func (*Writer) WriteBool

func (w *Writer) WriteBool(b bool) error

WriteBool writes a boolean value to the MessagePack stream.

func (*Writer) WriteBytes

func (w *Writer) WriteBytes(b []byte) error

WriteBytes writes a binary value to the MessagePack stream.

func (*Writer) WriteExt

func (w *Writer) WriteExt(typ int8, v encoding.BinaryMarshaler) error

WriteExt writes an extension value to the MessagePack stream.

func (*Writer) WriteFloat32

func (w *Writer) WriteFloat32(f float32) error

WriteFloat32 writes a 32-bit floating-point value to the MessagePack stream.

func (*Writer) WriteFloat64

func (w *Writer) WriteFloat64(f float64) error

WriteFloat64 writes a 64-bit floating-point value to the MessagePack stream.

func (*Writer) WriteInt

func (w *Writer) WriteInt(i int) error

WriteInt writes an integer value to the MessagePack stream.

func (*Writer) WriteInt16

func (w *Writer) WriteInt16(i int16) error

WriteInt16 writes a 16-bit integer value to the MessagePack stream.

func (*Writer) WriteInt32

func (w *Writer) WriteInt32(i int32) error

WriteInt32 writes a 32-bit integer value to the MessagePack stream.

func (*Writer) WriteInt64

func (w *Writer) WriteInt64(i int64) error

WriteInt64 writes a 64-bit integer value to the MessagePack stream.

func (*Writer) WriteInt8

func (w *Writer) WriteInt8(i int8) error

WriteInt8 writes an 8-bit integer value to the MessagePack stream.

func (*Writer) WriteMapHeader

func (w *Writer) WriteMapHeader(length int) error

WriteMapHeader writes the header of an map value to the MessagePack stream.

func (*Writer) WriteNil

func (w *Writer) WriteNil() error

WriteNil writes a nil value to the MessagePack stream.

func (*Writer) WriteRaw

func (w *Writer) WriteRaw(r Raw) error

WriteRaw writes raw bytes to the MessagePack stream, which represent an already encoded section.

func (*Writer) WriteString

func (w *Writer) WriteString(s string) error

WriteString writes a string value to the MessagePack stream.

func (*Writer) WriteTime

func (w *Writer) WriteTime(tm time.Time) error

WriteTime writes a time value to the MessagePack stream.

func (*Writer) WriteUint

func (w *Writer) WriteUint(i uint) error

WriteUint writes an unsigned integer value to the MessagePack stream.

func (*Writer) WriteUint16

func (w *Writer) WriteUint16(i uint16) error

WriteUint16 writes a 16-bit unsigned integer value to the MessagePack stream.

func (*Writer) WriteUint32

func (w *Writer) WriteUint32(i uint32) error

WriteUint32 writes a 32-bit unsigned integer value to the MessagePack stream.

func (*Writer) WriteUint64

func (w *Writer) WriteUint64(i uint64) error

WriteUint64 writes a 64-bit unsigned integer value to the MessagePack stream.

func (*Writer) WriteUint8

func (w *Writer) WriteUint8(i uint8) error

WriteUint8 writes an 8-bit unsigned integer value to the MessagePack stream.

Jump to

Keyboard shortcuts

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