msgp

package module
v0.0.0-...-5c34657 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 14 Imported by: 1

Documentation

Overview

Package msgp is the runtime support library for the msgp code generator (http://github.com/dchenk/msgp).

This package defines the utilities used by the msgp code generator for encoding and decoding MessagePack from []byte and io.Reader/io.Writer types. Most things here are intended to be used only in programs that use the msgp code generator, the point being to avoid runtime reflection.

This package defines four families of functions:

  • AppendXxxx() appends an object to a []byte in MessagePack encoding.
  • ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes.
  • (*Writer).WriteXxxx() writes an object to the buffered *Writer type.
  • (*Reader).ReadXxxx() reads an object from a buffered *Reader type.

Types that implement the msgp.Encoder and msgp.Decoder interfaces can be written and read from any io.Writer and io.Reader types using

msgp.Encode(io.Writer, msgp.Encoder)

and

msgp.Decode(io.Reader, msgp.Decoder)

There are also methods for converting MessagePack to JSON without an explicit de-serialization step.

For more tips and tricks please visit the wiki at http://github.com/dchenk/msgp

Index

Examples

Constants

View Source
const (
	// Complex64Extension represents an extension for complex64 numbers.
	Complex64Extension = 3

	// Complex128Extension represents an extension for complex128 numbers.
	Complex128Extension = 4

	// TimeExtension represents an extension for timestamps. This is not the timestamp format
	// defined in the MessagePack specification.
	TimeExtension = 5
)
View Source
const (
	Int8Size       = 2
	Int16Size      = 3
	Int32Size      = 5
	Int64Size      = 9
	Uint8Size      = 2
	Uint16Size     = 3
	Uint32Size     = 5
	Uint64Size     = 9
	IntSize        = Int64Size
	UintSize       = Uint64Size
	Float64Size    = 9
	Float32Size    = 5
	Complex64Size  = 10
	Complex128Size = 18

	ByteSize = 2
	BoolSize = 1
	NilSize  = 1
	TimeSize = 15

	MapHeaderSize   = 5
	ArrayHeaderSize = 5

	BytesPrefixSize     = 5
	StringPrefixSize    = 5
	ExtensionPrefixSize = 6
)

The sizes here are the worst-case (biggest) encoded sizes for each type, including the prefix with the type information. For variable-length types like slices and strings, the total encoded size is the prefix size plus the length of the actual object.

Variables

View Source
var ErrShortBytes error = errShort{}

ErrShortBytes is returned when the slice being decoded is too short to contain the contents of the message.

View Source
var Nowhere = nwhere{}

Nowhere is an io.Writer to nowhere (used by generated tests).

Functions

func AppendArrayHeader

func AppendArrayHeader(b []byte, size uint32) []byte

AppendArrayHeader appends an array header of the given size to b.

func AppendBool

func AppendBool(b []byte, t bool) []byte

AppendBool appends a bool to b.

func AppendByte

func AppendByte(b []byte, u byte) []byte

AppendByte appends a byte to b as a uint8 using AppendUint8.

func AppendBytes

func AppendBytes(b []byte, data []byte) []byte

AppendBytes appends the data slice to b as MessagePack 'bin' data.

func AppendComplex128

func AppendComplex128(b []byte, c complex128) []byte

AppendComplex128 appends a complex128 to b as a MessagePack extension.

func AppendComplex64

func AppendComplex64(b []byte, c complex64) []byte

AppendComplex64 appends a complex64 to b as a MessagePack extension.

func AppendExtension

func AppendExtension(b []byte, e Extension) ([]byte, error)

AppendExtension appends a MessagePack extension to slice b.

func AppendFloat32

func AppendFloat32(b []byte, f float32) []byte

AppendFloat32 appends a float32 to b.

func AppendFloat64

func AppendFloat64(b []byte, f float64) []byte

AppendFloat64 appends a float64 to b.

func AppendInt

func AppendInt(b []byte, i int) []byte

AppendInt appends an int to b.

func AppendInt16

func AppendInt16(b []byte, i int16) []byte

AppendInt16 appends an int16 to b.

func AppendInt32

func AppendInt32(b []byte, i int32) []byte

AppendInt32 appends an int32 to b.

func AppendInt64

func AppendInt64(b []byte, i int64) []byte

AppendInt64 appends an int64 to b.

func AppendInt8

func AppendInt8(b []byte, i int8) []byte

AppendInt8 appends an int8 to b.

func AppendIntf

func AppendIntf(b []byte, i interface{}) ([]byte, error)

AppendIntf appends to b the value of i with its concrete type. The type of i must be one of the following:

  • bool, float, string, []byte, int, uint, complex, time.Time, or nil
  • map[string]interface{} or map[string]string
  • []T, where T is another supported type
  • *T, where T is another supported type
  • type that implements the msgp.Marshaler interface
  • type that implements the msgp.Extension interface

func AppendMapHeader

func AppendMapHeader(b []byte, size uint32) []byte

AppendMapHeader appends a map header of the given size (number of elements) to b.

func AppendMapStrIntf

func AppendMapStrIntf(b []byte, m map[string]interface{}) ([]byte, error)

AppendMapStrIntf appends a map[string]interface{} to b as a MessagePack map.

func AppendMapStrStr

func AppendMapStrStr(b []byte, m map[string]string) []byte

AppendMapStrStr appends to b a map with 'str'-type keys and values as a MessagePack map.

func AppendNil

func AppendNil(b []byte) []byte

AppendNil appends a MessagePack nil byte to b.

func AppendString

func AppendString(b []byte, s string) []byte

AppendString appends a string as a MessagePack 'str' to b.

func AppendTime

func AppendTime(b []byte, t time.Time) []byte

AppendTime appends a time.Time to b as a MessagePack extension.

func AppendUint

func AppendUint(b []byte, u uint) []byte

AppendUint appends a uint to b.

func AppendUint16

func AppendUint16(b []byte, u uint16) []byte

AppendUint16 appends a uint16 to b.

func AppendUint32

func AppendUint32(b []byte, u uint32) []byte

AppendUint32 appends a uint32 to b.

func AppendUint64

func AppendUint64(b []byte, u uint64) []byte

AppendUint64 appends a uint64 to b.

func AppendUint8

func AppendUint8(b []byte, u uint8) []byte

AppendUint8 appends a uint8 to b.

func CopyReplace

func CopyReplace(key string, raw []byte, val []byte) []byte

CopyReplace works similarly to Replace except that the returned byte slice does not point to the same memory as 'raw'. CopyReplace returns 'nil' if the field doesn't exist or 'raw' isn't a map.

func CopyToJSON

func CopyToJSON(dst io.Writer, src io.Reader) (int64, error)

CopyToJSON reads MessagePack from src and copies it as JSON to dst until EOF.

func Decode

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

Decode decodes d from r.

Example
package main

import (
	"fmt"
	"os"

	"github.com/dchenk/msgp/msgp"
)

func main() {
	// Read a message from a file:
	file, err := os.Open("my-message")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	var theMessage msgp.Number
	err = msgp.Decode(file, &theMessage)
	if err != nil {
		panic(err)
	}
	fmt.Print(theMessage.Int())
}
Output:

func Encode

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

Encode encodes an Encoder to any io.Writer.

Example
package main

import (
	"os"

	"github.com/dchenk/msgp/msgp"
)

func main() {
	// Write out a message to a file:
	file, err := os.Create("my-message")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	var theMessage msgp.Number
	theMessage.AsInt(457)
	err = msgp.Encode(file, &theMessage)
	if err != nil {
		panic(err)
	}
}
Output:

func GuessSize

func GuessSize(i interface{}) int

GuessSize guesses the size of the underlying value of 'i'. If the underlying value is not a simple builtin (or []byte), GuessSize defaults to 512.

func HasKey

func HasKey(key string, raw []byte) bool

HasKey returns whether the map in 'raw' has a field with the key.

func IsNil

func IsNil(b []byte) bool

IsNil returns true if len(b)>0 and the leading byte is a "nil" MessagePack byte (0xc0).

func Locate

func Locate(key string, raw []byte) []byte

Locate returns a []byte pointing to the field in a MessagePack map with the provided key. (The returned []byte points to a sub-slice of 'raw'; Locate does no allocations.) If the key doesn't exist in the map, a zero-length []byte will be returned.

func ReadArrayHeaderBytes

func ReadArrayHeaderBytes(b []byte) (uint32, []byte, error)

ReadArrayHeaderBytes reads the array header size off of b and returns the array length and any remaining bytes. Possible errors are ErrShortBytes, TypeError, and InvalidPrefixError.

func ReadBoolBytes

func ReadBoolBytes(b []byte) (bool, []byte, error)

ReadBoolBytes tries to read a float64 from b and return the value and the remaining bytes. Possible errors are ErrShortBytes and TypeError.

func ReadByteBytes

func ReadByteBytes(b []byte) (byte, []byte, error)

ReadByteBytes is analogous to ReadUint8Bytes

func ReadBytesBytes

func ReadBytesBytes(b []byte, scratch []byte) ([]byte, []byte, error)

ReadBytesBytes reads a 'bin' object from b and returns its value and any remaining bytes. The data is copied to the scratch slice if it's big enough, otherwise a slice is allocated. Possible errors are ErrShortBytes and TypeError.

func ReadBytesZC

func ReadBytesZC(b []byte) ([]byte, []byte, error)

ReadBytesZC extracts a 'bin' object from b without copying. The first slice returned points to the same memory as the input slice, and the second slice is any remaining bytes. Possible errors are ErrShortBytes and TypeError.

func ReadComplex128Bytes

func ReadComplex128Bytes(b []byte) (c complex128, o []byte, err error)

ReadComplex128Bytes reads a complex128 extension object from 'b' and returns any remaining bytes. Possible errors are ErrShortBytes, TypeError, InvalidPrefixError, and ExtensionTypeError.

func ReadComplex64Bytes

func ReadComplex64Bytes(b []byte) (c complex64, o []byte, err error)

ReadComplex64Bytes reads a complex64 extension object from b and returns any remaining bytes. Possible errors include ErrShortBytes (not enough bytes in slice b), TypeError{} (object not a complex64), and ExtensionTypeError{} (object an extension of the correct size, but not a complex64)

func ReadExactBytes

func ReadExactBytes(b []byte, dst []byte) ([]byte, error)

ReadExactBytes reads into dst the bytes expected with the next object in b.

func ReadExtensionBytes

func ReadExtensionBytes(b []byte, e Extension) ([]byte, error)

ReadExtensionBytes reads an extension from b into e and returns any remaining bytes. Possible errors: - ErrShortBytes ('b' not long enough) - ExtensionTypeError{} (wire type not the same as e.Type()) - TypeError{} (next object not an extension) - InvalidPrefixError - An unmarshal error returned from e.UnmarshalBinary

func ReadFloat32Bytes

func ReadFloat32Bytes(b []byte) (float32, []byte, error)

ReadFloat32Bytes reads a float32 from b and returns the value and any remaining bytes. Possible errors are ErrShortBytes and TypeError.

func ReadFloat64Bytes

func ReadFloat64Bytes(b []byte) (float64, []byte, error)

ReadFloat64Bytes reads a float64 from b and returns the value and any remaining bytes. Possible errors are ErrShortBytes and TypeError.

func ReadInt16Bytes

func ReadInt16Bytes(b []byte) (int16, []byte, error)

ReadInt16Bytes reads an int16 from b and returns the value and any remaining bytes. Possible errors are ErrShortBytes, TypeError, and IntOverflow.

func ReadInt32Bytes

func ReadInt32Bytes(b []byte) (int32, []byte, error)

ReadInt32Bytes reads an int32 from b and returns the value and any remaining bytes. Possible errors include ErrShortBytes (too few bytes in b), TypeError{} (not an int), and IntOverflow{} (value doesn't fit in an int32).

func ReadInt64Bytes

func ReadInt64Bytes(b []byte) (int64, []byte, error)

ReadInt64Bytes reads an int64 from b and return the value and the remaining bytes. Errors that can be returned are ErrShortBytes, UintOverflow, InvalidPrefixError, and TypeError.

func ReadInt8Bytes

func ReadInt8Bytes(b []byte) (int8, []byte, error)

ReadInt8Bytes tries to read an int16 from b and return the value and the remaining bytes. Possible errors are ErrShortBytes, TypeError, and IntOverflow.

func ReadIntBytes

func ReadIntBytes(b []byte) (int, []byte, error)

ReadIntBytes tries to read an int from b and return the value and the remaining bytes. Possible errors are ErrShortBytes, TypeError, and IntOverflow (32-bit platforms only).

func ReadIntfBytes

func ReadIntfBytes(b []byte) (interface{}, []byte, error)

ReadIntfBytes reads the next object out of b as a raw interface{} and returns any remaining bytes.

func ReadMapHeaderBytes

func ReadMapHeaderBytes(b []byte) (uint32, []byte, error)

ReadMapHeaderBytes reads a map header size from b and returns the remaining bytes. Possible errors are ErrShortBytes and TypeError.

func ReadMapKeyZC

func ReadMapKeyZC(b []byte) ([]byte, []byte, error)

ReadMapKeyZC reads a 'str' or 'bin' object (a key to a map element) from b and returns the value and any remaining bytes. Possible errors are ErrShortBytes and TypeError.

func ReadMapStrIntfBytes

func ReadMapStrIntfBytes(b []byte, old map[string]interface{}) (map[string]interface{}, []byte, error)

ReadMapStrIntfBytes reads a map[string]interface{} out of b and returns the map and any remaining bytes. If map old is not nil, it will be cleared and used so that a map does not need to be created.

func ReadNilBytes

func ReadNilBytes(b []byte) ([]byte, error)

ReadNilBytes reads a "nil" byte off of b and returns any remaining bytes. Possible errors are ErrShortBytes, TypeError, and InvalidPrefixError.

func ReadStringAsBytes

func ReadStringAsBytes(b []byte, scratch []byte) ([]byte, []byte, error)

ReadStringAsBytes reads a 'str' object into a slice of bytes. The data read is the first slice returned, which may be written to the memory held by the scratch slice if it is large enough (scratch may be nil). The second slice returned contains the remaining bytes in b. Possible errors are ErrShortBytes and TypeError.

func ReadStringBytes

func ReadStringBytes(b []byte) (string, []byte, error)

ReadStringBytes reads a 'str' object from b and returns its value and any remaining bytes in b. Possible errors are ErrShortBytes and TypeError.

func ReadStringZC

func ReadStringZC(b []byte) ([]byte, []byte, error)

ReadStringZC reads a MessagePack string field without copying. The returned []byte points to the same memory as the input slice. Possible errors are ErrShortBytes and TypeError.

func ReadTimeBytes

func ReadTimeBytes(b []byte) (time.Time, []byte, error)

ReadTimeBytes reads a time.Time extension object from b and returns any remaining bytes. Possible errors include ErrShortBytes (not enough bytes in b), TypeError{} (object not a time), and ExtensionTypeError{} (object an extension of the correct size, but not a time.Time).

func ReadUint16Bytes

func ReadUint16Bytes(b []byte) (uint16, []byte, error)

ReadUint16Bytes tries to read a uint16 from b and return the value and the remaining bytes. Possible errors are ErrShortBytes, TypeError, and UintOverflow.

func ReadUint32Bytes

func ReadUint32Bytes(b []byte) (uint32, []byte, error)

ReadUint32Bytes tries to read a uint32 from b and return the value and the remaining bytes. Possible errors are ErrShortBytes, TypeError, and UintOverflow.

func ReadUint64Bytes

func ReadUint64Bytes(b []byte) (uint64, []byte, error)

ReadUint64Bytes reads a uint64 from b and returns the value and any remaining bytes. Possible errors include ErrShortBytes and TypeError.

func ReadUint8Bytes

func ReadUint8Bytes(b []byte) (uint8, []byte, error)

ReadUint8Bytes tries to read a uint8 from b and return the value and the remaining bytes. Possible errors are ErrShortBytes, TypeError, and UintOverflow.

func ReadUintBytes

func ReadUintBytes(b []byte) (uint, []byte, error)

ReadUintBytes tries to read a uint from b and return the value and the remaining bytes. Possible errors are ErrShortBytes, TypeError, and UintOverflow (32-bit platforms only).

func RegisterExtension

func RegisterExtension(typ int8, f func() Extension)

RegisterExtension registers extensions so that they can be initialized and returned by methods that decode `interface{}` values. This should only be called during initialization. Func f should return a newly-initialized zero value of the extension. Keep in mind that extensions 3, 4, and 5 are reserved for complex64, complex128, and time.Time, respectively, and that MessagePack reserves extension types from -127 to -1.

For example, if you wanted to register a user-defined struct:

msgp.RegisterExtension(10, func() msgp.Extension { &MyExtension{} })

RegisterExtension will panic if you call it multiple times with the same 'typ' argument or if you use a reserved type (3, 4, or 5).

func Remove

func Remove(key string, raw []byte) []byte

Remove removes a key-value pair from 'raw'. It returns 'raw' unchanged if the key didn't exist.

func Replace

func Replace(key string, raw []byte, val []byte) []byte

Replace takes a key ("key") in a MessagePack map ("raw") and replaces its value with the one provided and returns the new []byte. The returned []byte may point to the same memory as "raw". Replace makes no effort to evaluate the validity of the contents of 'val'. It may use up to the full capacity of 'raw.' Replace returns 'nil' if the field doesn't exist or if the object in 'raw' is not a map.

func Require

func Require(old []byte, extra int) []byte

Require returns a slice (with the contents of old) having a capacity to fit at least extra number of bytes after the current length. The length of the returned slice is the same as the length of the old slice.

func Skip

func Skip(b []byte) ([]byte, error)

Skip skips the next object in slice b and returns the remaining bytes. If the object is a map or array, all of its elements will be skipped. Possible errors are ErrShortBytes (not enough bytes in b) and InvalidPrefixError (bad encoding).

func UnmarshalAsJSON

func UnmarshalAsJSON(w io.Writer, msg []byte) ([]byte, error)

UnmarshalAsJSON takes raw MessagePack data and writes it as JSON to w. If an error is returned, the bytes not unmarshalled will also be returned. If no errors are encountered, the length of the returned slice will be zero.

Types

type ArrayError

type ArrayError struct {
	Wanted, Got uint32
}

An ArrayError error is returned when decoding a fix-sized array of the wrong size.

func (ArrayError) Error

func (a ArrayError) Error() string

Error implements the error interface.

func (ArrayError) Resumable

func (a ArrayError) Resumable() bool

Resumable is always true for ArrayErrors.

type Decoder

type Decoder interface {
	DecodeMsg(*Reader) error
}

Decoder is the interface implemented by objects that know how to read themselves from a *Reader.

type Encoder

type Encoder interface {
	EncodeMsg(*Writer) error
}

Encoder is the interface implemented by types that know how to write themselves as MessagePack using a *msgp.Writer.

type EndlessReader

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

EndlessReader is an io.Reader that loops over the same data endlessly. It is used for benchmarking.

func NewEndlessReader

func NewEndlessReader(b []byte, tb timer) *EndlessReader

NewEndlessReader returns a new *EndlessReader.

func (*EndlessReader) Read

func (c *EndlessReader) Read(p []byte) (int, error)

Read implements io.Reader. In practice, it always returns (len(p), nil) though it fills the supplied slice while the benchmark timer is stopped.

type ErrUnsupportedType

type ErrUnsupportedType struct {
	T reflect.Type
}

ErrUnsupportedType is returned when a bad argument is supplied to a function that takes `interface{}`.

func (*ErrUnsupportedType) Error

func (e *ErrUnsupportedType) Error() string

Error implements the error interface.

func (*ErrUnsupportedType) Resumable

func (e *ErrUnsupportedType) Resumable() bool

Resumable returns true for ErrUnsupportedType.

type Error

type Error interface {
	error
	// Resumable returns whether or not the error means that the stream
	// of data is malformed and the information is unrecoverable.
	Resumable() bool
}

Error is the interface satisfied by all of the errors that originate from this package.

type Extension

type Extension interface {
	// ExtensionType returns an int8 that identifies the extension's concrete type.
	// (Types below zero are reserved by the MessagePack specification.)
	ExtensionType() int8

	// Len returns the length of the data to be encoded.
	Len() int

	// MarshalBinaryTo copies the data into the supplied slice, assuming that the
	// slice has length Len().
	MarshalBinaryTo([]byte) error

	// UnmarshalBinary unmarshals the slice into the object.
	UnmarshalBinary([]byte) error
}

Extension is the interface implemented by types that want to define their own binary encoding.

type ExtensionTypeError

type ExtensionTypeError struct {
	Got, Want int8
}

ExtensionTypeError is an error type returned when there is a mis-match between an extension type and the type encoded on the wire.

func (ExtensionTypeError) Error

func (e ExtensionTypeError) Error() string

Error implements the error interface.

func (ExtensionTypeError) Resumable

func (e ExtensionTypeError) Resumable() bool

Resumable returns true for ExtensionTypeError errors.

type IntOverflow

type IntOverflow struct {
	Value         int64 // the value of the integer
	FailedBitsize int   // the bit size that the int64 could not fit into
}

An IntOverflow error is returned when an operation would downcast an integer to a type with too few bits to hold its value.

func (IntOverflow) Error

func (i IntOverflow) Error() string

Error implements the error interface.

func (IntOverflow) Resumable

func (i IntOverflow) Resumable() bool

Resumable is always true for overflows.

type InvalidPrefixError

type InvalidPrefixError byte

InvalidPrefixError is returned when a bad encoding uses a prefix that is not recognized in the MessagePack standard. This kind of error is unrecoverable.

func (InvalidPrefixError) Error

func (i InvalidPrefixError) Error() string

Error implements the error interface.

func (InvalidPrefixError) Resumable

func (i InvalidPrefixError) Resumable() bool

Resumable returns false for InvalidPrefixErrors.

type MarshalSizer

type MarshalSizer interface {
	Marshaler
	Sizer
}

MarshalSizer combines the Marshaler and Sizer interfaces.

type Marshaler

type Marshaler interface {
	MarshalMsg([]byte) ([]byte, error)
}

Marshaler is the interface implemented by types that know how to marshal themselves as MessagePack. MarshalMsg appends the marshalled form of the object to the provided byte slice, returning the extended slice and any errors encountered.

type Number

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

A Number can be an int64, uint64, float32, or float64 internally. It can decode itself from any of the native MessagePack number types. The zero-value of Number is Int(0).

func (*Number) AsFloat32

func (n *Number) AsFloat32(f float32)

AsFloat32 sets the number to a float32.

func (*Number) AsFloat64

func (n *Number) AsFloat64(f float64)

AsFloat64 sets the number to a float64.

func (*Number) AsInt

func (n *Number) AsInt(i int64)

AsInt sets the number to an int64.

func (*Number) AsUint

func (n *Number) AsUint(u uint64)

AsUint sets the number to a uint64.

func (*Number) DecodeMsg

func (n *Number) DecodeMsg(r *Reader) error

DecodeMsg implements msgp.Decoder.

func (*Number) EncodeMsg

func (n *Number) EncodeMsg(w *Writer) error

EncodeMsg implements msgp.Encoder.

func (*Number) Float

func (n *Number) Float() (float64, bool)

Float returns the number converted to a float64 and says whether or not the underlying type was either a float64 or a float32.

func (*Number) Int

func (n *Number) Int() (int64, bool)

Int returns the number converted to a int64 and says whether or not the underlying type was int64.

func (*Number) MarshalJSON

func (n *Number) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Number) MarshalMsg

func (n *Number) MarshalMsg(b []byte) ([]byte, error)

MarshalMsg implements msgp.Marshaler.

func (*Number) Msgsize

func (n *Number) Msgsize() int

Msgsize implements msgp.Sizer.

func (*Number) String

func (n *Number) String() string

String implements fmt.Stringer.

func (*Number) Type

func (n *Number) Type() Type

Type returns the number's type as one of Float64Type, Float32Type, UintType, or IntType.

func (*Number) Uint

func (n *Number) Uint() (uint64, bool)

Uint returns the number converted to a uint64 and says whether or not the underlying type was uint64.

func (*Number) UnmarshalMsg

func (n *Number) UnmarshalMsg(b []byte) ([]byte, error)

UnmarshalMsg implements msgp.Unmarshaler.

type Raw

type Raw []byte

Raw is raw encoded MessagePack. It implements Marshaler, Unmarshaler, Encoder, Decoder, and Sizer. Raw allows you to read and write data without interpreting the contents.

func (*Raw) DecodeMsg

func (r *Raw) DecodeMsg(f *Reader) error

DecodeMsg implements msgp.Decoder. It sets the value of r to be the next object on the wire.

func (Raw) EncodeMsg

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

EncodeMsg implements msgp.Encoder. It writes the raw bytes to the writer. If r is empty, then "nil" (0xc0) is written instead.

func (*Raw) MarshalJSON

func (r *Raw) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Raw) MarshalMsg

