msgpack

package module
v0.3.1-0...-20c9176 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2020 License: MIT Imports: 10 Imported by: 0

README

github.com/ybkimm/msgpack

godoc MIT License

Another MessagePack encoding/decoding library.

This library doesn't use reflection, instead it relies on small interface: Map, Array, and Extension.

Heavily inspired by GoJay.

Features

Get Started

go get -u github.com/ybkimm/msgpack

Benchmark

Testing PC spec:

  • AMD Ryzen 5 2400G (3.90 GHz, 4 Cores, 8 Threads)
  • 8GB RAM (Samsung DDR4)
  • Windows 10 Pro Build 18362.418
  • WSL
BenchmarkDecoder_decodeMap-8     	 5990668	       194 ns/op	      64 B/op	       1 allocs/op
BenchmarkDecoder_decodeArray-8   	 2336313	       512 ns/op	     320 B/op	       2 allocs/op
BenchmarkEncoder_encodeMap-8     	 3324308	       362 ns/op	     640 B/op	       3 allocs/op
BenchmarkEncoder_encodeArray-8   	 2410296	       492 ns/op	     592 B/op	       2 allocs/op

For benchmark data, see msgpack_test.go.

Todo List

  • Optimize JSON conversion
  • 100% coverage
  • New benchmark data
  • Examples

License

MIT License. See License.

Some code snippets are comes from gojay - See it's license.

Documentation

Index

Constants

View Source
const (
	Nil      = 0xC0
	False    = 0xC2
	True     = 0xC3
	Uint8    = 0xCC
	Uint16   = 0xCD
	Uint32   = 0xCE
	Uint64   = 0xCF
	Int8     = 0xD0
	Int16    = 0xD1
	Int32    = 0xD2
	Int64    = 0xD3
	Float32  = 0xCA
	Float64  = 0xCB
	String8  = 0xD9
	String16 = 0xDA
	String32 = 0xDB
	Binary8  = 0xC4
	Binary16 = 0xC5
	Binary32 = 0xC6
	Array16  = 0xDC
	Array32  = 0xDD
	Map16    = 0xDE
	Map32    = 0xDF
	Fixext1  = 0xD4
	Fixext2  = 0xD5
	Fixext4  = 0xD6
	Fixext8  = 0xD7
	Fixext16 = 0xD8
	Ext8     = 0xC7
	Ext16    = 0xC8
	Ext32    = 0xC9

	TimestampExt = -1
)

Variables

View Source
var (
	ErrTooLongString   = errors.New("msgpack: string is too long")
	ErrTooLongBinary   = errors.New("msgpack: binary is too long")
	ErrTooBigMap       = errors.New("msgpack: map is too big")
	ErrTooBigExtension = errors.New("msgpack: extension is too big")
	ErrUnexpectedEOF   = errors.New("msgpack: unexpected EOF")
	ErrNoReader        = errors.New("msgpack: reader is nil")
	ErrDecodeNil       = errors.New("msgpack: nil value was passed")
	ErrInvalidTime     = errors.New("msgpack: invalid time")
)

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns value v as msgpack format.

func MarshalArray

func MarshalArray(a Array) ([]byte, error)

MarshalArray returns array value as msgpack format.

func MarshalBinary

func MarshalBinary(v []byte) ([]byte, error)

MarshalBinary returns binary value as msgpack format.

func MarshalBool

func MarshalBool(v bool) ([]byte, error)

MarshalBool returns bool value as msgpack format.

func MarshalExtension

func MarshalExtension(ext Extension) ([]byte, error)

MarshalExtension returns extension value as msgpack format.

func MarshalFloat32

func MarshalFloat32(v float32) ([]byte, error)

MarshalFloat32 returns float32 value as msgpack format.

func MarshalFloat64

func MarshalFloat64(v float64) ([]byte, error)

MarshalFloat64 returns float64 value as msgpack format.

func MarshalInt

func MarshalInt(v int) ([]byte, error)

