msgpack

package
v0.0.0-...-e82649b Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MIT Imports: 13 Imported by: 1

README

msgpack

A msgpack serializer and deserializer

Build Status

GoDoc

SYNOPSIS

package msgpack_test

import (
  "fmt"
  "time"

  msgpack "github.com/lestrrat-go/msgpack"
  "github.com/pkg/errors"
)

type EventTime struct {
  time.Time
}

func init() {
  if err := msgpack.RegisterExt(0, EventTime{}); err != nil {
    panic(err)
  }
}

func (t *EventTime) DecodeMsgpack(d *msgpack.Decoder) error {
  r := d.Reader()
  sec, err := r.ReadUint32()
  if err != nil {
    return errors.Wrap(err, `failed to read uint32 from first 4 bytes`)
  }

  nsec, err := r.ReadUint32()
  if err != nil {
    return errors.Wrap(err, `failed to read uint32 from second 4 bytes`)
  }

  t.Time = time.Unix(int64(sec), int64(nsec)).UTC()
  return nil
}

func (t EventTime) EncodeMsgpack(e *msgpack.Encoder) error {
  w := e.Writer()
  if err := w.WriteUint32(uint32(t.Unix())); err != nil {
    return errors.Wrap(err, `failed to write EventTime seconds payload`)
  }

  if err := w.WriteUint32(uint32(t.Nanosecond())); err != nil {
    return errors.Wrap(err, `failed to write EventTime nanoseconds payload`)
  }

  return nil
}

type FluentdMessage struct {
  Tag    string
  Time   EventTime
  Record map[string]interface{}
  Option map[string]interface{}
}

func (m FluentdMessage) EncodeMsgpack(e *msgpack.Encoder) error {
  if err := e.EncodeArrayHeader(4); err != nil {
    return errors.Wrap(err, `failed to encode array header`)
  }
  if err := e.EncodeString(m.Tag); err != nil {
    return errors.Wrap(err, `failed to encode tag`)
  }
  if err := e.EncodeStruct(m.Time); err != nil {
    return errors.Wrap(err, `failed to encode time`)
  }
  if err := e.EncodeMap(m.Record); err != nil {
    return errors.Wrap(err, `failed to encode record`)
  }
  if err := e.EncodeMap(m.Option); err != nil {
    return errors.Wrap(err, `failed to encode option`)
  }
  return nil
}

func (m *FluentdMessage) DecodeMsgpack(e *msgpack.Decoder) error {
  var l int
  if err := e.DecodeArrayLength(&l); err != nil {
    return errors.Wrap(err, `failed to decode msgpack array length`)
  }

  if l != 4 {
    return errors.Errorf(`invalid array length %d (expected 4)`, l)
  }

  if err := e.DecodeString(&m.Tag); err != nil {
    return errors.Wrap(err, `failed to decode fluentd message tag`)
  }

  if err := e.DecodeStruct(&m.Time); err != nil {
    return errors.Wrap(err, `failed to decode fluentd time`)
  }

  if err := e.DecodeMap(&m.Record); err != nil {
    return errors.Wrap(err, `failed to decode fluentd record`)
  }

  if err := e.DecodeMap(&m.Option); err != nil {
    return errors.Wrap(err, `failed to decode fluentd option`)
  }

  return nil
}

func ExampleFluentdMessage() {
  var f1 = FluentdMessage{
    Tag:  "foo",
    Time: EventTime{Time: time.Unix(1234567890, 123).UTC()},
    Record: map[string]interface{}{
      "count": 1000,
    },
  }

  b, err := msgpack.Marshal(f1)
  if err != nil {
    fmt.Printf("%s\n", err)
    return
  }

  var f2 FluentdMessage
  if err := msgpack.Unmarshal(b, &f2); err != nil {
    fmt.Printf("%s\n", err)
    return
  }

  fmt.Printf("%s %s %v %v\n", f2.Tag, f2.Time, f2.Record, f2.Option)
  // OUTPUT:
  // foo 2009-02-13 23:31:30.000000123 +0000 UTC map[count:1000] map[]
}