func (r Raw) MarshalMsg(b []byte) ([]byte, error)

MarshalMsg implements msgp.Marshaler. It appends the raw contents of r to the provided byte slice. If r is empty, then "nil" (0xc0) is appended instead.

func (Raw) Msgsize

func (r Raw) Msgsize() int

Msgsize implements msgp.Sizer

func (*Raw) UnmarshalMsg

func (r *Raw) UnmarshalMsg(b []byte) ([]byte, error)

UnmarshalMsg implements msgp.Unmarshaler. It sets the contents of r to be the next object in the provided byte slice.

type RawExtension

type RawExtension struct {
	Data []byte
	Type int8
}

RawExtension implements the Extension interface.

func (*RawExtension) ExtensionType

func (r *RawExtension) ExtensionType() int8

ExtensionType implements Extension.ExtensionType, and returns r.Type

func (*RawExtension) Len

func (r *RawExtension) Len() int

Len implements Extension.Len.

func (*RawExtension) MarshalBinaryTo

func (r *RawExtension) MarshalBinaryTo(d []byte) error

MarshalBinaryTo implements Extension.MarshalBinaryTo and returns a copy of r.Data.

func (*RawExtension) UnmarshalBinary

func (r *RawExtension) UnmarshalBinary(b []byte) error

UnmarshalBinary implements Extension.UnmarshalBinary and sets r.Data to the contents of the provided slice.