MarshalInt returns int value as msgpack format.

func MarshalInt16

func MarshalInt16(v int16) ([]byte, error)

MarshalInt16 returns int16 value as msgpack format.

func MarshalInt32

func MarshalInt32(v int32) ([]byte, error)

MarshalInt32 returns int32 value as msgpack format.

func MarshalInt64

func MarshalInt64(v int64) ([]byte, error)

MarshalInt64 returns int64 value as msgpack format.

func MarshalInt8

func MarshalInt8(v int8) ([]byte, error)

MarshalInt8 returns int8 value as msgpack format.

func MarshalMap

func MarshalMap(o Map) ([]byte, error)

MarshalMap returns map value as msgpack format.

func MarshalString

func MarshalString(v string) ([]byte, error)

MarshalString returns string value as msgpack format.

func MarshalTime

func MarshalTime(t time.Time) ([]byte, error)

MarshalTime returns time.Time value as msgpack format.

func MarshalUint

func MarshalUint(v uint) ([]byte, error)

MarshalUint returns uint value as msgpack format.

func MarshalUint16

func MarshalUint16(v uint16) ([]byte, error)

MarshalUint16 returns uint16 value as msgpack format.

func MarshalUint32

func MarshalUint32(v uint32) ([]byte, error)

MarshalUint32 returns uint32 value as msgpack format.

func MarshalUint64

func MarshalUint64(v uint64) ([]byte, error)

MarshalUint64 returns uint64 value as msgpack format.

func MarshalUint8

func MarshalUint8(v uint8) ([]byte, error)

MarshalUint8 returns uint64 value as msgpack format.

func Unmarshal

func Unmarshal(data []byte, out interface{}) error

func UnmarshalArray

func UnmarshalArray(data []byte, v Array) error

func UnmarshalBinary

func UnmarshalBinary(data []byte, v *[]byte) error

func UnmarshalBool

func UnmarshalBool(data []byte, v *bool) error

func UnmarshalExtension

func UnmarshalExtension(data []byte, v Extension) error

func UnmarshalFloat32

func UnmarshalFloat32(data []byte, v *float32) error

func UnmarshalFloat64

func UnmarshalFloat64(data []byte, v *float64) error

func UnmarshalInt

func UnmarshalInt(data []byte, v *int) error

func UnmarshalInt16

func UnmarshalInt16(data []byte, v *int16) error

func UnmarshalInt32

func UnmarshalInt32(data []byte, v *int32) error

func UnmarshalInt64

func UnmarshalInt64(data []byte, v *int64) error

func UnmarshalInt8

func UnmarshalInt8(data []byte, v *int8) error

func UnmarshalMap

func UnmarshalMap(data []byte, v Map) error

func UnmarshalNullableArray

func UnmarshalNullableArray(data []byte, v NullableArray) error

func UnmarshalNullableBinary

func UnmarshalNullableBinary(data []byte, v **[]byte) error

func UnmarshalNullableBool

func UnmarshalNullableBool(data []byte, v **bool) error

func UnmarshalNullableExtension

func UnmarshalNullableExtension(data []byte, v NullableExtension) error

func UnmarshalNullableFloat32

func UnmarshalNullableFloat32(data []byte, v **float32) error

func UnmarshalNullableFloat64

func UnmarshalNullableFloat64(data []byte, v **float64) error

func UnmarshalNullableInt

func UnmarshalNullableInt(data []byte, v **int) error

func UnmarshalNullableInt16

func UnmarshalNullableInt16(data []byte, v **int16) error

func UnmarshalNullableInt32

func UnmarshalNullableInt32(data []byte, v **int32) error

func UnmarshalNullableInt64

func UnmarshalNullableInt64(data []byte, v **int64) error

func UnmarshalNullableInt8

func UnmarshalNullableInt8(data []byte, v **int8) error

func UnmarshalNullableMap

func UnmarshalNullableMap(data []byte, v NullableMap) error

func UnmarshalNullableString

