msgpack

package
v0.0.0-...-cd3ace8 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const TimestampTypeCode = 0xFF

TimestampTypeCode is ext type code of Timestamp.

Variables

This section is empty.

Functions

func Marshal

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

Marshal is encode any data to message pack.

func MarshalStringKey

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

MarshalStringKey is encode any data to message pack.

func Unmarshal

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

Unmarshal is decode message pack data to any data.

func UnmarshalStringKey

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

UnmarshalStringKey is decode message pack data to any data.

Types

type ArrayHeader

type ArrayHeader struct {
	Format Format
	Length uint32
}

ArrayHeader is header of array data.

type ArrayLengthTolerance

type ArrayLengthTolerance int

ArrayLengthTolerance is tolerance of array length when decode time.

const (
	// ArrayLengthToleranceLessThanOrEqual is allowed if the array length of message pack is the same or less than or equal to the length of go.
	ArrayLengthToleranceLessThanOrEqual ArrayLengthTolerance = iota

	// ArrayLengthToleranceEqualOnly is allowed if the array length of message pack is the only equal to the length of go.
	ArrayLengthToleranceEqualOnly

	// ArrayLengthToleranceRounding is allowed always.
	// If the array length of  is the same or greater than to the length of go, overloaded data is discarded.
	ArrayLengthToleranceRounding
)

func (ArrayLengthTolerance) String

func (i ArrayLengthTolerance) String() string

type Decoder

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

Decoder is message pack decoder.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder is create decoder instance.

func NewDecoderBytes

func NewDecoderBytes(in []byte) *Decoder

NewDecoderBytes is create decoder instance.

func (*Decoder) Decode

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

Decode is decode data from message pack.

Example (Streaming)
package main

import (
	"fmt"
	"log"

	"go.nanasi880.dev/x/encoding/msgpack"
)

func main() {

	examples := make([]int, 0)
	for i := 0; i < 10; i++ {
		examples = append(examples, i)
	}

	data, err := msgpack.Marshal(examples)
	if err != nil {
		log.Fatal(err)
	}

	ch := make(chan int)
	go func() {
		defer close(ch)

		dec := msgpack.NewDecoderBytes(data)

		format, err := dec.DecodeFormat()
		if err != nil {
			log.Fatal(err)
		}
		header, err := dec.DecodeArrayHeader(format)
		if err != nil {
			log.Fatal(err)
		}

		for i := uint32(0); i < header.Length; i++ {
			var value int
			err = dec.Decode(&value)
			if err != nil {
				log.Fatal(err)
			}
			ch <- value
		}
	}()

	for value := range ch {
		fmt.Println(value)
	}

}
Output:

0
1
2
3
4
5
6
7
8
9

func (*Decoder) DecodeArrayHeader

func (d *Decoder) DecodeArrayHeader(format Format) (ArrayHeader, error)

DecodeArrayHeader is decode array header from message pack.

func (*Decoder) DecodeExtHeader

func (d *Decoder) DecodeExtHeader(format Format) (ExtHeader, error)

DecodeExtHeader is decode ext header from message pack.

func (*Decoder) DecodeFloat32

func (d *Decoder) DecodeFloat32(format Format) (float32, error)

DecodeFloat32 is decode float type as float32 from message pack.

func (*Decoder) DecodeFloat64

func (d *Decoder) DecodeFloat64(format Format) (float64, error)

DecodeFloat64 is decode float type as float64 from message pack.

func (*Decoder) DecodeFormat

func (d *Decoder) DecodeFormat() (Format, error)

DecodeFormat is decode format from message pack.

func (*Decoder) DecodeInt64

func (d *Decoder) DecodeInt64(format Format) (int64, error)

DecodeInt64 is decode integer type as int64 from message pack.

func (*Decoder) DecodeMapHeader

func (d *Decoder) DecodeMapHeader(format Format) (MapHeader, error)

DecodeMapHeader is decode map header from message pack.

func (*Decoder) DecodeString

func (d *Decoder) DecodeString(format Format) (string, error)

DecodeString is decode string type or bin type as string from message pack.

func (*Decoder) DecodeTime

func (d *Decoder) DecodeTime(header ExtHeader) (time.Time, error)

DecodeTime is decode time from message pack.

func (*Decoder) DecodeUint64

func (d *Decoder) DecodeUint64(format Format) (uint64, error)

DecodeUint64 is decode integer type as uint64 from message pack.

func (*Decoder) Reset

func (d *Decoder) Reset(r io.Reader)

Reset is reset decoder. However, the work buffer will not be reset.

func (*Decoder) ResetBytes

func (d *Decoder) ResetBytes(in []byte)

ResetBytes is reset decoder. However, the work buffer will not be reset.

func (*Decoder) SetArrayLengthTolerance

func (d *Decoder) SetArrayLengthTolerance(tolerance ArrayLengthTolerance) *Decoder

SetArrayLengthTolerance is set ArrayLengthTolerance to Decoder.

func (*Decoder) SetStructKeyType

func (d *Decoder) SetStructKeyType(t StructKeyType) *Decoder

SetStructKeyType is set StructKeyType to Decoder.

func (*Decoder) SetStructTagName