type Reader

type Reader struct {
	// R is the buffered reader used to decode MessagePack. Don't use it directly.
	R *fwd.Reader
	// contains filtered or unexported fields
}

Reader wraps an io.Reader and provides methods to read MessagePack-encoded values from it. Readers are buffered.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a *Reader that reads from the provided reader. The reader will be buffered.

func NewReaderSize

func NewReaderSize(r io.Reader, sz int) *Reader

NewReaderSize returns a *Reader with a buffer of the given size. (This is vastly preferable to passing the decoder a reader that is already buffered.)

func (*Reader) BufferSize

func (m *Reader) BufferSize() int

BufferSize returns the capacity of the read buffer.

func (*Reader) Buffered

func (m *Reader) Buffered() int

Buffered returns the number of bytes currently in the read buffer.

func (*Reader) CopyNext

func (m *Reader) CopyNext(w io.Writer) (int64, error)

CopyNext reads the next object from m without decoding it and writes it to w. It avoids unnecessary copies internally.

func (*Reader) IsNil

func (m *Reader) IsNil() bool

IsNil says whether or not the next byte is a nil MessagePack byte (0xc0).

func (*Reader) NextType

func (m *Reader) NextType() (Type, error)

NextType returns the next object type to be decoded.

func (*Reader) Read