func ExampleEventTime() {
  var e1 = EventTime{Time: time.Unix(1234567890, 123).UTC()}

  b, err := msgpack.Marshal(e1)
  if err != nil {
    fmt.Printf("%s\n", err)
    return
  }

  var e2 interface{}
  if err := msgpack.Unmarshal(b, &e2); err != nil {
    fmt.Printf("%s\n", err)
    return
  }
  // OUTPUT:
}

STATUS

  • Requires more testing for array/map/struct types

DESCRIPTION

While tinkering with low-level msgpack stuff for the first time, I realized that I didn't know enough about its internal workings to make suggestions of have confidence producing bug reports, and I really should: So I wrote one for my own amusement and education.

FEATURES

API Compatibility With stdlib

github.com/vmihailenco/msgpack.v2, which this library was initially based upon, has subtle differences with the stdlib. For example, "github.com/vmihailenco/msgpack.v2".Decoder.Decode() has a signature of Decode(v ...interface{}), which doesn't match with the signature in, for example, encoding/json. This subtle difference makes it hard to use interfaces to make swappable serializers.

Also, all decoding API takes an argument to be assigned to instead of returning a value.

Custom Serialization

If you would like to customize serialization for a particular type, you can create a type that implements the msgpack.EncodeMsgpacker and/or msgpack.DecodeMsgpacker interface.

func (v *Object) EncodeMsgpack(e *msgpack.Encoder) error {
  ...
}

func (v *Object) DecodeMsgpack(d *msgpack.Decoder) error {
  ...
}

Low Level Writer/Reader

In some rare cases, such as when you are creating extensions, you need a more fine grained control on what you read or write. For this, you may use the msgpack.Writer and msgpack.Reader objects.

These objects know how to read or write bytes of data in the correct byte order.

Struct Tags

Struct tags are supported via the msgpack keyword. The syntax follows that of encoding/json package:

type Example struct {
    Foo struct `msgpack:"foo"`
    Bar struct `msgpack:"bar,omitempty"`
}

For convenience for those migrating from github.com/tinylib/msgpack, we also support the "msg" struct tag.

PROS/CONS

PROS

As most late comers are, I believe the project is a little bit cleaner than my predecessors, which possibly could mean a slightly easier experience for the users to hack and tweak it. I know, it's very subjective.

As far as comparisons against gopkg.in/vmihailenco/msgpack.v2 goes, this library tries to keep the API as compatible as possible to the standard library's encoding/* packages. For example, encoding/json allows:

  b, _ := json.Marshal(true)

  // using uninitialized empty interface
  var v interface{}
  json.Unmarshal(b, &v)

But if you do the same with gopkg.in/vmihailenco/msgpack.v2, this throws a panic:

  b, _ := msgpack.Marshal(true)

  // using uninitialized empty interface
  var v interface{}
  msgpack.Unmarshal(b, &v)

This library follows the semantics for encoding/json, and you can safely pass an uninitialized empty inteface to Unmarsha/Decode

CONS

As previously described, I have been learning by implementing this library. I intend to work on it until I'm satisfied, but unless you are the type of person who likes to live on the bleeding edge, you probably want to use another library.

BENCHMARKS

Current status