func UnmarshalNullableString(data []byte, v **string) error

func UnmarshalNullableTime

func UnmarshalNullableTime(data []byte, v **time.Time) error

func UnmarshalNullableUint

func UnmarshalNullableUint(data []byte, v **uint) error

func UnmarshalNullableUint16

func UnmarshalNullableUint16(data []byte, v **uint16) error

func UnmarshalNullableUint32

func UnmarshalNullableUint32(data []byte, v **uint32) error

func UnmarshalNullableUint64

func UnmarshalNullableUint64(data []byte, v **uint64) error

func UnmarshalNullableUint8

func UnmarshalNullableUint8(data []byte, v **uint8) error

func UnmarshalString

func UnmarshalString(data []byte, v *string) error

func UnmarshalTime

func UnmarshalTime(data []byte, v *time.Time) error

func UnmarshalUint

func UnmarshalUint(data []byte, v *uint) error

func UnmarshalUint16

func UnmarshalUint16(data []byte, v *uint16) error

func UnmarshalUint32

func UnmarshalUint32(data []byte, v *uint32) error

func UnmarshalUint64

func UnmarshalUint64(data []byte, v *uint64) error

func UnmarshalUint8

func UnmarshalUint8(data []byte, v *uint8) error

Types

type Array

type Array interface {
	MarshalMsgpackArray(e *Encoder, i int) error
	UnmarshalMsgpackArray(d *Decoder, l int) error
	Length() uint32
}

type BinaryArray

type BinaryArray [][]byte

func (*BinaryArray) Length

func (a *BinaryArray) Length() uint32

func (*BinaryArray) MarshalMsgpackArray

func (a *BinaryArray) MarshalMsgpackArray(e *Encoder, i int) error

func (*BinaryArray) UnmarshalMsgpackArray

func (a *BinaryArray) UnmarshalMsgpackArray(d *Decoder, l int) error

type BoolArray

type BoolArray []bool

func (*BoolArray) Length

func (a *BoolArray) Length() uint32

func (*BoolArray) MarshalMsgpackArray

func (a *BoolArray) MarshalMsgpackArray(e *Encoder, i int) error

func (*BoolArray) UnmarshalMsgpackArray

func (a *BoolArray) UnmarshalMsgpackArray(d *Decoder, l int) error

type Decoder

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

func NewBytesDecoder

func NewBytesDecoder(data []byte) *Decoder

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns new decoder instance.

func (*Decoder) Decode

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

func (*Decoder) DecodeArray

func (d *Decoder) DecodeArray(v Array) error

func (*Decoder) DecodeBinary

func (d *Decoder) DecodeBinary(v *[]byte) error

func (*Decoder) DecodeBool

func (d *Decoder) DecodeBool(v *bool) error

func (*Decoder) DecodeExtension

func (d *Decoder) DecodeExtension(v Extension) error

func (*Decoder) DecodeFloat32

func (d *Decoder) DecodeFloat32(v *float32) error

func (*Decoder) DecodeFloat64

func (d *Decoder) DecodeFloat64(v *float64) error

func (*Decoder) DecodeInt

func (d *Decoder) DecodeInt(v *int) error

func (*Decoder) DecodeInt16

func (d *Decoder) DecodeInt16(v *int16) error

func (*Decoder) DecodeInt32

func (d *Decoder) DecodeInt32(v *int32) error

func (*Decoder) DecodeInt64

func (d *Decoder) DecodeInt64(v *int64) error

func (*Decoder) DecodeInt8

func (d *Decoder) DecodeInt8(v *int8) error

func (*Decoder) DecodeMap

func (d *Decoder) DecodeMap(v Map) error

func (*Decoder) DecodeNullableArray

func (d *Decoder) DecodeNullableArray(v NullableArray) error

func (*Decoder) DecodeNullableBinary

func (d *Decoder) DecodeNullableBinary(v **[]byte) error

func (*Decoder) DecodeNullableBool

func (d *Decoder) DecodeNullableBool(v **bool) error