func (m *Reader) Read(p []byte) (int, error)

Read implements io.Reader.

func (*Reader) ReadArrayHeader

func (m *Reader) ReadArrayHeader() (uint32, error)

ReadArrayHeader reads the next object as an array header and returns the size of the array.

func (*Reader) ReadBool

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

ReadBool reads a bool from the reader.

func (*Reader) ReadByte

func (m *Reader) ReadByte() (byte, error)

ReadByte is analogous to ReadUint8. This is *not* an implementation of io.ByteReader.

func (*Reader) ReadBytes

func (m *Reader) ReadBytes(scratch []byte) ([]byte, error)

ReadBytes reads a MessagePack 'bin' object from the reader and returns its value. The scratch slice will be used for storage if it is not nil and large enough.

func (*Reader) ReadBytesHeader

func (m *Reader) ReadBytesHeader() (uint32, error)

ReadBytesHeader reads the size header of a MessagePack 'bin' object. The user is responsible for dealing with the given number of bytes from the reader in an application-specific way.

func (*Reader) ReadComplex128

func (m *Reader) ReadComplex128() (complex128, error)

ReadComplex128 reads a complex128 from the reader.

func (*Reader) ReadComplex64

func (m *Reader) ReadComplex64() (complex64, error)

ReadComplex64 reads a complex64 from the reader.