$ go test -tags bench -v -run=none -benchmem -bench .
BenchmarkEncodeFloat32/___lestrrat/float32_via_Encode()-4                     30000000        51.0 ns/op         4 B/op         1 allocs/op
BenchmarkEncodeFloat32/___lestrrat/float32_via_EncodeFloat32()-4             100000000        22.4 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeFloat32/vmihailenco/float32_via_Encode()-4                     20000000        55.8 ns/op         4 B/op         1 allocs/op
BenchmarkEncodeFloat32/vmihailenco/float32_via_EncodeFloat32()-4             100000000        23.9 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeFloat64/___lestrrat/float64_via_Encode()-4                     30000000        57.7 ns/op         8 B/op         1 allocs/op
BenchmarkEncodeFloat64/___lestrrat/float64_via_EncodeFloat64()-4             100000000        25.6 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeFloat64/vmihailenco/float64_via_Encode()-4                     20000000        60.4 ns/op         8 B/op         1 allocs/op
BenchmarkEncodeFloat64/vmihailenco/float64_via_EncodeFloat64()-4              50000000        25.7 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint8/___lestrrat/uint8_via_Encode()-4                         30000000        43.8 ns/op         1 B/op         1 allocs/op
BenchmarkEncodeUint8/___lestrrat/uint8_via_EncodeUint8()-4                   100000000        22.7 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint8/vmihailenco/uint8_via_Encode()-4                         10000000         178 ns/op         1 B/op         1 allocs/op
BenchmarkEncodeUint8/vmihailenco/uint8_via_EncodeUint8()-4                    50000000        26.9 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint16/___lestrrat/uint16_via_Encode()-4                       20000000        54.0 ns/op         2 B/op         1 allocs/op
BenchmarkEncodeUint16/___lestrrat/uint16_via_EncodeUint16()-4                100000000        24.6 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint16/vmihailenco/uint16_via_Encode()-4                        5000000         210 ns/op         2 B/op         1 allocs/op
BenchmarkEncodeUint16/vmihailenco/uint16_via_EncodeUint16()-4                 50000000        27.1 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint32/___lestrrat/uint32_via_Encode()-4                       20000000        56.2 ns/op         4 B/op         1 allocs/op
BenchmarkEncodeUint32/___lestrrat/uint32_via_EncodeUint32()-4                 50000000        23.5 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint32/vmihailenco/uint32_via_Encode()-4                       10000000         203 ns/op         4 B/op         1 allocs/op
BenchmarkEncodeUint32/vmihailenco/uint32_via_EncodeUint32()-4                 50000000        49.7 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint64/___lestrrat/uint64_via_Encode()-4                       20000000        75.2 ns/op         8 B/op         1 allocs/op
BenchmarkEncodeUint64/___lestrrat/uint64_via_EncodeUint64()-4                 50000000        29.9 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeUint64/vmihailenco/uint64_via_Encode()-4                       20000000        82.9 ns/op         8 B/op         1 allocs/op
BenchmarkEncodeUint64/vmihailenco/uint64_via_EncodeUint64()-4                 50000000        40.8 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt8/___lestrrat/int8_via_Encode()-4                           20000000        61.3 ns/op         1 B/op         1 allocs/op
BenchmarkEncodeInt8/___lestrrat/int8_via_EncodeInt8()-4                       30000000        37.8 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt8/vmihailenco/int8_via_Encode()-4                            5000000         277 ns/op         2 B/op         2 allocs/op
BenchmarkEncodeInt8/vmihailenco/int8_via_EncodeInt8()-4                       20000000        52.3 ns/op         1 B/op         1 allocs/op
BenchmarkEncodeInt8FixNum/___lestrrat/int8_via_Encode()-4                     20000000        63.2 ns/op         1 B/op         1 allocs/op
BenchmarkEncodeInt8FixNum/___lestrrat/int8_via_EncodeInt8()-4                 50000000        26.7 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt8FixNum/vmihailenco/int8_via_Encode()-4                      5000000         210 ns/op         2 B/op         2 allocs/op
BenchmarkEncodeInt8FixNum/vmihailenco/int8_via_EncodeInt8()-4                 50000000        35.1 ns/op         1 B/op         1 allocs/op
BenchmarkEncodeInt16/___lestrrat/int16_via_Encode()-4                         30000000        50.4 ns/op         2 B/op         1 allocs/op
BenchmarkEncodeInt16/___lestrrat/int16_via_EncodeInt16()-4                   100000000        23.4 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt16/vmihailenco/int16_via_Encode()-4                         10000000         186 ns/op         2 B/op         1 allocs/op
BenchmarkEncodeInt16/vmihailenco/int16_via_EncodeInt16()-4                    50000000        44.7 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt32/___lestrrat/int32_via_Encode()-4                         20000000        67.5 ns/op         4 B/op         1 allocs/op
BenchmarkEncodeInt32/___lestrrat/int32_via_EncodeInt32()-4                    50000000        33.0 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt32/vmihailenco/int32_via_Encode()-4                          5000000         301 ns/op         4 B/op         1 allocs/op
BenchmarkEncodeInt32/vmihailenco/int32_via_EncodeInt32()-4                    50000000        32.1 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt64/___lestrrat/int64_via_Encode()-4                         30000000        56.9 ns/op         8 B/op         1 allocs/op
BenchmarkEncodeInt64/___lestrrat/int64_via_EncodeInt64()-4                   100000000        24.8 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeInt64/vmihailenco/int64_via_Encode()-4                         20000000        62.3 ns/op         8 B/op         1 allocs/op
BenchmarkEncodeInt64/vmihailenco/int64_via_EncodeInt64()-4                    50000000        30.6 ns/op         0 B/op         0 allocs/op
BenchmarkEncodeString/___lestrrat/string_(255_bytes)_via_Encode()-4           10000000         208 ns/op       272 B/op         2 allocs/op
BenchmarkEncodeString/___lestrrat/string_(255_bytes)_via_EncodeString()-4     10000000         149 ns/op       256 B/op         1 allocs/op
BenchmarkEncodeString/___lestrrat/string_(256_bytes)_via_Encode()-4           10000000         190 ns/op       272 B/op         2 allocs/op
BenchmarkEncodeString/___lestrrat/string_(256_bytes)_via_EncodeString()-4     10000000         153 ns/op       256 B/op         1 allocs/op
BenchmarkEncodeString/___lestrrat/string_(65536_bytes)_via_Encode()-4           100000       13031 ns/op     65552 B/op         2 allocs/op
BenchmarkEncodeString/___lestrrat/string_(65536_bytes)_via_EncodeString()-4     100000       13420 ns/op     65536 B/op         1 allocs/op
BenchmarkEncodeString/vmihailenco/string_(255_bytes)_via_Encode()-4           10000000         204 ns/op       272 B/op         2 allocs/op
BenchmarkEncodeString/vmihailenco/string_(255_bytes)_via_EncodeString()-4     10000000         148 ns/op       256 B/op         1 allocs/op
BenchmarkEncodeString/vmihailenco/string_(256_bytes)_via_Encode()-4           10000000         206 ns/op       272 B/op         2 allocs/op
BenchmarkEncodeString/vmihailenco/string_(256_bytes)_via_EncodeString()-4     10000000         163 ns/op       256 B/op         1 allocs/op
BenchmarkEncodeString/vmihailenco/string_(65536_bytes)_via_Encode()-4           100000       14108 ns/op     65552 B/op         2 allocs/op
BenchmarkEncodeString/vmihailenco/string_(65536_bytes)_via_EncodeString()-4     100000       19093 ns/op     65536 B/op         1 allocs/op
BenchmarkEncodeArray/___lestrrat/array_via_Encode()-4                          3000000         484 ns/op        56 B/op         4 allocs/op
BenchmarkEncodeArray/___lestrrat/array_via_EncodeArray()-4                     5000000         345 ns/op        56 B/op         4 allocs/op
BenchmarkEncodeArray/vmihailenco/array_via_Encode()-4                          2000000         757 ns/op        33 B/op         2 allocs/op
BenchmarkEncodeMap/___lestrrat/map_via_Encode()-4                              2000000         994 ns/op       208 B/op         8 allocs/op
BenchmarkEncodeMap/___lestrrat/map_via_EncodeMap()-4                           1000000        1092 ns/op       208 B/op         8 allocs/op
BenchmarkEncodeMap/vmihailenco/map_via_Encode()-4                              1000000        1995 ns/op       224 B/op        11 allocs/op
BenchmarkDecodeUint8/___lestrrat/uint8_via_DecodeUint8()-4                    20000000        60.0 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint8/vmihailenco/uint8_via_DecodeUint8()_(return)-4           30000000        54.2 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint16/___lestrrat/uint16_via_DecodeUint16()-4                 30000000        53.6 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint16/vmihailenco/uint16_via_DecodeUint16()_(return)-4        20000000        94.0 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint32/___lestrrat/uint32_via_DecodeUint32()-4                 30000000        45.7 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint32/vmihailenco/uint32_via_DecodeUint32()_(return)-4        20000000        92.8 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint64/___lestrrat/uint64_via_DecodeUint64()-4                 30000000        48.5 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeUint64/vmihailenco/uint64_via_DecodeUint64()_(return)-4        20000000        86.1 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt8FixNum/___lestrrat/int8_via_DecodeInt8()-4                 50000000        34.8 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt8FixNum/vmihailenco/int8_via_DecodeInt8()_(return)-4        30000000        42.2 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt8/___lestrrat/int8_via_DecodeInt8()-4                       30000000        48.6 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt8/vmihailenco/int8_via_DecodeInt8()_(return)-4              30000000        55.5 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt16/___lestrrat/int16_via_DecodeInt16()-4                    30000000        47.2 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt16/vmihailenco/int16_via_DecodeInt16()_(return)-4           20000000        87.8 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt32/___lestrrat/int32_via_DecodeInt32()-4                    30000000        44.3 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt32/vmihailenco/int32_via_DecodeInt32()_(return)-4           20000000        87.5 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt64/___lestrrat/int64_via_DecodeInt64()-4                    30000000        47.6 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeInt64/vmihailenco/int64_via_DecodeInt64()_(return)-4           20000000        84.3 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeFloat32/___lestrrat/float32_via_DecodeFloat32()-4              30000000        39.1 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeFloat32/vmihailenco/float32_via_DecodeFloat32()_(return)-4     20000000        82.0 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeFloat64/___lestrrat/float64_via_DecodeFloat64()-4              30000000        38.4 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeFloat64/vmihailenco/float64_via_DecodeFloat64()_(return)-4     20000000        84.7 ns/op         0 B/op         0 allocs/op
BenchmarkDecodeString/___lestrrat/string_via_Decode()-4                        5000000         294 ns/op       256 B/op         1 allocs/op
BenchmarkDecodeString/___lestrrat/string_via_DecodeString()-4                  5000000         288 ns/op       256 B/op         1 allocs/op
BenchmarkDecodeString/vmihailenco/string_via_Decode()-4                       10000000         223 ns/op       256 B/op         1 allocs/op
BenchmarkDecodeString/vmihailenco/string_via_DecodeString()_(return)-4        10000000         211 ns/op       256 B/op         1 allocs/op
BenchmarkDecodeArray/___lestrrat/array_via_Decode()_(concrete)-4               2000000         773 ns/op       104 B/op         5 allocs/op
BenchmarkDecodeArray/___lestrrat/array_via_Decode()_(interface{})-4            2000000         942 ns/op       120 B/op         6 allocs/op
BenchmarkDecodeArray/___lestrrat/array_via_DecodeArray()-4                     2000000         763 ns/op       104 B/op         5 allocs/op
BenchmarkDecodeArray/vmihailenco/array_via_Decode()_(concrete)-4               1000000        1607 ns/op       248 B/op         9 allocs/op
BenchmarkDecodeArray/vmihailenco/array_via_Decode()_(interface{})-4            2000000         727 ns/op       120 B/op         6 allocs/op
BenchmarkDecodeMap/___lestrrat/map_via_Decode()-4                              1000000        1660 ns/op       440 B/op        12 allocs/op
BenchmarkDecodeMap/___lestrrat/map_via_DecodeMap()-4                           1000000        1609 ns/op       440 B/op        12 allocs/op
BenchmarkDecodeMap/vmihailenco/map_via_Decode()-4                              1000000        1070 ns/op       392 B/op         9 allocs/op
PASS
ok    github.com/lestrrat-go/msgpack  171.139s

