msgpack

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

integer.go

Example (BuildComplexMessage)
data := buildComplexMessage()
fmt.Println(data)
Output:


[131 170 115 105 109 112 108 101 95 107 101 121 172 115 105 109 112 108 101 95 118 97 108 117 101 172 110 101 115 116 101 100 95 97 114 114 97 121 147 1 2 147 3 4 5 170 110 101 115 116 101 100 95 109 97 112 130 169 105 110 110 101 114 95 107 101 121 146 195 194 169 102 108 111 97 116 95 107 101 121 203 64 9 33 249 240 27 134 110]
Example (IterateComplexMessage)
data := buildComplexMessage()
iter := NewIterator(nil)
iter.ResetBytes(data)

for iter.Next() {
	fmt.Println(iter.Type())

	if iter.Type() != types.Array && iter.Type() != types.Map {
		iter.Skip()
	}
}
Output:


map
str
str
str
array
uint
uint
array
uint
uint
uint
str
map
str
array
bool
bool
str
float

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrShortBuffer is returned when the byte slice is too short to read the expected data.
	ErrShortBuffer = io.ErrShortBuffer
	// ErrInvalidFormat is returned when the data does not conform to the expected MessagePack format.
	ErrInvalidFormat        = errors.New("invalid MessagePack format")
	ErrInvalidHeaderByte    = errors.New("invalid header byte")
	ErrInvalidExtByte       = errors.New("invalid extension byte")
	ErrReachedMaxBufferSize = errors.New("reached max buffer size")
	ErrInvalidOffset        = errors.New("offset must be greater than 0")
)

Functions

func AppendAny

func AppendAny(dst []byte, v any) []byte

func AppendArrayHeader

func AppendArrayHeader(dst []byte, n int) []byte

AppendArrayHeader appends a MessagePack array header to `dst` based on the number of elements `n`. Returns the updated byte slice.

func AppendBinary

func AppendBinary(dst []byte, data []byte) []byte

AppendBinary appends a MessagePack binary header and the binary `data` to `dst`. Returns the updated byte slice.

func AppendBinaryAppender

func AppendBinaryAppender(dst []byte, s encoding.BinaryAppender) []byte

AppendBinaryAppender appends a binary object to `dst` using a `BinaryAppender`. Returns the updated byte slice.

func AppendBinaryUnknownLength

func AppendBinaryUnknownLength(dst []byte, fn func(dst []byte) []byte) []byte

AppendBinaryUnknownLength appends a MessagePack binary header and binary data to the destination byte slice when the length of the binary data is unknown. It reserves space for the header, appends the data using the provided function `fn`, and updates the header with the actual length of the data.

func AppendBool

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

AppendBool appends a boolean value (`true` or `false`) as a MessagePack-encoded boolean to `dst`. Returns the updated byte slice.

func AppendFloat

func AppendFloat(dst []byte, f float64) []byte

AppendFloat appends a floating-point value (`f`) as a MessagePack-encoded float32 or float64 to `dst`. Returns the updated byte slice.

func AppendInt

func AppendInt(buf []byte, value int64) []byte

AppendInt appends a MessagePack-encoded integer to the buffer. Positive integers are encoded as TypeUint, and negative integers as TypeInt. It uses the most compact representation based on the value.

func AppendMapHeader

func AppendMapHeader(dst []byte, n int) []byte

AppendMapHeader appends a map header with `n` key-value pairs to `dst` as a MessagePack-encoded value. Returns the updated byte slice.

func AppendNil

func AppendNil(dst []byte) []byte

AppendNil appends a nil value to `dst` as a MessagePack-encoded value. Returns the updated byte slice.

func AppendString

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

AppendString appends the string `s` as a MessagePack-encoded value to `dst`. Returns the updated byte slice.

func AppendStringDynamic

func AppendStringDynamic(dst []byte, fn func(dst []byte) []byte) []byte

AppendStringDynamic appends a MessagePack-encoded string to dst. The string data is produced by calling fn. The function uses the smallest header possible without allocating additional memory (aside from appending).

func AppendStringMax255

func AppendStringMax255(dst []byte, fn func(dst []byte) []byte) []byte

AppendStringUnknownLength appends a string with an unknown length, but max 255 characters. The string data is appended using the provided function `fn`. Returns the updated byte slice.

func AppendStringUnknownLength

func AppendStringUnknownLength(dst []byte, fn func(dst []byte) []byte) []byte

