fastmsgpack

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: BSD-2-Clause Imports: 9 Imported by: 1

README

fastmsgpack

Go Reference

Fastmsgpack is a Golang msgpack decoder. It is fast, but lacks features you might need. It can be used in combination with other msgpack libraries for e.g. encoding.

Pros:
  • It is very fast to query a list of fields from a nested msgpack structure.
  • It is zero copy for strings and []byte.
  • No reflection usage.
Cons:
  • It can't encode.
  • The return value might contain pointers to the original data, so you can't modify the input data until you're done with the return value.
  • It uses unsafe (to cast []byte to string without copying).
  • It can't deserialize into your structs.
  • It only supports strings as map keys.
  • It decodes all ints as a Go int, including 64 bit ones, so it doesn't work on 32-bit platforms.
  • Using the Resolve API, you can't query fields inside of arrays (yet).

Supported extensions:

  • It supports extension -1 and decodes such values into a time.Time.
  • It supports extension -128 (interned strings). I didn't find any documentation for it, but I've based it on https://github.com/vmihailenco/msgpack. You can pass a dict to the decoder and it will replace indexes into that dict with the strings from the dict.

Returned types

Decode returns an any, which is either int, float32, float64, string, []byte, []any, map[string]any or time.Time.

(*Resolver).Resolve returns a list of such anys, one for each field requested.

Example

r := fastmsgpack.NewResolver([]string{"person.properties.firstName", "person.properties.age"}, nil)
found, err := r.Resolve(data)
firstName, ok := found[0].(string)
age, ok := found[1].(int)

Documentation

Overview

Package fastmsgpack is a msgpack decoder. See the README.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte, dict []string) (_ any, retErr 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.

Types

type Extension

type Extension struct {
	Data []byte
	Type int8
}

type Resolver

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

func NewResolver

func NewResolver(fields []string, dict []string) (*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) Resolve

func (r *Resolver) Resolve(data []byte) (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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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