Documentation
¶
Overview ¶
Package msgpack is a pure-Go (CGO-free) MRI-faithful MessagePack codec for the Ruby value model. It is the deterministic, interpreter-independent core of Ruby's `msgpack` gem: Pack renders a tree of Ruby values to MessagePack bytes and Unpack parses such bytes back, so Unpack(Pack(x)) round-trips the values Ruby programs serialise — byte-for-byte to the MessagePack spec and the gem, without any Ruby runtime.
Ruby value model ¶
A Ruby value is represented by an [any] drawn from a small, fixed set of Go types so a host (such as go-embedded-ruby) can map its own object graph to and from this package:
Ruby Go (Pack accepts) Go (Unpack returns) ---- ----------------- ------------------- nil nil nil true / false bool bool Integer int, int8..64, uint8..64 int64 or uint64 Float float64, float32 float64 String (UTF-8) string string String (binary) Bin([]byte), []byte Bin (ASCII-8BIT bytes) Symbol Symbol string (symbol→str default) Array []any []any Hash *Map (ordered), map[...]any *Map (insertion order) Time time.Time time.Time (ext type -1) ext *Ext (type, payload) *Ext
Pack also accepts a plain Go map[string]any / map[any]any (sorted by a stable key order for determinism); Unpack always yields an ordered *Map for a map so key order is preserved on round-trip.
Strings carry no encoding tag in Go, so Pack maps a Go string to the MessagePack str family (UTF-8) and a Bin / []byte to the bin family (ASCII-8BIT), matching how the gem distinguishes String encodings.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Pack ¶
Pack serialises a Ruby value to MessagePack bytes, matching MessagePack.pack / Object#to_msgpack from the `msgpack` gem. v is drawn from the package value model (see the package doc); a value outside that model returns an error rather than panicking. Integers use the minimal MessagePack width, strings use the str family (UTF-8) and Bin/[]byte the bin family, and time.Time uses the reserved Time extension (type -1), each byte-for-byte to the gem.
Types ¶
type Bin ¶
type Bin []byte
Bin is a Ruby binary (ASCII-8BIT) String. Pack emits it as the MessagePack bin family and Unpack returns a Bin for the bin family, distinguishing it from a UTF-8 str (which maps to a Go string).
type Ext ¶
Ext is a MessagePack extension object: an application-defined type byte and a raw payload. The reserved Time type is -1; Pack/Unpack handle time.Time via that type directly, so a host uses Ext for any other extension.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is an insertion-ordered Ruby Hash. Unpack returns maps as *Map so key order round-trips; Pack accepts *Map, a plain Go map (emitted in a stable key order), or a []Pair via NewMap.
type Packer ¶
type Packer struct {
// contains filtered or unexported fields
}
Packer is a streaming MessagePack encoder. Successive Write calls append their encodings to an internal buffer; Bytes returns the accumulated output. The zero Packer is ready to use. It mirrors the gem's MessagePack::Packer.
func (*Packer) Bytes ¶
Bytes returns the bytes written so far. The slice aliases the internal buffer until the next Write; copy it if it must outlive further writes.
type Symbol ¶
type Symbol string
Symbol is a Ruby Symbol (`:name`). By default the gem packs a Symbol as a str, so Pack emits Symbol as the str family and Unpack returns a plain string.
type Unpacker ¶
type Unpacker struct {
// contains filtered or unexported fields
}
Unpacker is a streaming MessagePack decoder over a byte buffer. Successive Read calls decode the next object; off advances past consumed bytes. It mirrors the gem's MessagePack::Unpacker.
func NewUnpacker ¶
NewUnpacker returns an Unpacker reading from b. The buffer is not copied.
type Value ¶
type Value = any
Value is the interface satisfied by every Ruby value this package handles. It is purely documentary — the public API uses any — but a host may use it to constrain its own adapters.
func Unpack ¶
Unpack parses MessagePack bytes into a Ruby value, matching MessagePack.unpack / MessagePack.load. Maps come back as an ordered *Map (key order preserved), arrays as []any, the str family as a Go string, the bin family as Bin, the Time extension as time.Time, and any other extension as *Ext. Trailing bytes after the first object are an error (use Unpacker to read a stream).