AppendStringUnknownLength appends a string with an unknown length to `dst` as a MessagePack-encoded value. The string data is appended using the provided function `fn`. Returns the updated byte slice.

func AppendTextAppender

func AppendTextAppender(dst []byte, s encoding.TextAppender) []byte

AppendTextAppender appends a string to `dst` using a `TextAppender` and encodes it as a MessagePack string. Returns the updated byte slice.

func AppendTimestamp

func AppendTimestamp(dst []byte, t time.Time, format ...TsFormat) []byte

AppendTimestamp appends a timestamp to the given byte slice in the specified format. dst: The byte slice to append to. t: The time.Time value to encode. format: Optional argument to specify the encoding format (default is TsAuto). Returns the updated byte slice with the appended timestamp.

func AppendUint

func AppendUint(buf []byte, value uint64) []byte

AppendUint appends a MessagePack-encoded unsigned integer to the buffer. It uses the most compact representation based on the value.

func ReadArrayHeader

func ReadArrayHeader(src []byte, offset int) (length int, newOffset int, err error)

ReadArrayHeader reads a MessagePack array header from `src` starting at `offset`. Returns the array length, the new offset, and an error if the header is invalid.

func ReadBinary

func ReadBinary(src []byte, offset int) (data []byte, newOffset int, err error)

ReadBinary reads a MessagePack binary object from `src` starting at `offset`. Returns the binary data, the new offset, and an error if the header is invalid or the buffer is too short.

func ReadBool

func ReadBool(src []byte, offset int) (value bool, newOffset int, err error)

ReadBool reads a MessagePack-encoded boolean value from `src` starting at `offset`. Returns the boolean value, the new offset, and an error if the header is invalid or the buffer is too short.

func ReadFloat

func ReadFloat(src []byte, offset int) (value float64, newOffset int, err error)

ReadFloat reads a MessagePack-encoded floating-point value from `src` starting at `offset`. Returns the floating-point value, the new offset, and an error if the data is invalid or incomplete.

func ReadInt

func ReadInt(src []byte, offset int) (value int64, newOffset int, err error)

ReadInt reads a MessagePack-encoded integer from `src` starting at `offset`. It handles both signed (TypeInt) and unsigned (TypeUint) integers. Returns the integer value as int64, the new offset, and an error if the data is invalid or incomplete.

func ReadMapHeader

func ReadMapHeader(src []byte, offset int) (length int, newOffset int, err error)

ReadMapHeader reads a map header from `src` starting at `offset`. Returns the number of key-value pairs, the new offset, and an error if the header is invalid.

func ReadNil

func ReadNil(src []byte, offset int) (newOffset int, err error)

ReadNil reads a nil value from `src` starting at `offset`. Returns the new offset and an error if the value is not nil or the buffer is too short.

func ReadString

func ReadString(src []byte, offset int) (s string, newOffset int, err error)

ReadString reads a MessagePack-encoded string from `src` starting at `offset`. Returns a zero-copy reference to the string from the `src` slice, the new offset, and an error if the data is invalid or incomplete.

func ReadStringCopy

func ReadStringCopy(src []byte, offset int) (s string, newOffset int, err error)

ReadStringCopy reads a MessagePack-encoded string from `src` starting at `offset`. Returns a copy of the string, the new offset, and an error if the data is invalid or incomplete.

func ReadTimestamp

func ReadTimestamp(src []byte, offset int) (t time.Time, newOffset int, err error)

ReadTimestamp decodes a timestamp from the given byte slice starting at the specified offset. src: The byte slice containing the encoded timestamp. offset: The position in the slice to start decoding from. Returns the decoded time.Time value, the new offset after decoding, and any error encountered.

Example
var buf []byte

t := time.Date(2025, 01, 01, 1, 2, 3, 0, time.UTC)
buf = AppendTimestamp(buf, t, Ts32)
buf = AppendTimestamp(buf, t, Ts64)
buf = AppendTimestamp(buf, t, Ts96)
buf = AppendTimestamp(buf, t, TsAuto)
buf = AppendTimestamp(buf, t, TsFluentd)
buf = AppendTimestamp(buf, t, TsInt)

fmt.Println(buf)

for range 6 {
	fmt.Println(sanitizeTimestamp(ReadTimestamp(buf, 0)))
}
Output:


[214 255 103 116 148 11 215 255 25 221 37 2 192 0 0 0 199 12 255 0 0 0 0 0 0 0 0 103 116 148 11 214 255 103 116 148 11 215 0 103 116 148 11 0 0 0 0 206 103 116 148 11]
2025-01-01 01:02:03 +0000 UTC <nil>
2025-01-01 01:02:03 +0000 UTC <nil>
2025-01-01 01:02:03 +0000 UTC <nil>
2025-01-01 01:02:03 +0000 UTC <nil>
2025-01-01 01:02:03 +0000 UTC <nil>
2025-01-01 01:02:03 +0000 UTC <nil>

func ReadUint

func ReadUint(src []byte, offset int) (value uint64, newOffset int, err error)

ReadUint reads a MessagePack-encoded unsigned integer from `src` starting at `offset`. It only accepts TypeUint and returns an error for any other type. Returns the unsigned integer value as uint64, the new offset, and an error if the data is invalid or incomplete.

Types

type Buffer added in v0.6.0

type Buffer struct {
	*buffer.Buffer
}

func (Buffer) WriteArrayHeader added in v0.6.0

func (w Buffer) WriteArrayHeader(n int)

WriteArrayHeader appends an array header to the buffer.

func (Buffer) WriteBinary added in v0.6.0

func (w Buffer) WriteBinary(data []byte)

WriteBinary appends binary data to the buffer.

func (Buffer) WriteBool added in v0.6.0

func (w Buffer) WriteBool(b bool)

WriteBool appends a boolean to the buffer.

func (Buffer) WriteFloat added in v0.6.0

func (w Buffer) WriteFloat(f float64)

WriteFloat64 appends a 64-bit floating-point number to the buffer.

func (Buffer) WriteInt added in v0.6.0

func (w Buffer) WriteInt(i int64)

WriteInt appends an integer to the buffer.

func (Buffer) WriteMapHeader added in v0.6.0

func (w Buffer) WriteMapHeader(n int)

WriteMapHeader appends a map header to the buffer.

func (Buffer) WriteNil added in v0.6.0

func (w Buffer) WriteNil()

WriteNil appends a nil value to the buffer.

func (Buffer) WriteString added in v0.6.0

func (w Buffer) WriteString(s string)

WriteString appends a string to the buffer.

func (Buffer) WriteStringMax255 added in v0.6.0

func (w Buffer) WriteStringMax255(fn func(dst []byte) []byte)

WriteStringMax255 appends a string of unknown length (but max 255 characters) to the buffer.

func (Buffer) WriteTimestamp added in v0.6.0

func (w Buffer) WriteTimestamp(t time.Time, format ...TsFormat)

WriteTimestamp appends a timestamp to the buffer.

func (Buffer) WriteUint added in v0.6.0

func (w Buffer) WriteUint(i uint64)

WriteUint appends an unsigned integer to the buffer.

type Float

type Float interface {
	~float32 | ~float64
}

type IterPool

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

memory waste.

func (*IterPool) Get

func (p *IterPool) Get(r io.Reader) (iter *Iterator)

func (*IterPool) Put

func (p *IterPool) Put(iter *Iterator)

type Iterator

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

Low-level iteration of a MessagePack stream.

func NewIterator

func NewIterator(r io.Reader) Iterator

func (*Iterator) Any

func (iter *Iterator) Any() any

func (*Iterator) AppendText added in v0.5.0

func (iter *Iterator) AppendText(b []byte) ([]byte, error)

Appends a text representation of current value to b. Implements encoding.TextAppender.

func (*Iterator) Bin

func (iter *Iterator) Bin() []byte

func (*Iterator) Bool

func (iter *Iterator) Bool() bool

func (*Iterator) Buffered added in v0.6.0

func (iter *Iterator) Buffered() int

func (*Iterator) DebugDump added in v0.6.0

func (iter *Iterator) DebugDump(w io.Writer)

func (*Iterator) Error

func (iter *Iterator) Error() error

func (*Iterator) Float

func (iter *Iterator) Float() float64

func (*Iterator) Flush added in v0.6.0

func (iter *Iterator) Flush()

func (*Iterator) Int

func (iter *Iterator) Int() int64

func (*Iterator) Items

func (iter *Iterator) Items() int

func (*Iterator) Len

func (iter *Iterator) Len() int

func (*Iterator) Next

func (iter *Iterator) Next() bool

Read next token. Must be called before any Read* method.

func (*Iterator) NextExpectedType

func (iter *Iterator) NextExpectedType(expected ...types.Type) (err error)

func (*Iterator) Reader

func (iter *Iterator) Reader() *ringbuf.LimitedReader

func (*Iterator) Reset

func (iter *Iterator) Reset(r io.Reader)