ACKNOWLEDGEMENTS

Much has been stolen from https://github.com/vmihailenco/msgpack

Documentation

Index

Constants

View Source
const MaxPositiveFixNum = byte(0x7f)
View Source
const MinNegativeFixNum = byte(0xe0)

Variables

This section is empty.

Functions

func IsArrayFamily

func IsArrayFamily(c Code) bool

IsArrayFamily returns true if the given code is equivalent to one of the `array` family in msgpack

func IsBinFamily

func IsBinFamily(c Code) bool

IsBinFamily returns true if the given code is equivalent to one of the `bin` family in msgpack

func IsExtFamily

func IsExtFamily(c Code) bool

IsExtFamily returns true if the given code is equivalent to one of the `ext` family in msgpack

func IsFixNumFamily

func IsFixNumFamily(c Code) bool

IsFixNumFamily returns true if the given code is equivalent to one of the fixed num family

func IsMapFamily

func IsMapFamily(c Code) bool

IsMapFamily returns true if the given code is equivalent to one of the `map` family in msgpack

func IsNegativeFixNum

func IsNegativeFixNum(c Code) bool

func IsPositiveFixNum

func IsPositiveFixNum(c Code) bool

func IsStrFamily

func IsStrFamily(c Code) bool

IsStrFamily returns true if the given code is equivalent to one of the `str` family in msgpack