func (*Reader) ReadExactBytes

func (m *Reader) ReadExactBytes(into []byte) error

ReadExactBytes reads a MessagePack 'bin'-encoded object off of the wire into the provided slice. An ArrayError will be returned if the object is not exactly the length of the input slice.

func (*Reader) ReadExtension

func (m *Reader) ReadExtension(e Extension) error

ReadExtension reads the next object from the reader as an extension. ReadExtension will fail if the next object in the stream is not an extension or if e.Type() is not the same as the wire type.

func (*Reader) ReadFloat32

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

ReadFloat32 reads a float32 from the reader.

func (*Reader) ReadFloat64

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

ReadFloat64 reads a float64 from the reader. If the value on the wire is encoded as a float32, it will be converted to a float64 without losing precision.

func (*Reader) ReadFull

func (m *Reader) ReadFull(p []byte) (int, error)

ReadFull implements io.ReadFull.

func (*Reader) ReadInt

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

ReadInt reads an int from the reader.

func (*Reader) ReadInt16

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

ReadInt16 reads an int16 from the reader.

func (*Reader) ReadInt32

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

ReadInt32 reads an int32 from the reader.

func (*Reader) ReadInt64

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

ReadInt64 reads an int64 from the reader. If an int64 is not available, this function tries to read an unsigned integer and convert it to an int64 if possible. Errors that can be returned include UintOverflow and TypeError.