func (*Iterator) ResetBytes

func (iter *Iterator) ResetBytes(b []byte)

func (*Iterator) Rewind added in v0.6.0

func (iter *Iterator) Rewind()

func (*Iterator) RingReader added in v0.6.0

func (iter *Iterator) RingReader() *ringbuf.Reader

func (*Iterator) SetManualFlush added in v0.6.0

func (iter *Iterator) SetManualFlush(v bool)

func (*Iterator) Skip

func (iter *Iterator) Skip()

func (*Iterator) Str

func (iter *Iterator) Str() string

func (*Iterator) Time

func (iter *Iterator) Time() time.Time

func (*Iterator) TotalRead

func (r *Iterator) TotalRead() int

Get total bytes read from underlying io.Reader since last reset.

func (*Iterator) Type

func (iter *Iterator) Type() types.Type

func (*Iterator) Uint

func (iter *Iterator) Uint() uint64

type Numeric

type Numeric interface {
	Signed | Unsigned
}

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type TsFormat

type TsFormat uint8
const (
	TsAuto    TsFormat = iota // Automatically determine the best timestamp format.
	Ts32                      // 0xd6: Unix timestamp with seconds precision.
	Ts64                      // 0xd7: Unix timestamp with nanoseconds precision (compact).
	Ts96                      // 0xc7: Unix timestamp with nanoseconds precision (full).
	TsInt                     // Unix timestamp with seconds precision as regular integer.
	TsFluentd                 // Fluntd Forward EventTime.
)

func (TsFormat) String

func (f TsFormat) String() string

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type Value

type Value []byte

func (Value) AppendBinary

func (v Value) AppendBinary(b []byte) ([]byte, error)

AppendBinary implements encoding.BinaryAppender.

func (Value) AppendJson

func (v Value) AppendJson(b []byte) ([]byte, error)

AppendJson implements fast.JsonAppender.

func (Value) AppendText

func (v Value) AppendText(b []byte) ([]byte, error)

AppendText implements encoding.TextAppender.

func (Value) Array

func (v Value) Array() iter.Seq[Value]
Example
var buf Value

buf = AppendArrayHeader(buf, 3)
buf = AppendString(buf, "foo")
buf = AppendString(buf, "bar")
buf = AppendString(buf, "baz")

fmt.Println(buf.Len())

for v := range buf.Array() {
	fmt.Println(v.String())
}
Output:


3
foo
bar
baz

func (Value) Bin

func (v Value) Bin() (val []byte)

func (Value) Bool

func (v Value) Bool() (val bool)

func (Value) BytesLen

func (v Value) BytesLen() (l int)

Returns the total number of bytes for the value. Head + body is included for all types except array and maps, where the body is excluded.

func (Value) Float

func (v Value) Float() (val float64)

func (Value) Int

func (v Value) Int() (val int64)

func (Value) IsZero

func (v Value) IsZero() bool

func (Value) Len

func (v Value) Len() (l int)

func (Value) Map

func (v Value) Map() iter.Seq2[Value, Value]
Example
var buf Value

buf = AppendMapHeader(buf, 3)
buf = AppendString(buf, "foo")
buf = AppendInt(buf, 123)
buf = AppendString(buf, "bar")
buf = AppendInt(buf, 456)
buf = AppendString(buf, "baz")
buf = AppendInt(buf, 789)

fmt.Println(buf.Len())

for k, v := range buf.Map() {
	fmt.Println(k.String(), v.String())
}
Output:


3
foo 123
bar 456
baz 789

func (Value) Str

func (v Value) Str() (val string)

func (Value) StrCopy

func (v Value) StrCopy() (val string)

func (Value) String

func (v Value) String() string

Returns an allocated string representation of the value.

func (Value) Timestamp

func (v Value) Timestamp() (val time.Time)

func (Value) Type

func (v Value) Type() (t types.Type)

func (Value) Uint

func (v Value) Uint() (val uint64)

type Writer

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

func NewWriter

func NewWriter(w io.Writer, buf *buffer.Buffer) Writer

NewWriter creates a new Writer with the provided io.Writer and initial buffer size.

func (Writer) Flush

func (w Writer) Flush() (err error)

func (*Writer) Reset

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

func (Writer) WriteBinaryReader

func (w Writer) WriteBinaryReader(size int, r io.Reader) (err error)

func (Writer) WriteCustom

func (w Writer) WriteCustom(fn func([]byte) []byte)

WriteCustom appends custom data to the buffer using a provided function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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