func Marshal

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

Marshal takes a Go value and serializes it in msgpack format.

func RegisterExt

func RegisterExt(typ int, v interface{}) error

func Unmarshal

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

Unmarshal takes a byte slice and a pointer to a Go value and deserializes the Go value from the data in msgpack format.

func WriteArrayHeader

func WriteArrayHeader(dst io.Writer, c int) error

func WriteMapHeader

func WriteMapHeader(dst io.Writer, c int) error

Types

type ArrayBuilder

type ArrayBuilder interface {
	Add(interface{})
	Bytes() ([]byte, error)
	Count() int
	Encode(io.Writer) error
	Reset()
}

ArrayBuilder is used to build a msgpack array

func NewArrayBuilder

func NewArrayBuilder() ArrayBuilder

type Code

type Code byte

Code represents the first by in a msgpack element. It tell us the data layout that follows it

const (
	InvalidCode    Code = 0
	FixMap0        Code = 0x80
	FixMap1        Code = 0x81
	FixMap2        Code = 0x82
	FixMap3        Code = 0x83
	FixMap4        Code = 0x84
	FixMap5        Code = 0x85
	FixMap6        Code = 0x86
	FixMap7        Code = 0x87
	FixMap8        Code = 0x88
	FixMap9        Code = 0x89
	FixMap10       Code = 0x8a
	FixMap11       Code = 0x8b
	FixMap12       Code = 0x8c
	FixMap13       Code = 0x8d
	FixMap14       Code = 0x8e
	FixMap15       Code = 0x8f
	FixArray0      Code = 0x90
	FixArray1      Code = 0x91
	FixArray2      Code = 0x92
	FixArray3      Code = 0x93
	FixArray4      Code = 0x94
	FixArray5      Code = 0x95
	FixArray6      Code = 0x96
	FixArray7      Code = 0x97
	FixArray8      Code = 0x98
	FixArray9      Code = 0x99
	FixArray10     Code = 0x9a
	FixArray11     Code = 0x9b
	FixArray12     Code = 0x9c
	FixArray13     Code = 0x9d
	FixArray14     Code = 0x9e
	FixArray15     Code = 0x9f
	NegFixedNumLow Code = 0xe0
	FixStr0        Code = 0xa0
	FixStr1        Code = 0xa1
	FixStr2        Code = 0xa2
	FixStr3        Code = 0xa3
	FixStr4        Code = 0xa4
	FixStr5        Code = 0xa5
	FixStr6        Code = 0xa6
	FixStr7        Code = 0xa7
	FixStr8        Code = 0xa8
	FixStr9        Code = 0xa9
	FixStr10       Code = 0xaa
	FixStr11       Code = 0xab
	FixStr12       Code = 0xac
	FixStr13       Code = 0xad
	FixStr14       Code = 0xae
	FixStr15       Code = 0xaf
	FixStr16       Code = 0xb0
	FixStr17       Code = 0xb1
	FixStr18       Code = 0xb2
	FixStr19       Code = 0xb3
	FixStr20       Code = 0xb4
	FixStr21       Code = 0xb5
	FixStr22       Code = 0xb6
	FixStr23       Code = 0xb7
	FixStr24       Code = 0xb8
	FixStr25       Code = 0xb9
	FixStr26       Code = 0xba
	FixStr27       Code = 0xbb
	FixStr28       Code = 0xbc
	FixStr29       Code = 0xbd
	FixStr30       Code = 0xbe
	FixStr31       Code = 0xbf
	Nil            Code = 0xc0
	False          Code = 0xc2
	True           Code = 0xc3
	Bin8           Code = 0xc4
	Bin16          Code = 0xc5
	Bin32          Code = 0xc6
	Ext8           Code = 0xc7
	Ext16          Code = 0xc8
	Ext32          Code = 0xc9
	Float          Code = 0xca
	Double         Code = 0xcb
	Uint8          Code = 0xcc
	Uint16         Code = 0xcd
	Uint32         Code = 0xce
	Uint64         Code = 0xcf
	Int8           Code = 0xd0
	Int16          Code = 0xd1
	Int32          Code = 0xd2
	Int64          Code = 0xd3
	FixExt1        Code = 0xd4
	FixExt2        Code = 0xd5
	FixExt4        Code = 0xd6
	FixExt8        Code = 0xd7
	FixExt16       Code = 0xd8
	Str8           Code = 0xd9
	Str16          Code = 0xda
	Str32          Code = 0xdb
	Array16        Code = 0xdc
	Array32        Code = 0xdd
	Map16          Code = 0xde
	Map32          Code = 0xdf
	FixedArrayMask Code = 0xf
)