func (*Reader) ReadInt8

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

ReadInt8 reads an int8 from the reader.

func (*Reader) ReadIntf

func (m *Reader) ReadIntf() (interface{}, error)

ReadIntf reads out the next object as a raw interface{}. Arrays are decoded as []interface{}, and maps are decoded as map[string]interface{}. Integers are decoded as int64, and unsigned integers are decoded as uint64.

func (*Reader) ReadMapHeader

func (m *Reader) ReadMapHeader() (uint32, error)

ReadMapHeader reads the next object as a map header and returns the size of the map. A TypeError{} is returned if the next object is not a map.

func (*Reader) ReadMapKey

func (m *Reader) ReadMapKey(scratch []byte) ([]byte, error)

ReadMapKey reads a 'str' or 'bin' object (a key to a map element) from the reader and returns the value as a []byte. It uses scratch for storage if it is large enough.

func (*Reader) ReadMapKeyPtr

func (m *Reader) ReadMapKeyPtr() ([]byte, error)

ReadMapKeyPtr returns a []byte pointing to the contents of a valid map key. The key cannot be empty, and it must be shorter than the total buffer size of the *Reader. The returned slice is only valid until the next *Reader method call. Be extremely careful when using this method; writing into the returned slice may corrupt future reads.

func (*Reader) ReadMapStrIntf