func (*Decoder) DecodeNullableExtension

func (d *Decoder) DecodeNullableExtension(v NullableExtension) error

func (*Decoder) DecodeNullableFloat32

func (d *Decoder) DecodeNullableFloat32(v **float32) error

func (*Decoder) DecodeNullableFloat64

func (d *Decoder) DecodeNullableFloat64(v **float64) error

func (*Decoder) DecodeNullableInt

func (d *Decoder) DecodeNullableInt(v **int) error

func (*Decoder) DecodeNullableInt16

func (d *Decoder) DecodeNullableInt16(v **int16) error

func (*Decoder) DecodeNullableInt32

func (d *Decoder) DecodeNullableInt32(v **int32) error

func (*Decoder) DecodeNullableInt64

func (d *Decoder) DecodeNullableInt64(v **int64) error

func (*Decoder) DecodeNullableInt8

func (d *Decoder) DecodeNullableInt8(v **int8) error

func (*Decoder) DecodeNullableMap

func (d *Decoder) DecodeNullableMap(v NullableMap) error

func (*Decoder) DecodeNullableString

func (d *Decoder) DecodeNullableString(v **string) error

func (*Decoder) DecodeNullableTime

func (d *Decoder) DecodeNullableTime(v **time.Time) error

func (*Decoder) DecodeNullableUint

func (d *Decoder) DecodeNullableUint(v **uint) error

func (*Decoder) DecodeNullableUint16

func (d *Decoder) DecodeNullableUint16(v **uint16) error

func (*Decoder) DecodeNullableUint32

func (d *Decoder) DecodeNullableUint32(v **uint32) error

func (*Decoder) DecodeNullableUint64

func (d *Decoder) DecodeNullableUint64(v **uint64) error

func (*Decoder) DecodeNullableUint8

func (d *Decoder) DecodeNullableUint8(v **uint8) error

func (*Decoder) DecodeString

func (d *Decoder) DecodeString(v *string) error

func (*Decoder) DecodeTime

func (d *Decoder) DecodeTime(v *time.Time) error

func (*Decoder) DecodeUint

func (d *Decoder) DecodeUint(v *uint) error

func (*Decoder) DecodeUint16

func (d *Decoder) DecodeUint16(v *uint16) error

func (*Decoder) DecodeUint32

func (d *Decoder) DecodeUint32(v *uint32) error

func (*Decoder) DecodeUint64

func (d *Decoder) DecodeUint64(v *uint64) error

func (*Decoder) DecodeUint8

func (d *Decoder) DecodeUint8(v *uint8) error

type Encoder

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

Encoder writes msgpack formatted variable to writer.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns new encoder instance.

func (*Encoder) Bytes

func (e *Encoder) Bytes() []byte

Bytes returns encoder's current buffer.

func (*Encoder) Error

func (e *Encoder) Error() error

func (*Encoder) PutArray

func (e *Encoder) PutArray(a Array) (err error)

PutArray puts array variable to encoder.

func (*Encoder) PutBinary

func (e *Encoder) PutBinary(v []byte) (err error)

PutBinary puts binary variable to encoder.

func (*Encoder) PutBool

func (e *Encoder) PutBool(v bool) (err error)

PutBool puts bool variable to encoder.

func (*Encoder) PutExtension

func (e *Encoder) PutExtension(ext Extension) (err error)

PutExtension puts extension variable to encoder.

func (*Encoder) PutFloat32

func (e *Encoder) PutFloat32(v float32) (err error)

PutFloat32 puts float32 variable to encoder.

func (*Encoder) PutFloat64

func (e *Encoder) PutFloat64(v float64) (err error)

PutFloat64 puts float64 variable to encoder.

func (*Encoder) PutInt

func (e *Encoder) PutInt(v int) (err error)

PutInt puts int variable to encoder.

func (*Encoder) PutInt16

func (e *Encoder) PutInt16(v int16) (err error)

PutInt16 puts int16 variable to encoder.