func (Code) Byte

func (t Code) Byte() byte

Byte returns the byte representation of the Code

func (Code) String

func (i Code) String() string

type DecodeMsgpacker

type DecodeMsgpacker interface {
	DecodeMsgpack(*Decoder) error
}

DecodeMsgpacker is an interface for those objects that provide their own deserialization. The objects are responsible for handling the code, payload length (if applicable), and payload (if applicable)

type Decoder

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

Encoder reads serialized data from a source pointed to by an io.Reader

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

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

Decode takes a pointer to a variable, and populates it with the value that was unmarshaled from the stream.

If the variable is a non-pointer or nil, an error is returned.

func (*Decoder) DecodeArray

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

func (*Decoder) DecodeArrayLength

func (d *Decoder) DecodeArrayLength(l *int) error

func (*Decoder) DecodeBool

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

func (*Decoder) DecodeBytes

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

func (*Decoder) DecodeExt

func (d *Decoder) DecodeExt(v DecodeMsgpacker) error

func (*Decoder) DecodeExtLength

func (d *Decoder) DecodeExtLength(l *int) error

func (*Decoder) DecodeExtType

func (d *Decoder) DecodeExtType(v *reflect.Type) 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[string]interface{}) error

func (*Decoder) DecodeMapLength

func (d *Decoder) DecodeMapLength(l *int) error