func (m *Reader) ReadMapStrIntf(mp map[string]interface{}) error

ReadMapStrIntf reads a MessagePack map into a map[string]interface{}. You must pass a non-nil map into the function.

func (*Reader) ReadNil

func (m *Reader) ReadNil() error

ReadNil reads a 'nil' MessagePack byte from the reader.

func (*Reader) ReadString

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

ReadString reads a UTF-8 string from the reader.

func (*Reader) ReadStringAsBytes

func (m *Reader) ReadStringAsBytes(scratch []byte) ([]byte, error)

ReadStringAsBytes reads a MessagePack 'str' (UTF-8) string and returns its value as bytes. The scratch slice will be used for storage if it is not nil and large enough.

func (*Reader) ReadStringHeader

func (m *Reader) ReadStringHeader() (sz uint32, err error)

ReadStringHeader reads a string header off of the wire. The user is then responsible for dealing with the next sz bytes from the reader in an application-specific manner.

func (*Reader) ReadTime

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

ReadTime reads a time.Time object from the reader. The returned time's location will be set to time.Local.

func (*Reader) ReadUint

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

ReadUint reads a uint from the reader.

func (*Reader) ReadUint16

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

ReadUint16 reads a uint16 from the reader.

func (*Reader) ReadUint32

func (m *Reader) ReadUint32() (u uint32, err error)

ReadUint32 reads a uint32 from the reader.

func (*Reader) ReadUint64

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

ReadUint64 reads a uint64 from the reader.

func (*Reader) ReadUint8

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

ReadUint8 reads a uint8 from the reader.

func (*Reader) Reset

func (m *Reader) Reset(r io.Reader)

Reset resets the underlying reader.

func (*Reader) Skip

func (m *Reader) Skip() error

Skip skips over the next object, regardless of its type. If it is an array or map, the whole array or map will be skipped.

func (*Reader) WriteToJSON

func (r *Reader) WriteToJSON(w io.Writer) (n int64, err error)

WriteToJSON translates MessagePack from r and writes it as JSON to w until the underlying reader returns io.EOF. WriteToJSON returns the number of bytes written. An error is returned only if reading stops before io.EOF.

type Sizer

type Sizer interface {
	Msgsize() int
}

Sizer is an interface implemented by types that can estimate their size when encoded to MessagePack. This interface is optional, but encoding/marshaling implementations may use this as a way to pre-allocate memory for serialization.

type Type

type Type byte

A Type is a MessagePack wire type, including this package's built-in extension types.

const (
	// InvalidType is the zero value of Type (not used).
	InvalidType Type = iota

	// StrType and the following types are built into the MessagePack standard.
	StrType
	BinType
	MapType
	ArrayType
	Float64Type
	Float32Type
	BoolType
	IntType
	UintType
	NilType
	ExtensionType

	// Complex64Type and the following types represent extensions.
	Complex64Type
	Complex128Type
	TimeType
)

func NextType

func NextType(b []byte) Type

NextType returns the type of the next object in the slice. If the length of the input is zero, it returns InvalidType.

func (Type) String

func (t Type) String() string

String implements fmt.Stringer

type TypeError

type TypeError struct {
	Method  Type // Type expected by method
	Encoded Type // Type actually encoded
}

A TypeError is returned when a particular decoding method is unsuitable for decoding a particular MessagePack value.

func (TypeError) Error

func (t TypeError) Error() string

Error implements the error interface.

func (TypeError) Resumable

func (t TypeError) Resumable() bool

Resumable returns true for TypeError errors.

type UintOverflow

type UintOverflow struct {
	Value         uint64 // value of the uint
	FailedBitsize int    // the bit size that couldn't fit the value
}

A UintOverflow error is returned when an operation would downcast an unsigned integer to a type with too few bits to hold its value.

func (UintOverflow) Error

func (u UintOverflow) Error() string

Error implements the error interface.

func (UintOverflow) Resumable

func (u UintOverflow) Resumable() bool

Resumable is always true for overflows.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalMsg([]byte) ([]byte, error)
}

Unmarshaler is the interface implemented by objects that know how to unmarshal themselves from MessagePack. UnmarshalMsg unmarshals the object from binary, returning any leftover bytes and any errors encountered.

type Writer

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

Writer is a buffered writer that can be used to write MessagePack objects to an io.Writer. You must call *Writer.Flush() to flush all of the buffered data to the underlying writer.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer.

func NewWriterSize

func NewWriterSize(w io.Writer, sz int) *Writer

NewWriterSize creates a Writer with a custom buffer size.

func (*Writer) Append

func (mw *Writer) Append(bts ...byte) error

