Documentation
¶
Overview ¶
Package fastmsgpack is a msgpack decoder. See the README.
Index ¶
- Variables
- func Canonical(dst, data []byte, eo EncodeOptions, opts ...DecodeOption) ([]byte, error)
- func Decode(data []byte, opts ...DecodeOption) (any, error)
- func Encode(dst []byte, v any) ([]byte, error)
- func LengthEncode(dst, data []byte) ([]byte, error)
- func Size(data []byte) (int, error)
- func SplitArray(data []byte) ([][]byte, error)
- func SplitMap(data []byte, dict *Dict) ([]string, [][]byte, error)
- type DecodeOption
- type Decoder
- func (d *Decoder) Break() error
- func (d *Decoder) DecodeArrayLen() (int, error)
- func (d *Decoder) DecodeBool() (bool, error)
- func (d *Decoder) DecodeFloat32() (float32, error)
- func (d *Decoder) DecodeFloat64() (float64, error)
- func (d *Decoder) DecodeInt() (int, error)
- func (d *Decoder) DecodeMapLen() (int, error)
- func (d *Decoder) DecodeRaw() ([]byte, error)
- func (d *Decoder) DecodeString() (string, error)
- func (d *Decoder) DecodeTime() (time.Time, error)
- func (d *Decoder) DecodeValue() (any, error)
- func (d *Decoder) PeekType() ValueType
- func (d *Decoder) Skip() error
- type Dict
- type EncodeOptions
- type Extension
- type FlavorBuilder
- type Resolver
- func (r *Resolver) AddArrayResolver(field string, sub *Resolver) (int, error)
- func (r *Resolver) Describe() ([]string, map[string]SubresolverDescription)
- func (r *Resolver) Resolve(data []byte, opts ...DecodeOption) (foundFields []any, retErr error)
- func (r *Resolver) Select(dst, data []byte) (_ []byte, retErr error)
- type SubresolverDescription
- type ValueType
Constants ¶
This section is empty.
Variables ¶
var ErrVoid = internal.ErrVoid
Functions ¶
func Canonical ¶ added in v0.5.0
func Canonical(dst, data []byte, eo EncodeOptions, opts ...DecodeOption) ([]byte, error)
func Decode ¶
func Decode(data []byte, opts ...DecodeOption) (any, error)
Decode the given data (with the optional given dictionary). Any []byte and string in the return value might point into memory from the given data. Don't modify the input data until you're done with the return value. The dictionary is optional and can be nil.
func LengthEncode ¶ added in v0.3.0
LengthEncode injects a length-encoding extension before every map and array to make skipping over it faster. The result is appended to dst and returned. dst can be nil.
func Size ¶ added in v0.3.0
Size returns the number of bytes the first entry in the given msgpack data is.
func SplitArray ¶ added in v0.3.0
SplitArray splits a msgpack array into the msgpack chunks of its components. The returned slices point into the given data.
Types ¶
type DecodeOption ¶ added in v0.3.0
type DecodeOption func(*internal.DecodeOptions)
func WithDict ¶ added in v0.3.0
func WithDict(dict *Dict) DecodeOption
func WithFlavorSelector ¶ added in v0.3.0
func WithFlavorSelector(field, value uint) DecodeOption
func WithInjection ¶ added in v0.4.0
func WithInjection(field uint, msgpack []byte) DecodeOption
WithInjection replaces any encountered extension 20 encoding number $field with the given msgpack data.
type Decoder ¶ added in v0.3.0
type Decoder struct {
// contains filtered or unexported fields
}
Decoder gives a low-level api for stepping through msgpack data. Any []byte and string in return values might point into memory from the given data. Don't modify the input data until you're done with the return value.
func NewDecoder ¶ added in v0.3.0
func NewDecoder(data []byte, opts ...DecodeOption) *Decoder
NewDecoder initializes a new Decoder.
func (*Decoder) Break ¶ added in v0.3.0
Break out of the map or array we're currently in. This can only be called before the last element of the array/map is read, because otherwise you'd break out one level higher.
func (*Decoder) DecodeArrayLen ¶ added in v0.3.0
func (*Decoder) DecodeBool ¶ added in v0.3.0
func (*Decoder) DecodeFloat32 ¶ added in v0.3.0
func (*Decoder) DecodeFloat64 ¶ added in v0.3.0
func (*Decoder) DecodeMapLen ¶ added in v0.3.0
func (*Decoder) DecodeRaw ¶ added in v0.3.0
DecodeRaw decodes the next value enough to know its length and returns the msgpack data for it while skipping over it.
func (*Decoder) DecodeString ¶ added in v0.3.0
func (*Decoder) DecodeValue ¶ added in v0.3.0
DecodeValue decodes the next value in the msgpack data. Return types are: nil, bool, int, float32, float64, string, []byte, time.Time, []any, map[string]any or Extension.
type Dict ¶ added in v0.3.0
type Dict struct { Strings []string // contains filtered or unexported fields }
Dict is a dictionary for smaller msgpack. Instead of putting the string into the binary data, we use the number of the entry in the dictionary. Dictionaries should be the same between encoders and decoders. Adding new entries at the end is safe, as long as all decoders have the new dict before trying to decode newly encoded msgpack.
type EncodeOptions ¶ added in v0.3.0
type Extension ¶
func (Extension) AppendMsgpack ¶ added in v0.3.0
func (Extension) MarshalMsgpack ¶ added in v0.3.0
type FlavorBuilder ¶ added in v0.5.0
type FlavorBuilder struct {
// contains filtered or unexported fields
}
FlavorBuilder helps create an encoded extension 18. The flavor extension is like a switch statement inside your data.
When decoding with WithFlavorSelector(1, 2) we will return x if it decodes the result of NewFlavorBuilder(1).AddCase(2, x).
You are expected to cover all possible cases when building. Using WithFlavorSelector(1, 5) without having called AddCase(5, x) or SetElse() is undefined behavior.
func NewFlavorBuilder ¶ added in v0.5.0
func NewFlavorBuilder(field uint) FlavorBuilder
func (*FlavorBuilder) AddCase ¶ added in v0.5.0
func (f *FlavorBuilder) AddCase(match uint, b []byte)
func (FlavorBuilder) MarshalMsgpack ¶ added in v0.5.0
func (f FlavorBuilder) MarshalMsgpack() ([]byte, error)
func (*FlavorBuilder) SetElse ¶ added in v0.5.0
func (f *FlavorBuilder) SetElse(b []byte)
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
func NewResolver ¶
func NewResolver(fields []string, opts ...DecodeOption) (*Resolver, error)
NewResolver prepares a new resolver. It can be reused for multiple Resolve calls. You can't query the same field twice. You can't even query a child of something else you request (e.g. both "person.properties" and "person.properties.age"). This is the only reason NewResolver might return an error. The dictionary is optional and can be nil.
func (*Resolver) AddArrayResolver ¶ added in v0.3.0
AddArrayResolver allows resolving inside array fields. For example like this pseudocode: `r.AddArrayResolve("person.addresses", NewResolver(["street"]))`. It returns the offset in the return value from Resolve(), which will be of type [][]any. AddArrayResolver can not be called concurrently with itself or Resolve. The dict that was given to the subresolver is not used.
r, err := NewResolver([]string{"person.properties.age"}, nil) sub, err := NewResolver([]string{"street", "number"}, nil) addrOffset, err := r.AddArrayResolver("person.addresses", sub) found, err := r.Resolve(msgpackData) age := found[0] // e.g. 5 addresses := found[addrOffset] // e.g. [][]any{[]any{"Main Street", 1}, []any{"Second Street", 2}}
func (*Resolver) Describe ¶ added in v0.3.0
func (r *Resolver) Describe() ([]string, map[string]SubresolverDescription)
Describe returns which fields and subresolvers were registered to this Resolver. The returned values should not be modified.
func (*Resolver) Resolve ¶
func (r *Resolver) Resolve(data []byte, opts ...DecodeOption) (foundFields []any, retErr error)
Resolve scans through the given data and returns an array with the fields you've requested from this Resolver. Any []byte and string in the return value might point into memory from the given data. Don't modify the input data until you're done with the return value.
type SubresolverDescription ¶ added in v0.3.0
type SubresolverDescription struct { Fields []string Subresolvers map[string]SubresolverDescription Index int }