func (d *Decoder) SetStructTagName(tagName string) *Decoder

SetStructTagName is set struct tag name to Decoder. If tagName is empty, use `msgpack` tag.

func (*Decoder) SetTimeZone

func (d *Decoder) SetTimeZone(loc *time.Location) *Decoder

SetTimeZone is set time zone to Decoder. The decoder will set this time zone to the time when decoding. If loc is nil, use the UTC time.

func (*Decoder) SkipObject

func (d *Decoder) SkipObject(format Format) error

SkipObject is seek current message pack object.

type Encoder

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

Encoder is message pack encoder.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder is create encoder instance.

func (*Encoder) Encode

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

Encode is encode data as message pack.

func (*Encoder) EncodeArrayHeader

func (e *Encoder) EncodeArrayHeader(length uint32) error

EncodeArrayHeader is encode array header as message pack.

func (*Encoder) EncodeBin

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

EncodeBin is encode binary as message pack.

func (*Encoder) EncodeBool

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

EncodeBool is encode bool as message pack.

func (*Encoder) EncodeFloat32

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

EncodeFloat32 is encode float32 as message pack.

func (*Encoder) EncodeFloat64

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

EncodeFloat64 is encode float64 as message pack.

func (*Encoder) EncodeInt64

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

EncodeInt64 is encode int64 as message pack.

func (*Encoder) EncodeMapHeader

func (e *Encoder) EncodeMapHeader(length uint32) error

EncodeMapHeader is encode map header as message pack.

func (*Encoder) EncodeNil

func (e *Encoder) EncodeNil() error

EncodeNil is encode Nil as message pack.

func (*Encoder) EncodeString

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

EncodeString is encode string as message pack.

func (*Encoder) EncodeTime

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

EncodeTime is encode time.Time as message pack.

func (*Encoder) EncodeUint64

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

EncodeUint64 is encode uint64 as message pack.

func (*Encoder) Reset

func (e *Encoder) Reset(w io.Writer)

Reset is reset encoder. However, the work buffer will not be reset.

func (*Encoder) SetStructKeyType

func (e *Encoder) SetStructKeyType(t StructKeyType) *Encoder

SetStructKeyType is set StructKeyType to Encoder.

func (*Encoder) SetStructTagName

func (e *Encoder) SetStructTagName(tagName string) *Encoder

SetStructTagName is set struct tag name to Encoder. If tagName is empty, use `msgpack` tag.

type ExtHeader

type ExtHeader struct {
	Format Format
	Type   byte
	Length uint32
}

ExtHeader is header of ext data.

type Format

type Format struct {
	Name FormatName
	Raw  byte
}

Format is message pack format data.

func (Format) String

func (f Format) String() string

type FormatName

type FormatName byte
const (
	PositiveFixInt FormatName = 0
	FixMap         FormatName = 0x80
	FixArray       FormatName = 0x90
	FixStr         FormatName = 0xa0
	Nil            FormatName = 0xc0
	Unused         FormatName = 0xc1
	False          FormatName = 0xc2
	True           FormatName = 0xc3
	Bin8           FormatName = 0xc4
	Bin16          FormatName = 0xc5
	Bin32          FormatName = 0xc6
	Ext8           FormatName = 0xc7
	Ext16          FormatName = 0xc8
	Ext32          FormatName = 0xc9
	Float32        FormatName = 0xca
	Float64        FormatName = 0xcb
	Uint8          FormatName = 0xcc
	Uint16         FormatName = 0xcd
	Uint32         FormatName = 0xce
	Uint64         FormatName = 0xcf
	Int8           FormatName = 0xd0
	Int16          FormatName = 0xd1
	Int32          FormatName = 0xd2
	Int64          FormatName = 0xd3
	FixExt1        FormatName = 0xd4
	FixExt2        FormatName = 0xd5
	FixExt4        FormatName = 0xd6
	FixExt8        FormatName = 0xd7
	FixExt16       FormatName = 0xd8
	Str8           FormatName = 0xd9
	Str16          FormatName = 0xda
	Str32          FormatName = 0xdb
	Array16        FormatName = 0xdc
	Array32        FormatName = 0xdd
	Map16          FormatName = 0xde
	Map32          FormatName = 0xdf
	NegativeFixInt FormatName = 0xe0
)

func (FormatName) Byte

func (i FormatName) Byte() byte

func (FormatName) String

func (i FormatName) String() string

type MapHeader

type MapHeader struct {
	Format Format
	Length uint32
}

MapHeader is header of map data.

type Marshaler

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

Marshaler is the interface implemented by types that can marshal themselves into valid message pack.

type StructKeyType

type StructKeyType byte

StructKeyType is determines how to serialize/deserialize the structure when it is encode/decode.

const (
	StructKeyTypeInt    StructKeyType = iota // StructKeyTypeInt is struct serialize/deserialize as int key array format.
	StructKeyTypeString                      // StructKeyTypeString is struct serialize/deserialize as string key map format.
)

func (StructKeyType) String

func (i StructKeyType) String() string

type Unmarshaler

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

Unmarshaler is the interface implemented by types that can unmarshal themselves into valid message pack.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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