Append can be used to append a few (no more than the total buffer length) single bytes to the buffer.

func (*Writer) Flush

func (mw *Writer) Flush() error

Flush flushes all of the buffered data to the underlying writer.

func (*Writer) OpenSpace

func (mw *Writer) OpenSpace() int

OpenSpace returns the number of bytes currently free for writing to the write buffer.

func (*Writer) Reset

func (mw *Writer) Reset(w io.Writer)

Reset resets the underlying buffer used by the Writer.

func (*Writer) Write

func (mw *Writer) Write(p []byte) (int, error)

Write implements io.Writer to write directly to the buffer.

func (*Writer) WriteArrayHeader

func (mw *Writer) WriteArrayHeader(sz uint32) error

WriteArrayHeader writes an array header of the given size to the buffer.

func (*Writer) WriteBool

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

WriteBool writes a bool to the writer.

func (*Writer) WriteByte

func (mw *Writer) WriteByte(u byte) error

WriteByte does the same thing as WriteUint8.

func (*Writer) WriteBytes

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

WriteBytes writes binary data as 'bin' to the writer.

func (*Writer) WriteBytesHeader

func (mw *Writer) WriteBytesHeader(sz uint32) error

WriteBytesHeader writes just the size header df a MessagePack 'bin' object. The user is responsible for then writing sz more bytes into the stream.

func (*Writer) WriteComplex128

func (mw *Writer) WriteComplex128(f complex128) error

WriteComplex128 writes a complex128 to the writer

func (*Writer) WriteComplex64

func (mw *Writer) WriteComplex64(f complex64) error

WriteComplex64 writes a complex64 to the writer.

func (*Writer) WriteExtension

func (mw *Writer) WriteExtension(e Extension) error

WriteExtension writes an extension type to the writer.

func (*Writer) WriteFloat32

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

WriteFloat32 writes a float32 to the writer

func (*Writer) WriteFloat64

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

WriteFloat64 writes a float64 to the writer

func (*Writer) WriteInt

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

WriteInt writes an int to the writer.

func (*Writer) WriteInt16

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

WriteInt16 writes an int16 to the writer.

func (*Writer) WriteInt32

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

WriteInt32 writes an int32 to the writer.

func (*Writer) WriteInt64

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

WriteInt64 writes an int64 to the writer.

func (*Writer) WriteInt8

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

WriteInt8 writes an int8 to the writer.

func (*Writer) WriteIntf

func (mw *Writer) WriteIntf(v interface{}) error

WriteIntf writes the concrete type of v. The type of v must be one of the following:

  • bool, float, string, []byte, int, uint, complex, time.Time, or nil
  • map of supported types, with string keys
  • array or slice of supported types
  • pointer to a supported type
  • type that implements the msgp.Encoder interface
  • type that implements the msgp.Extension interface

func (*Writer) WriteMapHeader

func (mw *Writer) WriteMapHeader(sz uint32) error

WriteMapHeader writes a map header of the given size to the buffer.

func (*Writer) WriteMapStrIntf

func (mw *Writer) WriteMapStrIntf(mp map[string]interface{}) (err error)

WriteMapStrIntf writes a map[string]interface to the writer

func (*Writer) WriteMapStrStr

func (mw *Writer) WriteMapStrStr(mp map[string]string) (err error)

WriteMapStrStr writes a map[string]string to the writer

func (*Writer) WriteNil

func (mw *Writer) WriteNil() error

WriteNil writes a nil byte to the buffer.

func (*Writer) WriteString

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

WriteString writes a MessagePack string to the writer. (This is NOT an implementation of io.StringWriter)

func (*Writer) WriteStringFromBytes

func (mw *Writer) WriteStringFromBytes(str []byte) error

WriteStringFromBytes writes a 'str' object from a []byte representing a string.

func (*Writer) WriteStringHeader

func (mw *Writer) WriteStringHeader(sz uint32) error

WriteStringHeader writes just the string size header of a MessagePack 'str' object. The user is responsible for writing sz more valid UTF-8 bytes to the stream.

func (*Writer) WriteTime

func (mw *Writer) WriteTime(t time.Time) error

WriteTime writes a time.Time object to the wire.

Time is encoded as Unix time, which means that location (time zone) data is removed from the object. The encoded object itself is 12 bytes: 8 bytes for a big-endian 64-bit integer denoting seconds elapsed since "zero" Unix time, followed by 4 bytes for a big-endian 32-bit signed integer denoting the nanosecond offset of the time. This encoding is intended to ease portability across languages.

func (*Writer) WriteUint

func (mw *Writer) WriteUint(u uint) error

WriteUint writes a uint to the writer.

func (*Writer) WriteUint16

func (mw *Writer) WriteUint16(u uint16) error

WriteUint16 writes a uint16 to the writer.

func (*Writer) WriteUint32

func (mw *Writer) WriteUint32(u uint32) error

WriteUint32 writes a uint32 to the writer.

func (*Writer) WriteUint64

func (mw *Writer) WriteUint64(u uint64) error

WriteUint64 writes a uint64 to the writer.

func (*Writer) WriteUint8

func (mw *Writer) WriteUint8(u uint8) error

WriteUint8 writes a uint8 to the writer.

Jump to

Keyboard shortcuts

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