msgpack

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Overview

Package msgpack implements MessagePack encoding and decoding.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLongStringOrBinary is the long string or binary error.
	ErrLongStringOrBinary = errors.New("msgpack: long string or binary")

	// ErrIllegalSize is the illegal array or map size error.
	ErrIllegalSize = errors.New("msgpack: illegal array or map size")
)
View Source
var ErrDataSizeTooLarge = errors.New("msgpack: data size too large")

ErrDataSizeTooLarge is the data size too large error.

View Source
var ErrInvalidDecodeArg = errors.New("msgpack: argument to Decode must be non-nil pointer, slice or map")

ErrInvalidDecodeArg is the invalid argument error.

Functions

This section is empty.

Types

type DecodeConvertError

type DecodeConvertError struct {
	// The MessagePack type of the value.
	SrcType Type
	// Option value.
	SrcValue interface{}
	// Type of the Go value that could not be assigned to.
	DestType reflect.Type
}

DecodeConvertError describes a MessagePack value that was not appropriate for a value of a specific Go type.

func (*DecodeConvertError) Error

func (e *DecodeConvertError) Error() string

Error implements the error interface.

type Decoder

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

Decoder reads MessagePack objects from an io.Reader.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder allocates and initializes a new decoder.

func (*Decoder) Bool

func (d *Decoder) Bool() bool

Bool returns the current Bool value.

func (*Decoder) Bytes

func (d *Decoder) Bytes() []byte

Bytes returns the current String, Binary or Extension value as a slice of bytes.

func (*Decoder) BytesNoCopy

func (d *Decoder) BytesNoCopy() []byte

BytesNoCopy returns the current String, Binary or Extension value as a slice of bytes. The underlying array may point to data that will be overwritten by a subsequent call to Unpack.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) (err error)

Decode decodes the next value in the stream to v.

Decode uses the inverse of the encodings that Encoder.Encode uses, allocating maps, slices, and pointers as necessary, with the following additional rules:

To decode into a pointer, Decode first handles the case of a MessagePack nil. In that case, Decode sets the pointer to nil. Otherwise, Decode decodes the stream into the value pointed at by the pointer. If the pointer is nil, Decode allocates a new value for it to point to.

To decode a MessagePack array into a slice, Decode sets the slice length to the length of the MessagePack array or reallocates the slice if there is insufficient capaicity. Slice elments are not cleared before decoding the element.

To decode a MessagePack array into a Go array, Decode decodes the MessagePack array elements into corresponding Go array elements. If the Go array is smaller than the MessagePack array, the additional MessagePack array elements are discarded. If the MessagePack array is smaller than the Go array, the additional Go array elements are set to zero values.

If a MessagePack value is not appropriate for a given target type, or if a MessagePack number overflows the target type, Decode skips that field and completes the decoding as best it can. If no more serious errors are encountered, Decode returns an DecodeConvertError describing the earliest such error.

func (*Decoder) Extension

func (d *Decoder) Extension() int

Extension returns the type of the current Extension value.

func (*Decoder) Float

func (d *Decoder) Float() float64

Float returns the current Float value.

func (*Decoder) Int

func (d *Decoder) Int() int64

Int returns the current Int value.

func (*Decoder) Len

func (d *Decoder) Len() int

Len returns the current ArrayLen or MapLen value.

func (*Decoder) SetExtensions

func (d *Decoder) SetExtensions(extensions ExtensionMap)

SetExtensions specifies functions for converting MessagePack extensions to Go values.

func (*Decoder) Skip

func (d *Decoder) Skip() error

Skip skips over any nested values in the stream.

func (*Decoder) String

func (d *Decoder) String() string

String returns the current String, Binary or Extension value as a string.

func (*Decoder) Type

func (d *Decoder) Type() Type

Type returns the type of the current value in the stream.

func (*Decoder) Uint

func (d *Decoder) Uint() uint64

Uint returns the current Uint value.

func (*Decoder) Unpack

func (d *Decoder) Unpack() error

Unpack reads the next value from the MessagePack stream. Call Type to get the type of the current value. Call Bool, Uint, Int, Float, Bytes or Extension to get the value.