func (*Encoder) PutInt32

func (e *Encoder) PutInt32(v int32) (err error)

PutInt32 puts int32 variable to encoder.

func (*Encoder) PutInt64

func (e *Encoder) PutInt64(v int64) (err error)

PutInt64 puts int64 variable to encoder.

func (*Encoder) PutInt8

func (e *Encoder) PutInt8(v int8) (err error)

PutInt8 puts int8 variable to encoder.

func (*Encoder) PutMap

func (e *Encoder) PutMap(o Map) (err error)

PutMap puts map variable to encoder.

func (*Encoder) PutString

func (e *Encoder) PutString(v string) (err error)

PutString puts string variable to encoder.

func (*Encoder) PutTime

func (e *Encoder) PutTime(t time.Time) (err error)

PutTime puts time.Time variable to encoder.

func (*Encoder) PutUint

func (e *Encoder) PutUint(v uint) (err error)

PutUint puts uint variable to encoder.

func (*Encoder) PutUint16

func (e *Encoder) PutUint16(v uint16) (err error)

PutUint16 puts uint16 variable to encoder.

func (*Encoder) PutUint32

func (e *Encoder) PutUint32(v uint32) (err error)

PutUint32 puts uint32 variable to encoder.

func (*Encoder) PutUint64

func (e *Encoder) PutUint64(v uint64) (err error)

PutUint64 puts uint64 variable to encoder.

func (*Encoder) PutUint8

func (e *Encoder) PutUint8(v uint8) (err error)

PutUint8 puts uint8 variable to encoder.

func (*Encoder) Reset

func (e *Encoder) Reset() *Encoder

Reset resets encoder.

type ErrUnexpectedByte

type ErrUnexpectedByte struct {
	Byte     byte
	Position int
	Stack    []byte
}

func (*ErrUnexpectedByte) Error

func (e *ErrUnexpectedByte) Error() string

type ErrUnexpectedExtensionType

type ErrUnexpectedExtensionType struct {
	Type int8
}

func (*ErrUnexpectedExtensionType) Error

type ErrUnexpectedJSONToken

type ErrUnexpectedJSONToken struct {
	Token json.Token
}

func (*ErrUnexpectedJSONToken) Error

func (e *ErrUnexpectedJSONToken) Error() string

type ErrUnknownKey

type ErrUnknownKey struct {
	Key string
}

func (*ErrUnknownKey) Error

func (e *ErrUnknownKey) Error() string

type ErrUnregisteredExtension

type ErrUnregisteredExtension struct {
	Type int8
}

func (*ErrUnregisteredExtension) Error

func (e *ErrUnregisteredExtension) Error() string

type ErrUnsupported

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

func (*ErrUnsupported) Error

func (e *ErrUnsupported) Error() string

type ErrUnsupportedType

type ErrUnsupportedType struct {
	Type reflect.Type
}

func (*ErrUnsupportedType) Error

func (e *ErrUnsupportedType) Error() string

type Extension

type Extension interface {
	ExtensionType() int8
	MarshalMsgpackExtension() []byte
	UnmarshalMsgpackExtension(p []byte) error
}

Extension is an interface for application-specific type.

type Float32Array

type Float32Array []float32

func (*Float32Array) Length

func (a *Float32Array) Length() uint32

func (*Float32Array) MarshalMsgpackArray

func (a *Float32Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Float32Array) UnmarshalMsgpackArray