func (*Decoder) DecodeNil

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

func (*Decoder) DecodeString

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

func (*Decoder) DecodeStruct

func (d *Decoder) DecodeStruct(v interface{}) 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

func (*Decoder) PeekCode

func (d *Decoder) PeekCode() (Code, error)

func (*Decoder) ReadCode

func (d *Decoder) ReadCode() (Code, error)

func (*Decoder) Reader

func (d *Decoder) Reader() Reader

type EncodeMsgpacker

type EncodeMsgpacker interface {
	EncodeMsgpack(*Encoder) error
}

EncodeMsgpacker is an interface for those objects that provide their own serialization. The objects are responsible for providing the complete msgpack payload, including the code, payload length (if applicable), and payload (if applicable)

type Encoder

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

Encoder writes serialized data to a destination pointed to by an io.Writer

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates a new Encoder that writes serialized forms to the specified io.Writer

Note that Encoders are NEVER meant to be shared concurrently between goroutines. You DO NOT write serialized data concurrently to the same destination.

func (*Encoder) Encode

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

func (*Encoder) EncodeArray

func (e *Encoder) EncodeArray(v interface{}) error

func (*Encoder) EncodeArrayHeader

func (e *Encoder) EncodeArrayHeader(l int) error

func (*Encoder) EncodeBool

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

func (*Encoder) EncodeBytes