type Encoder

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

Encoder writes values in MessagePack format.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder allocates and initializes a new Unpacker.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) (err error)

Encode writes the MessagePack encoding of v to the stream.

Encode traverses the value v recursively. If an encountered value implements the Marshaler interface Encode calls its MarshalMsgPack method to write the value to the stream.

Otherwise, Encode uses the following type-dependent default encodings:

Go Type             MessagePack Type
bool                true or false
float32, float64    float64
string              string
[]byte              binary
slices, arrays      array
struct, map         map

Struct values encode as maps or arrays. If any struct field tag specifies the "array" option, then the struct is encoded as an array. Otherwise, the struct is encoded as a map. Each exported struct field becomes a member of the map unless

  • the field's tag is "-", or
  • the field is empty and its tag specifies the "omitempty" option.

Anonymous struct fields are marshaled as if their inner exported fields were fields in the outer struct.

The struct field tag "empty" specifies a default value when decoding and the empty value for the "omitempty" option.

Pointer values encode as the value pointed to. A nil pointer encodes as the MessagePack nil value.

Interface values encode as the value contained in the interface. A nil interface value encodes as the MessagePack nil value.

func (*Encoder) PackArrayLen

func (e *Encoder) PackArrayLen(n int64) error

PackArrayLen write an Array length to the MessagePack stream. The application must write n objects to the stream following this call.

func (*Encoder) PackBinary

func (e *Encoder) PackBinary(v []byte) error

PackBinary writes a Binary value to the MessagePack stream.

func (*Encoder) PackBool

func (e *Encoder) PackBool(b bool) error

PackBool writes a Bool value to the MessagePack stream.

func (*Encoder) PackExtension

func (e *Encoder) PackExtension(kind int, data []byte) error

PackExtension writes an extension to the MessagePack stream.

func (*Encoder) PackFloat

func (e *Encoder) PackFloat(f float64) error

PackFloat writes a Float value to the MessagePack stream.

func (*Encoder) PackInt

func (e *Encoder) PackInt(v int64) error

PackInt packs an Int value to the MessagePack stream.

func (*Encoder) PackMapLen

func (e *Encoder) PackMapLen(n int64) error

PackMapLen write an Map length to the MessagePack stream. The application must write n key-value pairs to the stream following this call.

func (*Encoder) PackNil

func (e *Encoder) PackNil() error

PackNil writes a Nil value to the MessagePack stream.

func (*Encoder) PackRaw

func (e *Encoder) PackRaw(p []byte) error

PackRaw writes bytes directly to the MessagePack stream. It is the application's responsibility to ensure that the bytes are valid.

func (*Encoder) PackString

func (e *Encoder) PackString(v string) error

PackString writes a String value to the MessagePack stream.

func (*Encoder) PackStringBytes

func (e *Encoder) PackStringBytes(v []byte) error

PackStringBytes writes a String value to the MessagePack stream.

func (*Encoder) PackUint

func (e *Encoder) PackUint(v uint64) error

PackUint packs a Uint value to the message pack stream.

type ExtensionMap

type ExtensionMap map[int]func([]byte) (interface{}, error)

ExtensionMap specifies functions for converting MessagePack extensions to Go values.

The key is the MessagePack extension type. The value is a function that converts the extension data to a Go value.

type Marshaler

type Marshaler interface {
	MarshalMsgPack(e *Encoder) error
}

Marshaler is the interface implemented by objects that can encode themselves to a MessagePack stream.

type Type

type Type int

Type represents the type of value in the MessagePack stream.

const (
	Invalid Type = iota
	Nil
	Bool
	Int
	Uint
	Float
	ArrayLen
	MapLen
	String
	Binary
	Extension
)

list of MessagePack types.

func (Type) String

func (t Type) String() string

String returns a string representation of the Type.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalMsgPack(d *Decoder) error
}

Unmarshaler is the interface implemented by objects that can decode themselves from a MessagePack stream.

Directories

Path Synopsis
Package rpc implements MessagePack RPC.
Package rpc implements MessagePack RPC.

Jump to

Keyboard shortcuts

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