func (a *Float32Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Float64Array

type Float64Array []float64

func (*Float64Array) Length

func (a *Float64Array) Length() uint32

func (*Float64Array) MarshalMsgpackArray

func (a *Float64Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Float64Array) UnmarshalMsgpackArray

func (a *Float64Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Int16Array

type Int16Array []int16

func (*Int16Array) Length

func (a *Int16Array) Length() uint32

func (*Int16Array) MarshalMsgpackArray

func (a *Int16Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Int16Array) UnmarshalMsgpackArray

func (a *Int16Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Int32Array

type Int32Array []int32

func (*Int32Array) Length

func (a *Int32Array) Length() uint32

func (*Int32Array) MarshalMsgpackArray

func (a *Int32Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Int32Array) UnmarshalMsgpackArray

func (a *Int32Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Int64Array

type Int64Array []int64

func (*Int64Array) Length

func (a *Int64Array) Length() uint32

func (*Int64Array) MarshalMsgpackArray

func (a *Int64Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Int64Array) UnmarshalMsgpackArray

func (a *Int64Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Int8Array

type Int8Array []int8

func (*Int8Array) Length

func (a *Int8Array) Length() uint32

func (*Int8Array) MarshalMsgpackArray

func (a *Int8Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Int8Array) UnmarshalMsgpackArray

func (a *Int8Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type IntArray

type IntArray []int

func (*IntArray) Length

func (a *IntArray) Length() uint32

func (*IntArray) MarshalMsgpackArray

func (a *IntArray) MarshalMsgpackArray(e *Encoder, i int) error

func (*IntArray) UnmarshalMsgpackArray

func (a *IntArray) UnmarshalMsgpackArray(d *Decoder, l int) error

type Map

type Map interface {
	MarshalMsgpackMap(e *Encoder, key string) error
	UnmarshalMsgpackMap(d *Decoder, key string) error
	Fields() []string
}

Map represents key-value pairs of objects

type Nullable

type Nullable interface {
	UnmarshalMsgpackNull() error
}

Nullable represents an object that can be nil.

type NullableArray

type NullableArray interface {
	Nullable
	Array
}

type NullableExtension

type NullableExtension interface {
	Nullable
	Extension
}

NullableExtension is an extension that can be nil.

type NullableMap

type NullableMap interface {
	Nullable
	Map
}

NullableMap is a map that can be nil.

type StringArray

type StringArray []string

func (*StringArray) Length

func (a *StringArray) Length() uint32

func (*StringArray) MarshalMsgpackArray

func (a *StringArray) MarshalMsgpackArray(e *Encoder, i int) error

func (*StringArray) UnmarshalMsgpackArray

func (a *StringArray) UnmarshalMsgpackArray(d *Decoder, l int) error

type TimeArray

type TimeArray []time.Time

func (*TimeArray) Length

func (a *TimeArray) Length() uint32

func (*TimeArray) MarshalMsgpackArray

func (a *TimeArray) MarshalMsgpackArray(e *Encoder, i int) error

func (*TimeArray) UnmarshalMsgpackArray

func (a *TimeArray) UnmarshalMsgpackArray(d *Decoder, l int) error

type Uint16Array

type Uint16Array []uint16

func (*Uint16Array) Length

func (a *Uint16Array) Length() uint32

func (*Uint16Array) MarshalMsgpackArray

func (a *Uint16Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Uint16Array) UnmarshalMsgpackArray

func (a *Uint16Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Uint32Array

type Uint32Array []uint32

func (*Uint32Array) Length

func (a *Uint32Array) Length() uint32

func (*Uint32Array) MarshalMsgpackArray

func (a *Uint32Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Uint32Array) UnmarshalMsgpackArray

func (a *Uint32Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type Uint64Array

type Uint64Array []uint64

func (*Uint64Array) Length

func (a *Uint64Array) Length() uint32

func (*Uint64Array) MarshalMsgpackArray

func (a *Uint64Array) MarshalMsgpackArray(e *Encoder, i int) error

func (*Uint64Array) UnmarshalMsgpackArray

func (a *Uint64Array) UnmarshalMsgpackArray(d *Decoder, l int) error

type UintArray

type UintArray []uint

func (*UintArray) Length

func (a *UintArray) Length() uint32

func (*UintArray) MarshalMsgpackArray

func (a *UintArray) MarshalMsgpackArray(e *Encoder, i int) error

func (*UintArray) UnmarshalMsgpackArray

func (a *UintArray) UnmarshalMsgpackArray(d *Decoder, l int) error

Jump to

Keyboard shortcuts

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