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 ¶
- Variables
- func AppendAny(dst []byte, v any) []byte
- func AppendArrayHeader(dst []byte, n int) []byte
- func AppendBinary(dst []byte, data []byte) []byte
- func AppendBinaryAppender(dst []byte, s encoding.BinaryAppender) []byte
- func AppendBinaryUnknownLength(dst []byte, fn func(dst []byte) []byte) []byte
- func AppendBool(dst []byte, b bool) []byte
- func AppendFloat(dst []byte, f float64) []byte
- func AppendInt(buf []byte, value int64) []byte
- func AppendMapHeader(dst []byte, n int) []byte
- func AppendNil(dst []byte) []byte
- func AppendString(dst []byte, s string) []byte
- func AppendStringDynamic(dst []byte, fn func(dst []byte) []byte) []byte
- func AppendStringMax255(dst []byte, fn func(dst []byte) []byte) []byte
- func AppendStringUnknownLength(dst []byte, fn func(dst []byte) []byte) []byte
- func AppendTextAppender(dst []byte, s encoding.TextAppender) []byte
- func AppendTimestamp(dst []byte, t time.Time, format ...TsFormat) []byte
- func AppendUint(buf []byte, value uint64) []byte
- func ReadArrayHeader(src []byte, offset int) (length int, newOffset int, err error)
- func ReadBinary(src []byte, offset int) (data []byte, newOffset int, err error)
- func ReadBool(src []byte, offset int) (value bool, newOffset int, err error)
- func ReadFloat(src []byte, offset int) (value float64, newOffset int, err error)
- func ReadInt(src []byte, offset int) (value int64, newOffset int, err error)
- func ReadMapHeader(src []byte, offset int) (length int, newOffset int, err error)
- func ReadNil(src []byte, offset int) (newOffset int, err error)
- func ReadString(src []byte, offset int) (s string, newOffset int, err error)
- func ReadStringCopy(src []byte, offset int) (s string, newOffset int, err error)
- func ReadTimestamp(src []byte, offset int) (t time.Time, newOffset int, err error)
- func ReadUint(src []byte, offset int) (value uint64, newOffset int, err error)
- type Buffer
- func (w Buffer) WriteArrayHeader(n int)
- func (w Buffer) WriteBinary(data []byte)
- func (w Buffer) WriteBool(b bool)
- func (w Buffer) WriteFloat(f float64)
- func (w Buffer) WriteInt(i int64)
- func (w Buffer) WriteMapHeader(n int)
- func (w Buffer) WriteNil()
- func (w Buffer) WriteString(s string)
- func (w Buffer) WriteStringMax255(fn func(dst []byte) []byte)
- func (w Buffer) WriteTimestamp(t time.Time, format ...TsFormat)
- func (w Buffer) WriteUint(i uint64)
- type Float
- type IterPool
- type Iterator
- func (iter *Iterator) Any() any
- func (iter *Iterator) AppendText(b []byte) ([]byte, error)
- func (iter *Iterator) Bin() []byte
- func (iter *Iterator) Bool() bool
- func (iter *Iterator) Buffered() int
- func (iter *Iterator) DebugDump(w io.Writer)
- func (iter *Iterator) Error() error
- func (iter *Iterator) Float() float64
- func (iter *Iterator) Flush()
- func (iter *Iterator) Int() int64
- func (iter *Iterator) Items() int
- func (iter *Iterator) Len() int
- func (iter *Iterator) Next() bool
- func (iter *Iterator) NextExpectedType(expected ...types.Type) (err error)
- func (iter *Iterator) Reader() *ringbuf.LimitedReader
- func (iter *Iterator) Reset(r io.Reader)
- func (iter *Iterator) ResetBytes(b []byte)
- func (iter *Iterator) Rewind()
- func (iter *Iterator) RingReader() *ringbuf.Reader
- func (iter *Iterator) SetManualFlush(v bool)
- func (iter *Iterator) Skip()
- func (iter *Iterator) Str() string
- func (iter *Iterator) Time() time.Time
- func (r *Iterator) TotalRead() int
- func (iter *Iterator) Type() types.Type
- func (iter *Iterator) Uint() uint64
- type Numeric
- type Signed
- type TsFormat
- type Unsigned
- type Value
- func (v Value) AppendBinary(b []byte) ([]byte, error)
- func (v Value) AppendJson(b []byte) ([]byte, error)
- func (v Value) AppendText(b []byte) ([]byte, error)
- func (v Value) Array() iter.Seq[Value]
- func (v Value) Bin() (val []byte)
- func (v Value) Bool() (val bool)
- func (v Value) BytesLen() (l int)
- func (v Value) Float() (val float64)
- func (v Value) Int() (val int64)
- func (v Value) IsZero() bool
- func (v Value) Len() (l int)
- func (v Value) Map() iter.Seq2[Value, Value]
- func (v Value) Str() (val string)
- func (v Value) StrCopy() (val string)
- func (v Value) String() string
- func (v Value) Timestamp() (val time.Time)
- func (v Value) Type() (t types.Type)
- func (v Value) Uint() (val uint64)
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 AppendArrayHeader ¶
AppendArrayHeader appends a MessagePack array header to `dst` based on the number of elements `n`. Returns the updated byte slice.
func AppendBinary ¶
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 ¶
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 ¶
AppendBool appends a boolean value (`true` or `false`) as a MessagePack-encoded boolean to `dst`. Returns the updated byte slice.
func AppendFloat ¶
AppendFloat appends a floating-point value (`f`) as a MessagePack-encoded float32 or float64 to `dst`. Returns the updated byte slice.
func AppendInt ¶
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 ¶
AppendMapHeader appends a map header with `n` key-value pairs to `dst` as a MessagePack-encoded value. Returns the updated byte slice.
func AppendNil ¶
AppendNil appends a nil value to `dst` as a MessagePack-encoded value. Returns the updated byte slice.
func AppendString ¶
AppendString appends the string `s` as a MessagePack-encoded value to `dst`. Returns the updated byte slice.
func AppendStringDynamic ¶
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 ¶
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 ¶
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 ¶
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 ¶
AppendUint appends a MessagePack-encoded unsigned integer to the buffer. It uses the most compact representation based on the value.
func ReadArrayHeader ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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>
Types ¶
type Buffer ¶ added in v0.6.0
func (Buffer) WriteArrayHeader ¶ added in v0.6.0
WriteArrayHeader appends an array header to the buffer.
func (Buffer) WriteBinary ¶ added in v0.6.0
WriteBinary appends binary data to the buffer.
func (Buffer) WriteFloat ¶ added in v0.6.0
WriteFloat64 appends a 64-bit floating-point number to the buffer.
func (Buffer) WriteMapHeader ¶ added in v0.6.0
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
WriteString appends a string to the buffer.
func (Buffer) WriteStringMax255 ¶ added in v0.6.0
WriteStringMax255 appends a string of unknown length (but max 255 characters) to the buffer.
func (Buffer) WriteTimestamp ¶ added in v0.6.0
WriteTimestamp appends a timestamp to the buffer.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Low-level iteration of a MessagePack stream.
func NewIterator ¶
func (*Iterator) AppendText ¶ added in v0.5.0
Appends a text representation of current value to b. Implements encoding.TextAppender.
func (*Iterator) NextExpectedType ¶
func (*Iterator) Reader ¶
func (iter *Iterator) Reader() *ringbuf.LimitedReader
func (*Iterator) ResetBytes ¶
func (*Iterator) RingReader ¶ added in v0.6.0
func (*Iterator) SetManualFlush ¶ added in v0.6.0
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. )
type Value ¶
type Value []byte
func (Value) AppendBinary ¶
AppendBinary implements encoding.BinaryAppender.
func (Value) AppendJson ¶
AppendJson implements fast.JsonAppender.
func (Value) AppendText ¶
AppendText implements encoding.TextAppender.
func (Value) Array ¶
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) BytesLen ¶
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) Map ¶
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
type Writer ¶
type Writer struct {
Buffer
// contains filtered or unexported fields
}
func NewWriter ¶
NewWriter creates a new Writer with the provided io.Writer and initial buffer size.
func (Writer) WriteBinaryReader ¶
func (Writer) WriteCustom ¶
WriteCustom appends custom data to the buffer using a provided function.