func (e *Encoder) EncodeBytes(b []byte) error

func (*Encoder) EncodeExt

func (e *Encoder) EncodeExt(v EncodeMsgpacker) error

func (*Encoder) EncodeExtHeader

func (e *Encoder) EncodeExtHeader(l int) error

func (*Encoder) EncodeExtType

func (e *Encoder) EncodeExtType(v EncodeMsgpacker) error

func (*Encoder) EncodeFloat32

func (e *Encoder) EncodeFloat32(f float32) error

func (*Encoder) EncodeFloat64

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

func (*Encoder) EncodeInt

func (e *Encoder) EncodeInt(v int) error

func (*Encoder) EncodeInt16

func (e *Encoder) EncodeInt16(v int16) error

func (*Encoder) EncodeInt32

func (e *Encoder) EncodeInt32(v int32) error

func (*Encoder) EncodeInt64

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

func (*Encoder) EncodeInt8

func (e *Encoder) EncodeInt8(v int8) error

func (*Encoder) EncodeMap

func (e *Encoder) EncodeMap(v interface{}) error

func (*Encoder) EncodeNegativeFixNum

func (e *Encoder) EncodeNegativeFixNum(i int8) error

func (*Encoder) EncodeNil

func (e *Encoder) EncodeNil() error

func (*Encoder) EncodePositiveFixNum

func (e *Encoder) EncodePositiveFixNum(i uint8) error

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(s string) error

func (*Encoder) EncodeStruct

func (e *Encoder) EncodeStruct(v interface{}) error

EncodeStruct encodes a struct value as a map object.

func (*Encoder) EncodeTime

func (e *Encoder) EncodeTime(t time.Time) error

EncodeTime encodes time.Time as a sequence of two integers

func (*Encoder) EncodeUint

func (e *Encoder) EncodeUint(v uint) error

func (*Encoder) EncodeUint16

func (e *Encoder) EncodeUint16(v uint16) error

func (*Encoder) EncodeUint32

func (e *Encoder) EncodeUint32(v uint32) error

func (*Encoder) EncodeUint64

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

func (*Encoder) EncodeUint8

func (e *Encoder) EncodeUint8(v uint8) error

func (*Encoder) Writer

func (e *Encoder) Writer() Writer

type InvalidDecodeError

type InvalidDecodeError struct {
	Type reflect.Type
}

func (*InvalidDecodeError) Error

func (e *InvalidDecodeError) Error() string

type MapBuilder

type MapBuilder interface {
	Add(string, interface{})
	Bytes() ([]byte, error)
	Count() int
	Encode(io.Writer) error
	Reset()
}

MapBuilder is used to build a msgpack map

func NewMapBuilder

func NewMapBuilder() MapBuilder

type Reader

type Reader interface {
	io.Reader
	ReadByte() (byte, error)
	ReadUint8() (uint8, error)
	ReadUint16() (uint16, error)
	ReadUint32() (uint32, error)
	ReadUint64() (uint64, error)
	ReadByteUint8() (byte, uint8, error)
	ReadByteUint16() (byte, uint16, error)
	ReadByteUint32() (byte, uint32, error)
	ReadByteUint64() (byte, uint64, error)
}

Reader handles low-level reading from an io.Reader. Note that Readers are NEVER meant to be shared concurrently between goroutines. You DO NOT read data concurrently from the same serialized source.

func NewReader

func NewReader(r io.Reader) Reader

type Writer

type Writer interface {
	io.Writer
	WriteByte(byte) error
	WriteByteUint8(byte, uint8) error
	WriteByteUint16(byte, uint16) error
	WriteByteUint32(byte, uint32) error
	WriteByteUint64(byte, uint64) error
	WriteString(string) (int, error)
	WriteUint8(uint8) error
	WriteUint16(uint16) error
	WriteUint32(uint32) error
	WriteUint64(uint64) error
}

Writer handles low-level writing to an io.Writer. Note that Writers are NEVER meant to be shared concurrently between goroutines. You DO NOT write serialized data concurrently to the same destination.

func NewWriter

func NewWriter(w io.Writer) Writer

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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