add minio and file upload
This commit is contained in:
24
vendor/github.com/tinylib/msgp/msgp/advise_linux.go
generated
vendored
Normal file
24
vendor/github.com/tinylib/msgp/msgp/advise_linux.go
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
//go:build linux && !appengine && !tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func adviseRead(mem []byte) {
|
||||
syscall.Madvise(mem, syscall.MADV_SEQUENTIAL|syscall.MADV_WILLNEED)
|
||||
}
|
||||
|
||||
func adviseWrite(mem []byte) {
|
||||
syscall.Madvise(mem, syscall.MADV_SEQUENTIAL)
|
||||
}
|
||||
|
||||
func fallocate(f *os.File, sz int64) error {
|
||||
err := syscall.Fallocate(int(f.Fd()), 0, 0, sz)
|
||||
if err == syscall.ENOTSUP {
|
||||
return f.Truncate(sz)
|
||||
}
|
||||
return err
|
||||
}
|
||||
17
vendor/github.com/tinylib/msgp/msgp/advise_other.go
generated
vendored
Normal file
17
vendor/github.com/tinylib/msgp/msgp/advise_other.go
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
//go:build (!linux && !tinygo && !windows) || appengine
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// TODO: darwin, BSD support
|
||||
|
||||
func adviseRead(mem []byte) {}
|
||||
|
||||
func adviseWrite(mem []byte) {}
|
||||
|
||||
func fallocate(f *os.File, sz int64) error {
|
||||
return f.Truncate(sz)
|
||||
}
|
||||
159
vendor/github.com/tinylib/msgp/msgp/autoshim.go
generated
vendored
Normal file
159
vendor/github.com/tinylib/msgp/msgp/autoshim.go
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
package msgp
|
||||
|
||||
import "strconv"
|
||||
|
||||
// AutoShim provides helper functions for converting between string and
|
||||
// numeric types.
|
||||
type AutoShim struct{}
|
||||
|
||||
// ParseUint converts a string to a uint.
|
||||
func (a AutoShim) ParseUint(s string) (uint, error) {
|
||||
v, err := strconv.ParseUint(s, 10, strconv.IntSize)
|
||||
return uint(v), err
|
||||
}
|
||||
|
||||
// ParseUint8 converts a string to a uint8.
|
||||
func (a AutoShim) ParseUint8(s string) (uint8, error) {
|
||||
v, err := strconv.ParseUint(s, 10, 8)
|
||||
return uint8(v), err
|
||||
}
|
||||
|
||||
// ParseUint16 converts a string to a uint16.
|
||||
func (a AutoShim) ParseUint16(s string) (uint16, error) {
|
||||
v, err := strconv.ParseUint(s, 10, 16)
|
||||
return uint16(v), err
|
||||
}
|
||||
|
||||
// ParseUint32 converts a string to a uint32.
|
||||
func (a AutoShim) ParseUint32(s string) (uint32, error) {
|
||||
v, err := strconv.ParseUint(s, 10, 32)
|
||||
return uint32(v), err
|
||||
}
|
||||
|
||||
// ParseUint64 converts a string to a uint64.
|
||||
func (a AutoShim) ParseUint64(s string) (uint64, error) {
|
||||
v, err := strconv.ParseUint(s, 10, 64)
|
||||
return v, err
|
||||
}
|
||||
|
||||
// ParseInt converts a string to an int.
|
||||
func (a AutoShim) ParseInt(s string) (int, error) {
|
||||
v, err := strconv.ParseInt(s, 10, strconv.IntSize)
|
||||
return int(v), err
|
||||
}
|
||||
|
||||
// ParseInt8 converts a string to an int8.
|
||||
func (a AutoShim) ParseInt8(s string) (int8, error) {
|
||||
v, err := strconv.ParseInt(s, 10, 8)
|
||||
return int8(v), err
|
||||
}
|
||||
|
||||
// ParseInt16 converts a string to an int16.
|
||||
func (a AutoShim) ParseInt16(s string) (int16, error) {
|
||||
v, err := strconv.ParseInt(s, 10, 16)
|
||||
return int16(v), err
|
||||
}
|
||||
|
||||
// ParseInt32 converts a string to an int32.
|
||||
func (a AutoShim) ParseInt32(s string) (int32, error) {
|
||||
v, err := strconv.ParseInt(s, 10, 32)
|
||||
return int32(v), err
|
||||
}
|
||||
|
||||
// ParseInt64 converts a string to an int64.
|
||||
func (a AutoShim) ParseInt64(s string) (int64, error) {
|
||||
v, err := strconv.ParseInt(s, 10, 64)
|
||||
return v, err
|
||||
}
|
||||
|
||||
// ParseBool converts a string to a bool.
|
||||
func (a AutoShim) ParseBool(s string) (bool, error) {
|
||||
return strconv.ParseBool(s)
|
||||
}
|
||||
|
||||
// ParseFloat64 converts a string to a float64.
|
||||
func (a AutoShim) ParseFloat64(s string) (float64, error) {
|
||||
return strconv.ParseFloat(s, 64)
|
||||
}
|
||||
|
||||
// ParseFloat32 converts a string to a float32.
|
||||
func (a AutoShim) ParseFloat32(s string) (float32, error) {
|
||||
v, err := strconv.ParseFloat(s, 32)
|
||||
return float32(v), err
|
||||
}
|
||||
|
||||
// ParseByte converts a string to a byte.
|
||||
func (a AutoShim) ParseByte(s string) (byte, error) {
|
||||
v, err := strconv.ParseUint(s, 10, 8)
|
||||
return byte(v), err
|
||||
}
|
||||
|
||||
// Uint8String returns the string representation of a uint8.
|
||||
func (a AutoShim) Uint8String(v uint8) string {
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
}
|
||||
|
||||
// UintString returns the string representation of a uint.
|
||||
func (a AutoShim) UintString(v uint) string {
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
}
|
||||
|
||||
// Uint16String returns the string representation of a uint16.
|
||||
func (a AutoShim) Uint16String(v uint16) string {
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
}
|
||||
|
||||
// Uint32String returns the string representation of a uint32.
|
||||
func (a AutoShim) Uint32String(v uint32) string {
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
}
|
||||
|
||||
// Uint64String returns the string representation of a uint64.
|
||||
func (a AutoShim) Uint64String(v uint64) string {
|
||||
return strconv.FormatUint(v, 10)
|
||||
}
|
||||
|
||||
// IntString returns the string representation of an int.
|
||||
func (a AutoShim) IntString(v int) string {
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
}
|
||||
|
||||
// Int8String returns the string representation of an int8.
|
||||
func (a AutoShim) Int8String(v int8) string {
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
}
|
||||
|
||||
// Int16String returns the string representation of an int16.
|
||||
func (a AutoShim) Int16String(v int16) string {
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
}
|
||||
|
||||
// Int32String returns the string representation of an int32.
|
||||
func (a AutoShim) Int32String(v int32) string {
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
}
|
||||
|
||||
// Int64String returns the string representation of an int64.
|
||||
func (a AutoShim) Int64String(v int64) string {
|
||||
return strconv.FormatInt(v, 10)
|
||||
}
|
||||
|
||||
// BoolString returns the string representation of a bool.
|
||||
func (a AutoShim) BoolString(v bool) string {
|
||||
return strconv.FormatBool(v)
|
||||
}
|
||||
|
||||
// Float64String returns the string representation of a float64.
|
||||
func (a AutoShim) Float64String(v float64) string {
|
||||
return strconv.FormatFloat(v, 'g', -1, 64)
|
||||
}
|
||||
|
||||
// Float32String returns the string representation of a float32.
|
||||
func (a AutoShim) Float32String(v float32) string {
|
||||
return strconv.FormatFloat(float64(v), 'g', -1, 32)
|
||||
}
|
||||
|
||||
// ByteString returns the string representation of a byte.
|
||||
func (a AutoShim) ByteString(v byte) string {
|
||||
return strconv.FormatUint(uint64(v), 10)
|
||||
}
|
||||
45
vendor/github.com/tinylib/msgp/msgp/circular.go
generated
vendored
Normal file
45
vendor/github.com/tinylib/msgp/msgp/circular.go
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
package msgp
|
||||
|
||||
type timer interface {
|
||||
StartTimer()
|
||||
StopTimer()
|
||||
}
|
||||
|
||||
// EndlessReader is an io.Reader
|
||||
// that loops over the same data
|
||||
// endlessly. It is used for benchmarking.
|
||||
type EndlessReader struct {
|
||||
tb timer
|
||||
data []byte
|
||||
offset int
|
||||
}
|
||||
|
||||
// NewEndlessReader returns a new endless reader.
|
||||
// Buffer b cannot be empty
|
||||
func NewEndlessReader(b []byte, tb timer) *EndlessReader {
|
||||
if len(b) == 0 {
|
||||
panic("EndlessReader cannot be of zero length")
|
||||
}
|
||||
// Double until we reach 4K.
|
||||
for len(b) < 4<<10 {
|
||||
b = append(b, b...)
|
||||
}
|
||||
return &EndlessReader{tb: tb, data: b, offset: 0}
|
||||
}
|
||||
|
||||
// Read implements io.Reader. In practice, it
|
||||
// always returns (len(p), nil), although it
|
||||
// fills the supplied slice while the benchmark
|
||||
// timer is stopped.
|
||||
func (c *EndlessReader) Read(p []byte) (int, error) {
|
||||
var n int
|
||||
l := len(p)
|
||||
m := len(c.data)
|
||||
nn := copy(p[n:], c.data[c.offset:])
|
||||
n += nn
|
||||
for n < l {
|
||||
n += copy(p[n:], c.data[:])
|
||||
}
|
||||
c.offset = (c.offset + l) % m
|
||||
return n, nil
|
||||
}
|
||||
172
vendor/github.com/tinylib/msgp/msgp/defs.go
generated
vendored
Normal file
172
vendor/github.com/tinylib/msgp/msgp/defs.go
generated
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
// This package is the support library for the msgp code generator (http://github.com/tinylib/msgp).
|
||||
//
|
||||
// This package defines the utilites used by the msgp code generator for encoding and decoding MessagePack
|
||||
// from []byte and io.Reader/io.Writer types. Much of this package is devoted to helping the msgp code
|
||||
// generator implement the Marshaler/Unmarshaler and Encodable/Decodable interfaces.
|
||||
//
|
||||
// This package defines four "families" of functions:
|
||||
// - AppendXxxx() appends an object to a []byte in MessagePack encoding.
|
||||
// - ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes.
|
||||
// - (*Writer).WriteXxxx() writes an object to the buffered *Writer type.
|
||||
// - (*Reader).ReadXxxx() reads an object from a buffered *Reader type.
|
||||
//
|
||||
// Once a type has satisfied the `Encodable` and `Decodable` interfaces,
|
||||
// it can be written and read from arbitrary `io.Writer`s and `io.Reader`s using
|
||||
//
|
||||
// msgp.Encode(io.Writer, msgp.Encodable)
|
||||
//
|
||||
// and
|
||||
//
|
||||
// msgp.Decode(io.Reader, msgp.Decodable)
|
||||
//
|
||||
// There are also methods for converting MessagePack to JSON without
|
||||
// an explicit de-serialization step.
|
||||
//
|
||||
// For additional tips, tricks, and gotchas, please visit
|
||||
// the wiki at http://github.com/tinylib/msgp
|
||||
package msgp
|
||||
|
||||
// RT is the runtime interface for all types that can be encoded and decoded.
|
||||
type RT interface {
|
||||
Decodable
|
||||
Encodable
|
||||
Sizer
|
||||
Unmarshaler
|
||||
Marshaler
|
||||
}
|
||||
|
||||
// PtrTo is the runtime interface for all types that can be encoded and decoded.
|
||||
type PtrTo[T any] interface {
|
||||
~*T
|
||||
}
|
||||
|
||||
// RTFor is the runtime interface for all types that can be encoded and decoded.
|
||||
// Use for generic types.
|
||||
type RTFor[T any] interface {
|
||||
PtrTo[T]
|
||||
RT
|
||||
}
|
||||
|
||||
const (
|
||||
last4 = 0x0f
|
||||
first4 = 0xf0
|
||||
last5 = 0x1f
|
||||
first3 = 0xe0
|
||||
last7 = 0x7f
|
||||
|
||||
// recursionLimit is the limit of recursive calls.
|
||||
// This limits the call depth of dynamic code, like Skip and interface conversions.
|
||||
recursionLimit = 100000
|
||||
)
|
||||
|
||||
func isfixint(b byte) bool {
|
||||
return b>>7 == 0
|
||||
}
|
||||
|
||||
func isnfixint(b byte) bool {
|
||||
return b&first3 == mnfixint
|
||||
}
|
||||
|
||||
func isfixmap(b byte) bool {
|
||||
return b&first4 == mfixmap
|
||||
}
|
||||
|
||||
func isfixarray(b byte) bool {
|
||||
return b&first4 == mfixarray
|
||||
}
|
||||
|
||||
func isfixstr(b byte) bool {
|
||||
return b&first3 == mfixstr
|
||||
}
|
||||
|
||||
func wfixint(u uint8) byte {
|
||||
return u & last7
|
||||
}
|
||||
|
||||
func rfixint(b byte) uint8 {
|
||||
return b
|
||||
}
|
||||
|
||||
func wnfixint(i int8) byte {
|
||||
return byte(i) | mnfixint
|
||||
}
|
||||
|
||||
func rnfixint(b byte) int8 {
|
||||
return int8(b)
|
||||
}
|
||||
|
||||
func rfixmap(b byte) uint8 {
|
||||
return b & last4
|
||||
}
|
||||
|
||||
func wfixmap(u uint8) byte {
|
||||
return mfixmap | (u & last4)
|
||||
}
|
||||
|
||||
func rfixstr(b byte) uint8 {
|
||||
return b & last5
|
||||
}
|
||||
|
||||
func wfixstr(u uint8) byte {
|
||||
return (u & last5) | mfixstr
|
||||
}
|
||||
|
||||
func rfixarray(b byte) uint8 {
|
||||
return (b & last4)
|
||||
}
|
||||
|
||||
func wfixarray(u uint8) byte {
|
||||
return (u & last4) | mfixarray
|
||||
}
|
||||
|
||||
// These are all the byte
|
||||
// prefixes defined by the
|
||||
// msgpack standard
|
||||
const (
|
||||
// 0XXXXXXX
|
||||
mfixint uint8 = 0x00
|
||||
|
||||
// 111XXXXX
|
||||
mnfixint uint8 = 0xe0
|
||||
|
||||
// 1000XXXX
|
||||
mfixmap uint8 = 0x80
|
||||
|
||||
// 1001XXXX
|
||||
mfixarray uint8 = 0x90
|
||||
|
||||
// 101XXXXX
|
||||
mfixstr uint8 = 0xa0
|
||||
|
||||
mnil uint8 = 0xc0
|
||||
mfalse uint8 = 0xc2
|
||||
mtrue uint8 = 0xc3
|
||||
mbin8 uint8 = 0xc4
|
||||
mbin16 uint8 = 0xc5
|
||||
mbin32 uint8 = 0xc6
|
||||
mext8 uint8 = 0xc7
|
||||
mext16 uint8 = 0xc8
|
||||
mext32 uint8 = 0xc9
|
||||
mfloat32 uint8 = 0xca
|
||||
mfloat64 uint8 = 0xcb
|
||||
muint8 uint8 = 0xcc
|
||||
muint16 uint8 = 0xcd
|
||||
muint32 uint8 = 0xce
|
||||
muint64 uint8 = 0xcf
|
||||
mint8 uint8 = 0xd0
|
||||
mint16 uint8 = 0xd1
|
||||
mint32 uint8 = 0xd2
|
||||
mint64 uint8 = 0xd3
|
||||
mfixext1 uint8 = 0xd4
|
||||
mfixext2 uint8 = 0xd5
|
||||
mfixext4 uint8 = 0xd6
|
||||
mfixext8 uint8 = 0xd7
|
||||
mfixext16 uint8 = 0xd8
|
||||
mstr8 uint8 = 0xd9
|
||||
mstr16 uint8 = 0xda
|
||||
mstr32 uint8 = 0xdb
|
||||
marray16 uint8 = 0xdc
|
||||
marray32 uint8 = 0xdd
|
||||
mmap16 uint8 = 0xde
|
||||
mmap32 uint8 = 0xdf
|
||||
)
|
||||
242
vendor/github.com/tinylib/msgp/msgp/edit.go
generated
vendored
Normal file
242
vendor/github.com/tinylib/msgp/msgp/edit.go
generated
vendored
Normal file
@@ -0,0 +1,242 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Locate returns a []byte pointing to the field
|
||||
// in a messagepack map with the provided key. (The returned []byte
|
||||
// points to a sub-slice of 'raw'; Locate does no allocations.) If the
|
||||
// key doesn't exist in the map, a zero-length []byte will be returned.
|
||||
func Locate(key string, raw []byte) []byte {
|
||||
s, n := locate(raw, key)
|
||||
return raw[s:n]
|
||||
}
|
||||
|
||||
// Replace takes a key ("key") in a messagepack map ("raw")
|
||||
// and replaces its value with the one provided and returns
|
||||
// the new []byte. The returned []byte may point to the same
|
||||
// memory as "raw". Replace makes no effort to evaluate the validity
|
||||
// of the contents of 'val'. It may use up to the full capacity of 'raw.'
|
||||
// Replace returns 'nil' if the field doesn't exist or if the object in 'raw'
|
||||
// is not a map.
|
||||
func Replace(key string, raw []byte, val []byte) []byte {
|
||||
start, end := locate(raw, key)
|
||||
if start == end {
|
||||
return nil
|
||||
}
|
||||
return replace(raw, start, end, val, true)
|
||||
}
|
||||
|
||||
// CopyReplace works similarly to Replace except that the returned
|
||||
// byte slice does not point to the same memory as 'raw'. CopyReplace
|
||||
// returns 'nil' if the field doesn't exist or 'raw' isn't a map.
|
||||
func CopyReplace(key string, raw []byte, val []byte) []byte {
|
||||
start, end := locate(raw, key)
|
||||
if start == end {
|
||||
return nil
|
||||
}
|
||||
return replace(raw, start, end, val, false)
|
||||
}
|
||||
|
||||
// Remove removes a key-value pair from 'raw'. It returns
|
||||
// 'raw' unchanged if the key didn't exist.
|
||||
func Remove(key string, raw []byte) []byte {
|
||||
start, end := locateKV(raw, key)
|
||||
if start == end {
|
||||
return raw
|
||||
}
|
||||
raw = raw[:start+copy(raw[start:], raw[end:])]
|
||||
return resizeMap(raw, -1)
|
||||
}
|
||||
|
||||
// HasKey returns whether the map in 'raw' has
|
||||
// a field with key 'key'
|
||||
func HasKey(key string, raw []byte) bool {
|
||||
sz, bts, err := ReadMapHeaderBytes(raw)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
var field []byte
|
||||
for range sz {
|
||||
field, bts, err = ReadStringZC(bts)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if UnsafeString(field) == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func replace(raw []byte, start int, end int, val []byte, inplace bool) []byte {
|
||||
ll := end - start // length of segment to replace
|
||||
lv := len(val)
|
||||
|
||||
if inplace {
|
||||
extra := lv - ll
|
||||
|
||||
// fastest case: we're doing
|
||||
// a 1:1 replacement
|
||||
if extra == 0 {
|
||||
copy(raw[start:], val)
|
||||
return raw
|
||||
|
||||
} else if extra < 0 {
|
||||
// 'val' smaller than replaced value
|
||||
// copy in place and shift back
|
||||
|
||||
x := copy(raw[start:], val)
|
||||
y := copy(raw[start+x:], raw[end:])
|
||||
return raw[:start+x+y]
|
||||
|
||||
} else if extra < cap(raw)-len(raw) {
|
||||
// 'val' less than (cap-len) extra bytes
|
||||
// copy in place and shift forward
|
||||
raw = raw[0 : len(raw)+extra]
|
||||
// shift end forward
|
||||
copy(raw[end+extra:], raw[end:])
|
||||
copy(raw[start:], val)
|
||||
return raw
|
||||
}
|
||||
}
|
||||
|
||||
// we have to allocate new space
|
||||
out := make([]byte, len(raw)+len(val)-ll)
|
||||
x := copy(out, raw[:start])
|
||||
y := copy(out[x:], val)
|
||||
copy(out[x+y:], raw[end:])
|
||||
return out
|
||||
}
|
||||
|
||||
// locate does a naive O(n) search for the map key; returns start, end
|
||||
// (returns 0,0 on error)
|
||||
func locate(raw []byte, key string) (start int, end int) {
|
||||
var (
|
||||
sz uint32
|
||||
bts []byte
|
||||
field []byte
|
||||
err error
|
||||
)
|
||||
sz, bts, err = ReadMapHeaderBytes(raw)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// loop and locate field
|
||||
for i := uint32(0); i < sz; i++ {
|
||||
field, bts, err = ReadStringZC(bts)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
if UnsafeString(field) == key {
|
||||
// start location
|
||||
l := len(raw)
|
||||
start = l - len(bts)
|
||||
bts, err = Skip(bts)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
end = l - len(bts)
|
||||
return
|
||||
}
|
||||
bts, err = Skip(bts)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// locate key AND value
|
||||
func locateKV(raw []byte, key string) (start int, end int) {
|
||||
var (
|
||||
sz uint32
|
||||
bts []byte
|
||||
field []byte
|
||||
err error
|
||||
)
|
||||
sz, bts, err = ReadMapHeaderBytes(raw)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
for i := uint32(0); i < sz; i++ {
|
||||
tmp := len(bts)
|
||||
field, bts, err = ReadStringZC(bts)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
if UnsafeString(field) == key {
|
||||
start = len(raw) - tmp
|
||||
bts, err = Skip(bts)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
end = len(raw) - len(bts)
|
||||
return
|
||||
}
|
||||
bts, err = Skip(bts)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// delta is delta on map size
|
||||
func resizeMap(raw []byte, delta int64) []byte {
|
||||
var sz int64
|
||||
switch raw[0] {
|
||||
case mmap16:
|
||||
sz = int64(big.Uint16(raw[1:]))
|
||||
if sz+delta <= math.MaxUint16 {
|
||||
big.PutUint16(raw[1:], uint16(sz+delta))
|
||||
return raw
|
||||
}
|
||||
if cap(raw)-len(raw) >= 2 {
|
||||
raw = raw[0 : len(raw)+2]
|
||||
copy(raw[5:], raw[3:])
|
||||
raw[0] = mmap32
|
||||
big.PutUint32(raw[1:], uint32(sz+delta))
|
||||
return raw
|
||||
}
|
||||
n := make([]byte, 0, len(raw)+5)
|
||||
n = AppendMapHeader(n, uint32(sz+delta))
|
||||
return append(n, raw[3:]...)
|
||||
|
||||
case mmap32:
|
||||
sz = int64(big.Uint32(raw[1:]))
|
||||
big.PutUint32(raw[1:], uint32(sz+delta))
|
||||
return raw
|
||||
|
||||
default:
|
||||
sz = int64(rfixmap(raw[0]))
|
||||
if sz+delta < 16 {
|
||||
raw[0] = wfixmap(uint8(sz + delta))
|
||||
return raw
|
||||
} else if sz+delta <= math.MaxUint16 {
|
||||
if cap(raw)-len(raw) >= 2 {
|
||||
raw = raw[0 : len(raw)+2]
|
||||
copy(raw[3:], raw[1:])
|
||||
raw[0] = mmap16
|
||||
big.PutUint16(raw[1:], uint16(sz+delta))
|
||||
return raw
|
||||
}
|
||||
n := make([]byte, 0, len(raw)+5)
|
||||
n = AppendMapHeader(n, uint32(sz+delta))
|
||||
return append(n, raw[1:]...)
|
||||
}
|
||||
if cap(raw)-len(raw) >= 4 {
|
||||
raw = raw[0 : len(raw)+4]
|
||||
copy(raw[5:], raw[1:])
|
||||
raw[0] = mmap32
|
||||
big.PutUint32(raw[1:], uint32(sz+delta))
|
||||
return raw
|
||||
}
|
||||
n := make([]byte, 0, len(raw)+5)
|
||||
n = AppendMapHeader(n, uint32(sz+delta))
|
||||
return append(n, raw[1:]...)
|
||||
}
|
||||
}
|
||||
128
vendor/github.com/tinylib/msgp/msgp/elsize.go
generated
vendored
Normal file
128
vendor/github.com/tinylib/msgp/msgp/elsize.go
generated
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
package msgp
|
||||
|
||||
func calcBytespec(v byte) bytespec {
|
||||
// single byte values
|
||||
switch v {
|
||||
|
||||
case mnil:
|
||||
return bytespec{size: 1, extra: constsize, typ: NilType}
|
||||
case mfalse:
|
||||
return bytespec{size: 1, extra: constsize, typ: BoolType}
|
||||
case mtrue:
|
||||
return bytespec{size: 1, extra: constsize, typ: BoolType}
|
||||
case mbin8:
|
||||
return bytespec{size: 2, extra: extra8, typ: BinType}
|
||||
case mbin16:
|
||||
return bytespec{size: 3, extra: extra16, typ: BinType}
|
||||
case mbin32:
|
||||
return bytespec{size: 5, extra: extra32, typ: BinType}
|
||||
case mext8:
|
||||
return bytespec{size: 3, extra: extra8, typ: ExtensionType}
|
||||
case mext16:
|
||||
return bytespec{size: 4, extra: extra16, typ: ExtensionType}
|
||||
case mext32:
|
||||
return bytespec{size: 6, extra: extra32, typ: ExtensionType}
|
||||
case mfloat32:
|
||||
return bytespec{size: 5, extra: constsize, typ: Float32Type}
|
||||
case mfloat64:
|
||||
return bytespec{size: 9, extra: constsize, typ: Float64Type}
|
||||
case muint8:
|
||||
return bytespec{size: 2, extra: constsize, typ: UintType}
|
||||
case muint16:
|
||||
return bytespec{size: 3, extra: constsize, typ: UintType}
|
||||
case muint32:
|
||||
return bytespec{size: 5, extra: constsize, typ: UintType}
|
||||
case muint64:
|
||||
return bytespec{size: 9, extra: constsize, typ: UintType}
|
||||
case mint8:
|
||||
return bytespec{size: 2, extra: constsize, typ: IntType}
|
||||
case mint16:
|
||||
return bytespec{size: 3, extra: constsize, typ: IntType}
|
||||
case mint32:
|
||||
return bytespec{size: 5, extra: constsize, typ: IntType}
|
||||
case mint64:
|
||||
return bytespec{size: 9, extra: constsize, typ: IntType}
|
||||
case mfixext1:
|
||||
return bytespec{size: 3, extra: constsize, typ: ExtensionType}
|
||||
case mfixext2:
|
||||
return bytespec{size: 4, extra: constsize, typ: ExtensionType}
|
||||
case mfixext4:
|
||||
return bytespec{size: 6, extra: constsize, typ: ExtensionType}
|
||||
case mfixext8:
|
||||
return bytespec{size: 10, extra: constsize, typ: ExtensionType}
|
||||
case mfixext16:
|
||||
return bytespec{size: 18, extra: constsize, typ: ExtensionType}
|
||||
case mstr8:
|
||||
return bytespec{size: 2, extra: extra8, typ: StrType}
|
||||
case mstr16:
|
||||
return bytespec{size: 3, extra: extra16, typ: StrType}
|
||||
case mstr32:
|
||||
return bytespec{size: 5, extra: extra32, typ: StrType}
|
||||
case marray16:
|
||||
return bytespec{size: 3, extra: array16v, typ: ArrayType}
|
||||
case marray32:
|
||||
return bytespec{size: 5, extra: array32v, typ: ArrayType}
|
||||
case mmap16:
|
||||
return bytespec{size: 3, extra: map16v, typ: MapType}
|
||||
case mmap32:
|
||||
return bytespec{size: 5, extra: map32v, typ: MapType}
|
||||
}
|
||||
|
||||
switch {
|
||||
|
||||
// fixint
|
||||
case v >= mfixint && v < 0x80:
|
||||
return bytespec{size: 1, extra: constsize, typ: IntType}
|
||||
|
||||
// fixstr gets constsize, since the prefix yields the size
|
||||
case v >= mfixstr && v < 0xc0:
|
||||
return bytespec{size: 1 + rfixstr(v), extra: constsize, typ: StrType}
|
||||
|
||||
// fixmap
|
||||
case v >= mfixmap && v < 0x90:
|
||||
return bytespec{size: 1, extra: varmode(2 * rfixmap(v)), typ: MapType}
|
||||
|
||||
// fixarray
|
||||
case v >= mfixarray && v < 0xa0:
|
||||
return bytespec{size: 1, extra: varmode(rfixarray(v)), typ: ArrayType}
|
||||
|
||||
// nfixint
|
||||
case v >= mnfixint && uint16(v) < 0x100:
|
||||
return bytespec{size: 1, extra: constsize, typ: IntType}
|
||||
|
||||
}
|
||||
|
||||
// 0xC1 is unused per the spec and falls through to here,
|
||||
// everything else is covered above
|
||||
|
||||
return bytespec{}
|
||||
}
|
||||
|
||||
func getType(v byte) Type {
|
||||
return getBytespec(v).typ
|
||||
}
|
||||
|
||||
// a valid bytespsec has
|
||||
// non-zero 'size' and
|
||||
// non-zero 'typ'
|
||||
type bytespec struct {
|
||||
size uint8 // prefix size information
|
||||
extra varmode // extra size information
|
||||
typ Type // type
|
||||
_ byte // makes bytespec 4 bytes (yes, this matters)
|
||||
}
|
||||
|
||||
// size mode
|
||||
// if positive, # elements for composites
|
||||
type varmode int8
|
||||
|
||||
const (
|
||||
constsize varmode = 0 // constant size (size bytes + uint8(varmode) objects)
|
||||
extra8 varmode = -1 // has uint8(p[1]) extra bytes
|
||||
extra16 varmode = -2 // has be16(p[1:]) extra bytes
|
||||
extra32 varmode = -3 // has be32(p[1:]) extra bytes
|
||||
map16v varmode = -4 // use map16
|
||||
map32v varmode = -5 // use map32
|
||||
array16v varmode = -6 // use array16
|
||||
array32v varmode = -7 // use array32
|
||||
)
|
||||
20
vendor/github.com/tinylib/msgp/msgp/elsize_default.go
generated
vendored
Normal file
20
vendor/github.com/tinylib/msgp/msgp/elsize_default.go
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
//go:build !tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
// size of every object on the wire,
|
||||
// plus type information. gives us
|
||||
// constant-time type information
|
||||
// for traversing composite objects.
|
||||
var sizes [256]bytespec
|
||||
|
||||
func init() {
|
||||
for i := range 256 {
|
||||
sizes[i] = calcBytespec(byte(i))
|
||||
}
|
||||
}
|
||||
|
||||
// getBytespec gets inlined to a simple array index
|
||||
func getBytespec(v byte) bytespec {
|
||||
return sizes[v]
|
||||
}
|
||||
12
vendor/github.com/tinylib/msgp/msgp/elsize_tinygo.go
generated
vendored
Normal file
12
vendor/github.com/tinylib/msgp/msgp/elsize_tinygo.go
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
//go:build tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
// for tinygo, getBytespec just calls calcBytespec
|
||||
// a simple/slow function with a switch statement -
|
||||
// doesn't require any heap alloc, moves the space
|
||||
// requirements into code instad of ram
|
||||
|
||||
func getBytespec(v byte) bytespec {
|
||||
return calcBytespec(v)
|
||||
}
|
||||
402
vendor/github.com/tinylib/msgp/msgp/errors.go
generated
vendored
Normal file
402
vendor/github.com/tinylib/msgp/msgp/errors.go
generated
vendored
Normal file
@@ -0,0 +1,402 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const resumableDefault = false
|
||||
|
||||
var (
|
||||
// ErrShortBytes is returned when the
|
||||
// slice being decoded is too short to
|
||||
// contain the contents of the message
|
||||
ErrShortBytes error = errShort{}
|
||||
|
||||
// ErrRecursion is returned when the maximum recursion limit is reached for an operation.
|
||||
// This should only realistically be seen on adversarial data trying to exhaust the stack.
|
||||
ErrRecursion error = errRecursion{}
|
||||
|
||||
// ErrLimitExceeded is returned when a set limit is exceeded.
|
||||
// Limits can be set on the Reader to prevent excessive memory usage by adversarial data.
|
||||
ErrLimitExceeded error = errLimitExceeded{}
|
||||
|
||||
// this error is only returned
|
||||
// if we reach code that should
|
||||
// be unreachable
|
||||
fatal error = errFatal{}
|
||||
)
|
||||
|
||||
// Error is the interface satisfied
|
||||
// by all of the errors that originate
|
||||
// from this package.
|
||||
type Error interface {
|
||||
error
|
||||
|
||||
// Resumable returns whether
|
||||
// or not the error means that
|
||||
// the stream of data is malformed
|
||||
// and the information is unrecoverable.
|
||||
Resumable() bool
|
||||
}
|
||||
|
||||
// contextError allows msgp Error instances to be enhanced with additional
|
||||
// context about their origin.
|
||||
type contextError interface {
|
||||
Error
|
||||
|
||||
// withContext must not modify the error instance - it must clone and
|
||||
// return a new error with the context added.
|
||||
withContext(ctx string) error
|
||||
}
|
||||
|
||||
// Cause returns the underlying cause of an error that has been wrapped
|
||||
// with additional context.
|
||||
func Cause(e error) error {
|
||||
out := e
|
||||
if e, ok := e.(errWrapped); ok && e.cause != nil {
|
||||
out = e.cause
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Resumable returns whether or not the error means that the stream of data is
|
||||
// malformed and the information is unrecoverable.
|
||||
func Resumable(e error) bool {
|
||||
if e, ok := e.(Error); ok {
|
||||
return e.Resumable()
|
||||
}
|
||||
return resumableDefault
|
||||
}
|
||||
|
||||
// WrapError wraps an error with additional context that allows the part of the
|
||||
// serialized type that caused the problem to be identified. Underlying errors
|
||||
// can be retrieved using Cause()
|
||||
//
|
||||
// The input error is not modified - a new error should be returned.
|
||||
//
|
||||
// ErrShortBytes is not wrapped with any context due to backward compatibility
|
||||
// issues with the public API.
|
||||
func WrapError(err error, ctx ...any) error {
|
||||
switch e := err.(type) {
|
||||
case errShort:
|
||||
return e
|
||||
case contextError:
|
||||
return e.withContext(ctxString(ctx))
|
||||
default:
|
||||
return errWrapped{cause: err, ctx: ctxString(ctx)}
|
||||
}
|
||||
}
|
||||
|
||||
func addCtx(ctx, add string) string {
|
||||
if ctx != "" {
|
||||
return add + "/" + ctx
|
||||
} else {
|
||||
return add
|
||||
}
|
||||
}
|
||||
|
||||
// errWrapped allows arbitrary errors passed to WrapError to be enhanced with
|
||||
// context and unwrapped with Cause()
|
||||
type errWrapped struct {
|
||||
cause error
|
||||
ctx string
|
||||
}
|
||||
|
||||
func (e errWrapped) Error() string {
|
||||
if e.ctx != "" {
|
||||
return e.cause.Error() + " at " + e.ctx
|
||||
} else {
|
||||
return e.cause.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func (e errWrapped) Resumable() bool {
|
||||
if e, ok := e.cause.(Error); ok {
|
||||
return e.Resumable()
|
||||
}
|
||||
return resumableDefault
|
||||
}
|
||||
|
||||
// Unwrap returns the cause.
|
||||
func (e errWrapped) Unwrap() error { return e.cause }
|
||||
|
||||
type errShort struct{}
|
||||
|
||||
func (e errShort) Error() string { return "msgp: too few bytes left to read object" }
|
||||
func (e errShort) Resumable() bool { return false }
|
||||
|
||||
type errFatal struct {
|
||||
ctx string
|
||||
}
|
||||
|
||||
func (f errFatal) Error() string {
|
||||
out := "msgp: fatal decoding error (unreachable code)"
|
||||
if f.ctx != "" {
|
||||
out += " at " + f.ctx
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (f errFatal) Resumable() bool { return false }
|
||||
|
||||
func (f errFatal) withContext(ctx string) error { f.ctx = addCtx(f.ctx, ctx); return f }
|
||||
|
||||
type errRecursion struct{}
|
||||
|
||||
func (e errRecursion) Error() string { return "msgp: recursion limit reached" }
|
||||
func (e errRecursion) Resumable() bool { return false }
|
||||
|
||||
type errLimitExceeded struct{}
|
||||
|
||||
func (e errLimitExceeded) Error() string { return "msgp: configured reader limit exceeded" }
|
||||
func (e errLimitExceeded) Resumable() bool { return false }
|
||||
|
||||
// ArrayError is an error returned
|
||||
// when decoding a fix-sized array
|
||||
// of the wrong size
|
||||
type ArrayError struct {
|
||||
Wanted uint32
|
||||
Got uint32
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (a ArrayError) Error() string {
|
||||
out := "msgp: wanted array of size " + strconv.Itoa(int(a.Wanted)) + "; got " + strconv.Itoa(int(a.Got))
|
||||
if a.ctx != "" {
|
||||
out += " at " + a.ctx
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Resumable is always 'true' for ArrayErrors
|
||||
func (a ArrayError) Resumable() bool { return true }
|
||||
|
||||
func (a ArrayError) withContext(ctx string) error { a.ctx = addCtx(a.ctx, ctx); return a }
|
||||
|
||||
// IntOverflow is returned when a call
|
||||
// would downcast an integer to a type
|
||||
// with too few bits to hold its value.
|
||||
type IntOverflow struct {
|
||||
Value int64 // the value of the integer
|
||||
FailedBitsize int // the bit size that the int64 could not fit into
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (i IntOverflow) Error() string {
|
||||
str := "msgp: " + strconv.FormatInt(i.Value, 10) + " overflows int" + strconv.Itoa(i.FailedBitsize)
|
||||
if i.ctx != "" {
|
||||
str += " at " + i.ctx
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// Resumable is always 'true' for overflows
|
||||
func (i IntOverflow) Resumable() bool { return true }
|
||||
|
||||
func (i IntOverflow) withContext(ctx string) error { i.ctx = addCtx(i.ctx, ctx); return i }
|
||||
|
||||
// UintOverflow is returned when a call
|
||||
// would downcast an unsigned integer to a type
|
||||
// with too few bits to hold its value
|
||||
type UintOverflow struct {
|
||||
Value uint64 // value of the uint
|
||||
FailedBitsize int // the bit size that couldn't fit the value
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (u UintOverflow) Error() string {
|
||||
str := "msgp: " + strconv.FormatUint(u.Value, 10) + " overflows uint" + strconv.Itoa(u.FailedBitsize)
|
||||
if u.ctx != "" {
|
||||
str += " at " + u.ctx
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// Resumable is always 'true' for overflows
|
||||
func (u UintOverflow) Resumable() bool { return true }
|
||||
|
||||
func (u UintOverflow) withContext(ctx string) error { u.ctx = addCtx(u.ctx, ctx); return u }
|
||||
|
||||
// InvalidTimestamp is returned when an invalid timestamp is encountered
|
||||
type InvalidTimestamp struct {
|
||||
Nanos int64 // value of the nano, if invalid
|
||||
FieldLength int // Unexpected field length.
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (u InvalidTimestamp) Error() (str string) {
|
||||
if u.Nanos > 0 {
|
||||
str = "msgp: timestamp nanosecond field value " + strconv.FormatInt(u.Nanos, 10) + " exceeds maximum allows of 999999999"
|
||||
} else if u.FieldLength >= 0 {
|
||||
str = "msgp: invalid timestamp field length " + strconv.FormatInt(int64(u.FieldLength), 10) + " - must be 4, 8 or 12"
|
||||
}
|
||||
if u.ctx != "" {
|
||||
str += " at " + u.ctx
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// Resumable is always 'true' for overflows
|
||||
func (u InvalidTimestamp) Resumable() bool { return true }
|
||||
|
||||
func (u InvalidTimestamp) withContext(ctx string) error { u.ctx = addCtx(u.ctx, ctx); return u }
|
||||
|
||||
// UintBelowZero is returned when a call
|
||||
// would cast a signed integer below zero
|
||||
// to an unsigned integer.
|
||||
type UintBelowZero struct {
|
||||
Value int64 // value of the incoming int
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (u UintBelowZero) Error() string {
|
||||
str := "msgp: attempted to cast int " + strconv.FormatInt(u.Value, 10) + " to unsigned"
|
||||
if u.ctx != "" {
|
||||
str += " at " + u.ctx
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// Resumable is always 'true' for overflows
|
||||
func (u UintBelowZero) Resumable() bool { return true }
|
||||
|
||||
func (u UintBelowZero) withContext(ctx string) error {
|
||||
u.ctx = ctx
|
||||
return u
|
||||
}
|
||||
|
||||
// A TypeError is returned when a particular
|
||||
// decoding method is unsuitable for decoding
|
||||
// a particular MessagePack value.
|
||||
type TypeError struct {
|
||||
Method Type // Type expected by method
|
||||
Encoded Type // Type actually encoded
|
||||
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (t TypeError) Error() string {
|
||||
out := "msgp: attempted to decode type " + quoteStr(t.Encoded.String()) + " with method for " + quoteStr(t.Method.String())
|
||||
if t.ctx != "" {
|
||||
out += " at " + t.ctx
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Resumable returns 'true' for TypeErrors
|
||||
func (t TypeError) Resumable() bool { return true }
|
||||
|
||||
func (t TypeError) withContext(ctx string) error { t.ctx = addCtx(t.ctx, ctx); return t }
|
||||
|
||||
// returns either InvalidPrefixError or
|
||||
// TypeError depending on whether or not
|
||||
// the prefix is recognized
|
||||
func badPrefix(want Type, lead byte) error {
|
||||
t := getType(lead)
|
||||
if t == InvalidType {
|
||||
return InvalidPrefixError(lead)
|
||||
}
|
||||
return TypeError{Method: want, Encoded: t}
|
||||
}
|
||||
|
||||
// InvalidPrefixError is returned when a bad encoding
|
||||
// uses a prefix that is not recognized in the MessagePack standard.
|
||||
// This kind of error is unrecoverable.
|
||||
type InvalidPrefixError byte
|
||||
|
||||
// Error implements the error interface
|
||||
func (i InvalidPrefixError) Error() string {
|
||||
return "msgp: unrecognized type prefix 0x" + strconv.FormatInt(int64(i), 16)
|
||||
}
|
||||
|
||||
// Resumable returns 'false' for InvalidPrefixErrors
|
||||
func (i InvalidPrefixError) Resumable() bool { return false }
|
||||
|
||||
// ErrUnsupportedType is returned
|
||||
// when a bad argument is supplied
|
||||
// to a function that takes `interface{}`.
|
||||
type ErrUnsupportedType struct {
|
||||
T reflect.Type
|
||||
|
||||
ctx string
|
||||
}
|
||||
|
||||
// Error implements error
|
||||
func (e *ErrUnsupportedType) Error() string {
|
||||
out := "msgp: type " + quoteStr(e.T.String()) + " not supported"
|
||||
if e.ctx != "" {
|
||||
out += " at " + e.ctx
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Resumable returns 'true' for ErrUnsupportedType
|
||||
func (e *ErrUnsupportedType) Resumable() bool { return true }
|
||||
|
||||
func (e *ErrUnsupportedType) withContext(ctx string) error {
|
||||
o := *e
|
||||
o.ctx = addCtx(o.ctx, ctx)
|
||||
return &o
|
||||
}
|
||||
|
||||
// simpleQuoteStr is a simplified version of strconv.Quote for TinyGo,
|
||||
// which takes up a lot less code space by escaping all non-ASCII
|
||||
// (UTF-8) bytes with \x. Saves about 4k of code size
|
||||
// (unicode tables, needed for IsPrint(), are big).
|
||||
// It lives in errors.go just so we can test it in errors_test.go
|
||||
func simpleQuoteStr(s string) string {
|
||||
const (
|
||||
lowerhex = "0123456789abcdef"
|
||||
)
|
||||
|
||||
sb := make([]byte, 0, len(s)+2)
|
||||
|
||||
sb = append(sb, `"`...)
|
||||
|
||||
l: // loop through string bytes (not UTF-8 characters)
|
||||
for i := 0; i < len(s); i++ {
|
||||
b := s[i]
|
||||
// specific escape chars
|
||||
switch b {
|
||||
case '\\':
|
||||
sb = append(sb, `\\`...)
|
||||
case '"':
|
||||
sb = append(sb, `\"`...)
|
||||
case '\a':
|
||||
sb = append(sb, `\a`...)
|
||||
case '\b':
|
||||
sb = append(sb, `\b`...)
|
||||
case '\f':
|
||||
sb = append(sb, `\f`...)
|
||||
case '\n':
|
||||
sb = append(sb, `\n`...)
|
||||
case '\r':
|
||||
sb = append(sb, `\r`...)
|
||||
case '\t':
|
||||
sb = append(sb, `\t`...)
|
||||
case '\v':
|
||||
sb = append(sb, `\v`...)
|
||||
default:
|
||||
// no escaping needed (printable ASCII)
|
||||
if b >= 0x20 && b <= 0x7E {
|
||||
sb = append(sb, b)
|
||||
continue l
|
||||
}
|
||||
// anything else is \x
|
||||
sb = append(sb, `\x`...)
|
||||
sb = append(sb, lowerhex[b>>4])
|
||||
sb = append(sb, lowerhex[b&0xF])
|
||||
continue l
|
||||
}
|
||||
}
|
||||
|
||||
sb = append(sb, `"`...)
|
||||
return string(sb)
|
||||
}
|
||||
24
vendor/github.com/tinylib/msgp/msgp/errors_default.go
generated
vendored
Normal file
24
vendor/github.com/tinylib/msgp/msgp/errors_default.go
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
//go:build !tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ctxString converts the incoming interface{} slice into a single string.
|
||||
func ctxString(ctx []any) string {
|
||||
out := ""
|
||||
for idx, cv := range ctx {
|
||||
if idx > 0 {
|
||||
out += "/"
|
||||
}
|
||||
out += fmt.Sprintf("%v", cv)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func quoteStr(s string) string {
|
||||
return strconv.Quote(s)
|
||||
}
|
||||
41
vendor/github.com/tinylib/msgp/msgp/errors_tinygo.go
generated
vendored
Normal file
41
vendor/github.com/tinylib/msgp/msgp/errors_tinygo.go
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
//go:build tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// ctxString converts the incoming interface{} slice into a single string,
|
||||
// without using fmt under tinygo
|
||||
func ctxString(ctx []interface{}) string {
|
||||
out := ""
|
||||
for idx, cv := range ctx {
|
||||
if idx > 0 {
|
||||
out += "/"
|
||||
}
|
||||
out += ifToStr(cv)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type stringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
func ifToStr(i interface{}) string {
|
||||
switch v := i.(type) {
|
||||
case stringer:
|
||||
return v.String()
|
||||
case error:
|
||||
return v.Error()
|
||||
case string:
|
||||
return v
|
||||
default:
|
||||
return reflect.ValueOf(i).String()
|
||||
}
|
||||
}
|
||||
|
||||
func quoteStr(s string) string {
|
||||
return simpleQuoteStr(s)
|
||||
}
|
||||
567
vendor/github.com/tinylib/msgp/msgp/extension.go
generated
vendored
Normal file
567
vendor/github.com/tinylib/msgp/msgp/extension.go
generated
vendored
Normal file
@@ -0,0 +1,567 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
// Complex64Extension is the extension number used for complex64
|
||||
Complex64Extension = 3
|
||||
|
||||
// Complex128Extension is the extension number used for complex128
|
||||
Complex128Extension = 4
|
||||
|
||||
// TimeExtension is the extension number used for time.Time
|
||||
TimeExtension = 5
|
||||
|
||||
// MsgTimeExtension is the extension number for timestamps as defined in
|
||||
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
|
||||
MsgTimeExtension = -1
|
||||
)
|
||||
|
||||
// msgTimeExtension is a painful workaround to avoid "constant -1 overflows byte".
|
||||
var msgTimeExtension = int8(MsgTimeExtension)
|
||||
|
||||
// our extensions live here
|
||||
var extensionReg = make(map[int8]func() Extension)
|
||||
|
||||
// RegisterExtension registers extensions so that they
|
||||
// can be initialized and returned by methods that
|
||||
// decode `interface{}` values. This should only
|
||||
// be called during initialization. f() should return
|
||||
// a newly-initialized zero value of the extension. Keep in
|
||||
// mind that extensions 3, 4, and 5 are reserved for
|
||||
// complex64, complex128, and time.Time, respectively,
|
||||
// and that MessagePack reserves extension types from -127 to -1.
|
||||
//
|
||||
// For example, if you wanted to register a user-defined struct:
|
||||
//
|
||||
// msgp.RegisterExtension(10, func() msgp.Extension { &MyExtension{} })
|
||||
//
|
||||
// RegisterExtension will panic if you call it multiple times
|
||||
// with the same 'typ' argument, or if you use a reserved
|
||||
// type (3, 4, or 5).
|
||||
func RegisterExtension(typ int8, f func() Extension) {
|
||||
switch typ {
|
||||
case Complex64Extension, Complex128Extension, TimeExtension:
|
||||
panic(errors.New("msgp: forbidden extension type: " + strconv.Itoa(int(typ))))
|
||||
}
|
||||
if _, ok := extensionReg[typ]; ok {
|
||||
panic(errors.New("msgp: RegisterExtension() called with typ " + strconv.Itoa(int(typ)) + " more than once"))
|
||||
}
|
||||
extensionReg[typ] = f
|
||||
}
|
||||
|
||||
// ExtensionTypeError is an error type returned
|
||||
// when there is a mis-match between an extension type
|
||||
// and the type encoded on the wire
|
||||
type ExtensionTypeError struct {
|
||||
Got int8
|
||||
Want int8
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (e ExtensionTypeError) Error() string {
|
||||
return "msgp: error decoding extension: wanted type " + strconv.Itoa(int(e.Want)) + "; got type " + strconv.Itoa(int(e.Got))
|
||||
}
|
||||
|
||||
// Resumable returns 'true' for ExtensionTypeErrors
|
||||
func (e ExtensionTypeError) Resumable() bool { return true }
|
||||
|
||||
func errExt(got int8, wanted int8) error {
|
||||
return ExtensionTypeError{Got: got, Want: wanted}
|
||||
}
|
||||
|
||||
// Extension is the interface fulfilled
|
||||
// by types that want to define their
|
||||
// own binary encoding.
|
||||
type Extension interface {
|
||||
// ExtensionType should return
|
||||
// a int8 that identifies the concrete
|
||||
// type of the extension. (Types <0 are
|
||||
// officially reserved by the MessagePack
|
||||
// specifications.)
|
||||
ExtensionType() int8
|
||||
|
||||
// Len should return the length
|
||||
// of the data to be encoded
|
||||
Len() int
|
||||
|
||||
// MarshalBinaryTo should copy
|
||||
// the data into the supplied slice,
|
||||
// assuming that the slice has length Len()
|
||||
MarshalBinaryTo([]byte) error
|
||||
|
||||
UnmarshalBinary([]byte) error
|
||||
}
|
||||
|
||||
// RawExtension implements the Extension interface
|
||||
type RawExtension struct {
|
||||
Data []byte
|
||||
Type int8
|
||||
}
|
||||
|
||||
// ExtensionType implements Extension.ExtensionType, and returns r.Type
|
||||
func (r *RawExtension) ExtensionType() int8 { return r.Type }
|
||||
|
||||
// Len implements Extension.Len, and returns len(r.Data)
|
||||
func (r *RawExtension) Len() int { return len(r.Data) }
|
||||
|
||||
// MarshalBinaryTo implements Extension.MarshalBinaryTo,
|
||||
// and returns a copy of r.Data
|
||||
func (r *RawExtension) MarshalBinaryTo(d []byte) error {
|
||||
copy(d, r.Data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements Extension.UnmarshalBinary,
|
||||
// and sets r.Data to the contents of the provided slice
|
||||
func (r *RawExtension) UnmarshalBinary(b []byte) error {
|
||||
if cap(r.Data) >= len(b) {
|
||||
r.Data = r.Data[0:len(b)]
|
||||
} else {
|
||||
r.Data = make([]byte, len(b))
|
||||
}
|
||||
copy(r.Data, b)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mw *Writer) writeExtensionHeader(length int, extType int8) error {
|
||||
switch length {
|
||||
case 0:
|
||||
o, err := mw.require(3)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mext8
|
||||
mw.buf[o+1] = 0
|
||||
mw.buf[o+2] = byte(extType)
|
||||
case 1:
|
||||
o, err := mw.require(2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext1
|
||||
mw.buf[o+1] = byte(extType)
|
||||
case 2:
|
||||
o, err := mw.require(2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext2
|
||||
mw.buf[o+1] = byte(extType)
|
||||
case 4:
|
||||
o, err := mw.require(2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext4
|
||||
mw.buf[o+1] = byte(extType)
|
||||
case 8:
|
||||
o, err := mw.require(2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext8
|
||||
mw.buf[o+1] = byte(extType)
|
||||
case 16:
|
||||
o, err := mw.require(2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext16
|
||||
mw.buf[o+1] = byte(extType)
|
||||
default:
|
||||
switch {
|
||||
case length < math.MaxUint8:
|
||||
o, err := mw.require(3)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mext8
|
||||
mw.buf[o+1] = byte(length)
|
||||
mw.buf[o+2] = byte(extType)
|
||||
case length < math.MaxUint16:
|
||||
o, err := mw.require(4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mext16
|
||||
big.PutUint16(mw.buf[o+1:], uint16(length))
|
||||
mw.buf[o+3] = byte(extType)
|
||||
default:
|
||||
o, err := mw.require(6)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mext32
|
||||
big.PutUint32(mw.buf[o+1:], uint32(length))
|
||||
mw.buf[o+5] = byte(extType)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteExtension writes an extension type to the writer
|
||||
func (mw *Writer) WriteExtension(e Extension) error {
|
||||
length := e.Len()
|
||||
|
||||
err := mw.writeExtensionHeader(length, e.ExtensionType())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// we can only write directly to the
|
||||
// buffer if we're sure that it
|
||||
// fits the object
|
||||
if length <= mw.bufsize() {
|
||||
o, err := mw.require(length)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return e.MarshalBinaryTo(mw.buf[o:])
|
||||
}
|
||||
// here we create a new buffer
|
||||
// just large enough for the body
|
||||
// and save it as the write buffer
|
||||
err = mw.flush()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buf := make([]byte, length)
|
||||
err = e.MarshalBinaryTo(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf = buf
|
||||
mw.wloc = length
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteExtensionRaw writes an extension type to the writer
|
||||
func (mw *Writer) WriteExtensionRaw(extType int8, payload []byte) error {
|
||||
if err := mw.writeExtensionHeader(len(payload), extType); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// instead of using mw.Write(), we'll copy the data through the internal
|
||||
// buffer, otherwise the payload would be moved to the heap
|
||||
// (meaning we can use stack-allocated buffers with zero allocations)
|
||||
for len(payload) > 0 {
|
||||
chunkSize := mw.avail()
|
||||
if chunkSize == 0 {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
chunkSize = mw.avail()
|
||||
}
|
||||
if chunkSize > len(payload) {
|
||||
chunkSize = len(payload)
|
||||
}
|
||||
|
||||
mw.wloc += copy(mw.buf[mw.wloc:], payload[:chunkSize])
|
||||
payload = payload[chunkSize:]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// peek at the extension type, assuming the next
|
||||
// kind to be read is Extension
|
||||
func (m *Reader) peekExtensionType() (int8, error) {
|
||||
_, _, extType, err := m.peekExtensionHeader()
|
||||
|
||||
return extType, err
|
||||
}
|
||||
|
||||
// peekExtension peeks at the extension encoding type
|
||||
// (must guarantee at least 1 byte in 'b')
|
||||
func peekExtension(b []byte) (int8, error) {
|
||||
spec := getBytespec(b[0])
|
||||
size := spec.size
|
||||
if spec.typ != ExtensionType {
|
||||
return 0, badPrefix(ExtensionType, b[0])
|
||||
}
|
||||
if len(b) < int(size) {
|
||||
return 0, ErrShortBytes
|
||||
}
|
||||
// for fixed extensions,
|
||||
// the type information is in
|
||||
// the second byte
|
||||
if spec.extra == constsize {
|
||||
return int8(b[1]), nil
|
||||
}
|
||||
// otherwise, it's in the last
|
||||
// part of the prefix
|
||||
return int8(b[size-1]), nil
|
||||
}
|
||||
|
||||
func (m *Reader) peekExtensionHeader() (offset int, length int, extType int8, err error) {
|
||||
var p []byte
|
||||
p, err = m.R.Peek(2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
offset = 2
|
||||
|
||||
lead := p[0]
|
||||
switch lead {
|
||||
case mfixext1:
|
||||
extType = int8(p[1])
|
||||
length = 1
|
||||
return
|
||||
|
||||
case mfixext2:
|
||||
extType = int8(p[1])
|
||||
length = 2
|
||||
return
|
||||
|
||||
case mfixext4:
|
||||
extType = int8(p[1])
|
||||
length = 4
|
||||
return
|
||||
|
||||
case mfixext8:
|
||||
extType = int8(p[1])
|
||||
length = 8
|
||||
return
|
||||
|
||||
case mfixext16:
|
||||
extType = int8(p[1])
|
||||
length = 16
|
||||
return
|
||||
|
||||
case mext8:
|
||||
p, err = m.R.Peek(3)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
offset = 3
|
||||
extType = int8(p[2])
|
||||
length = int(p[1])
|
||||
|
||||
case mext16:
|
||||
p, err = m.R.Peek(4)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
offset = 4
|
||||
extType = int8(p[3])
|
||||
length = int(big.Uint16(p[1:]))
|
||||
|
||||
case mext32:
|
||||
p, err = m.R.Peek(6)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
offset = 6
|
||||
extType = int8(p[5])
|
||||
length = int(big.Uint32(p[1:]))
|
||||
|
||||
default:
|
||||
err = badPrefix(ExtensionType, lead)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ReadExtension reads the next object from the reader
|
||||
// as an extension. ReadExtension will fail if the next
|
||||
// object in the stream is not an extension, or if
|
||||
// e.Type() is not the same as the wire type.
|
||||
func (m *Reader) ReadExtension(e Extension) error {
|
||||
offset, length, extType, err := m.peekExtensionHeader()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if expectedType := e.ExtensionType(); extType != expectedType {
|
||||
return errExt(extType, expectedType)
|
||||
}
|
||||
if uint32(length) > m.GetMaxElements() {
|
||||
return ErrLimitExceeded
|
||||
}
|
||||
|
||||
p, err := m.R.Peek(offset + length)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = e.UnmarshalBinary(p[offset:])
|
||||
if err == nil {
|
||||
// consume the peeked bytes
|
||||
_, err = m.R.Skip(offset + length)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadExtensionRaw reads the next object from the reader
|
||||
// as an extension. The returned slice is only
|
||||
// valid until the next *Reader method call.
|
||||
func (m *Reader) ReadExtensionRaw() (int8, []byte, error) {
|
||||
offset, length, extType, err := m.peekExtensionHeader()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
if uint32(length) > m.GetMaxElements() {
|
||||
return 0, nil, ErrLimitExceeded
|
||||
}
|
||||
|
||||
payload, err := m.R.Next(offset + length)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
return extType, payload[offset:], nil
|
||||
}
|
||||
|
||||
// AppendExtension appends a MessagePack extension to the provided slice
|
||||
func AppendExtension(b []byte, e Extension) ([]byte, error) {
|
||||
l := e.Len()
|
||||
var o []byte
|
||||
var n int
|
||||
switch l {
|
||||
case 0:
|
||||
o, n = ensure(b, 3)
|
||||
o[n] = mext8
|
||||
o[n+1] = 0
|
||||
o[n+2] = byte(e.ExtensionType())
|
||||
return o[:n+3], nil
|
||||
case 1:
|
||||
o, n = ensure(b, 3)
|
||||
o[n] = mfixext1
|
||||
o[n+1] = byte(e.ExtensionType())
|
||||
n += 2
|
||||
case 2:
|
||||
o, n = ensure(b, 4)
|
||||
o[n] = mfixext2
|
||||
o[n+1] = byte(e.ExtensionType())
|
||||
n += 2
|
||||
case 4:
|
||||
o, n = ensure(b, 6)
|
||||
o[n] = mfixext4
|
||||
o[n+1] = byte(e.ExtensionType())
|
||||
n += 2
|
||||
case 8:
|
||||
o, n = ensure(b, 10)
|
||||
o[n] = mfixext8
|
||||
o[n+1] = byte(e.ExtensionType())
|
||||
n += 2
|
||||
case 16:
|
||||
o, n = ensure(b, 18)
|
||||
o[n] = mfixext16
|
||||
o[n+1] = byte(e.ExtensionType())
|
||||
n += 2
|
||||
default:
|
||||
switch {
|
||||
case l < math.MaxUint8:
|
||||
o, n = ensure(b, l+3)
|
||||
o[n] = mext8
|
||||
o[n+1] = byte(l)
|
||||
o[n+2] = byte(e.ExtensionType())
|
||||
n += 3
|
||||
case l < math.MaxUint16:
|
||||
o, n = ensure(b, l+4)
|
||||
o[n] = mext16
|
||||
big.PutUint16(o[n+1:], uint16(l))
|
||||
o[n+3] = byte(e.ExtensionType())
|
||||
n += 4
|
||||
default:
|
||||
o, n = ensure(b, l+6)
|
||||
o[n] = mext32
|
||||
big.PutUint32(o[n+1:], uint32(l))
|
||||
o[n+5] = byte(e.ExtensionType())
|
||||
n += 6
|
||||
}
|
||||
}
|
||||
return o, e.MarshalBinaryTo(o[n:])
|
||||
}
|
||||
|
||||
// ReadExtensionBytes reads an extension from 'b' into 'e'
|
||||
// and returns any remaining bytes.
|
||||
// Possible errors:
|
||||
// - ErrShortBytes ('b' not long enough)
|
||||
// - ExtensionTypeError{} (wire type not the same as e.Type())
|
||||
// - TypeError{} (next object not an extension)
|
||||
// - InvalidPrefixError
|
||||
// - An umarshal error returned from e.UnmarshalBinary
|
||||
func ReadExtensionBytes(b []byte, e Extension) ([]byte, error) {
|
||||
typ, remain, data, err := readExt(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
if typ != e.ExtensionType() {
|
||||
return b, errExt(typ, e.ExtensionType())
|
||||
}
|
||||
return remain, e.UnmarshalBinary(data)
|
||||
}
|
||||
|
||||
// readExt will read the extension type, and return remaining bytes,
|
||||
// as well as the data of the extension.
|
||||
func readExt(b []byte) (typ int8, remain []byte, data []byte, err error) {
|
||||
l := len(b)
|
||||
if l < 3 {
|
||||
return 0, b, nil, ErrShortBytes
|
||||
}
|
||||
lead := b[0]
|
||||
var (
|
||||
sz int // size of 'data'
|
||||
off int // offset of 'data'
|
||||
)
|
||||
switch lead {
|
||||
case mfixext1:
|
||||
typ = int8(b[1])
|
||||
sz = 1
|
||||
off = 2
|
||||
case mfixext2:
|
||||
typ = int8(b[1])
|
||||
sz = 2
|
||||
off = 2
|
||||
case mfixext4:
|
||||
typ = int8(b[1])
|
||||
sz = 4
|
||||
off = 2
|
||||
case mfixext8:
|
||||
typ = int8(b[1])
|
||||
sz = 8
|
||||
off = 2
|
||||
case mfixext16:
|
||||
typ = int8(b[1])
|
||||
sz = 16
|
||||
off = 2
|
||||
case mext8:
|
||||
sz = int(b[1])
|
||||
typ = int8(b[2])
|
||||
off = 3
|
||||
if sz == 0 {
|
||||
return typ, b[3:], b[3:3], nil
|
||||
}
|
||||
case mext16:
|
||||
if l < 4 {
|
||||
return 0, b, nil, ErrShortBytes
|
||||
}
|
||||
sz = int(big.Uint16(b[1:]))
|
||||
typ = int8(b[3])
|
||||
off = 4
|
||||
case mext32:
|
||||
if l < 6 {
|
||||
return 0, b, nil, ErrShortBytes
|
||||
}
|
||||
sz = int(big.Uint32(b[1:]))
|
||||
typ = int8(b[5])
|
||||
off = 6
|
||||
default:
|
||||
return 0, b, nil, badPrefix(ExtensionType, lead)
|
||||
}
|
||||
// the data of the extension starts
|
||||
// at 'off' and is 'sz' bytes long
|
||||
tot := off + sz
|
||||
if len(b[off:]) < sz {
|
||||
return 0, b, nil, ErrShortBytes
|
||||
}
|
||||
return typ, b[tot:], b[off:tot:tot], nil
|
||||
}
|
||||
90
vendor/github.com/tinylib/msgp/msgp/file.go
generated
vendored
Normal file
90
vendor/github.com/tinylib/msgp/msgp/file.go
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
//go:build (linux || darwin || dragonfly || freebsd || illumos || netbsd || openbsd) && !appengine && !tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// ReadFile reads a file into 'dst' using
|
||||
// a read-only memory mapping. Consequently,
|
||||
// the file must be mmap-able, and the
|
||||
// Unmarshaler should never write to
|
||||
// the source memory. (Methods generated
|
||||
// by the msgp tool obey that constraint, but
|
||||
// user-defined implementations may not.)
|
||||
//
|
||||
// Reading and writing through file mappings
|
||||
// is only efficient for large files; small
|
||||
// files are best read and written using
|
||||
// the ordinary streaming interfaces.
|
||||
func ReadFile(dst Unmarshaler, file *os.File) error {
|
||||
stat, err := file.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := syscall.Mmap(int(file.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_SHARED)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
adviseRead(data)
|
||||
_, err = dst.UnmarshalMsg(data)
|
||||
uerr := syscall.Munmap(data)
|
||||
if err == nil {
|
||||
err = uerr
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalSizer is the combination
|
||||
// of the Marshaler and Sizer
|
||||
// interfaces.
|
||||
type MarshalSizer interface {
|
||||
Marshaler
|
||||
Sizer
|
||||
}
|
||||
|
||||
// WriteFile writes a file from 'src' using
|
||||
// memory mapping. It overwrites the entire
|
||||
// contents of the previous file.
|
||||
// The mapping size is calculated
|
||||
// using the `Msgsize()` method
|
||||
// of 'src', so it must produce a result
|
||||
// equal to or greater than the actual encoded
|
||||
// size of the object. Otherwise,
|
||||
// a fault (SIGBUS) will occur.
|
||||
//
|
||||
// Reading and writing through file mappings
|
||||
// is only efficient for large files; small
|
||||
// files are best read and written using
|
||||
// the ordinary streaming interfaces.
|
||||
//
|
||||
// NOTE: The performance of this call
|
||||
// is highly OS- and filesystem-dependent.
|
||||
// Users should take care to test that this
|
||||
// performs as expected in a production environment.
|
||||
// (Linux users should run a kernel and filesystem
|
||||
// that support fallocate(2) for the best results.)
|
||||
func WriteFile(src MarshalSizer, file *os.File) error {
|
||||
sz := src.Msgsize()
|
||||
err := fallocate(file, int64(sz))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := syscall.Mmap(int(file.Fd()), 0, sz, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
adviseWrite(data)
|
||||
chunk := data[:0]
|
||||
chunk, err = src.MarshalMsg(chunk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uerr := syscall.Munmap(data)
|
||||
if uerr != nil {
|
||||
return uerr
|
||||
}
|
||||
return file.Truncate(int64(len(chunk)))
|
||||
}
|
||||
47
vendor/github.com/tinylib/msgp/msgp/file_port.go
generated
vendored
Normal file
47
vendor/github.com/tinylib/msgp/msgp/file_port.go
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
//go:build windows || appengine || tinygo
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// MarshalSizer is the combination
|
||||
// of the Marshaler and Sizer
|
||||
// interfaces.
|
||||
type MarshalSizer interface {
|
||||
Marshaler
|
||||
Sizer
|
||||
}
|
||||
|
||||
func ReadFile(dst Unmarshaler, file *os.File) error {
|
||||
if u, ok := dst.(Decodable); ok {
|
||||
return u.DecodeMsg(NewReader(file))
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dst.UnmarshalMsg(data)
|
||||
return err
|
||||
}
|
||||
|
||||
func WriteFile(src MarshalSizer, file *os.File) error {
|
||||
if e, ok := src.(Encodable); ok {
|
||||
w := NewWriter(file)
|
||||
err := e.EncodeMsg(w)
|
||||
if err == nil {
|
||||
err = w.Flush()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
raw, err := src.MarshalMsg(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = file.Write(raw)
|
||||
return err
|
||||
}
|
||||
199
vendor/github.com/tinylib/msgp/msgp/integers.go
generated
vendored
Normal file
199
vendor/github.com/tinylib/msgp/msgp/integers.go
generated
vendored
Normal file
@@ -0,0 +1,199 @@
|
||||
package msgp
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
/* ----------------------------------
|
||||
integer encoding utilities
|
||||
(inline-able)
|
||||
|
||||
TODO(tinylib): there are faster,
|
||||
albeit non-portable solutions
|
||||
to the code below. implement
|
||||
byteswap?
|
||||
---------------------------------- */
|
||||
|
||||
func putMint64(b []byte, i int64) {
|
||||
_ = b[8] // bounds check elimination
|
||||
|
||||
b[0] = mint64
|
||||
b[1] = byte(i >> 56)
|
||||
b[2] = byte(i >> 48)
|
||||
b[3] = byte(i >> 40)
|
||||
b[4] = byte(i >> 32)
|
||||
b[5] = byte(i >> 24)
|
||||
b[6] = byte(i >> 16)
|
||||
b[7] = byte(i >> 8)
|
||||
b[8] = byte(i)
|
||||
}
|
||||
|
||||
func getMint64(b []byte) int64 {
|
||||
_ = b[8] // bounds check elimination
|
||||
|
||||
return (int64(b[1]) << 56) | (int64(b[2]) << 48) |
|
||||
(int64(b[3]) << 40) | (int64(b[4]) << 32) |
|
||||
(int64(b[5]) << 24) | (int64(b[6]) << 16) |
|
||||
(int64(b[7]) << 8) | (int64(b[8]))
|
||||
}
|
||||
|
||||
func putMint32(b []byte, i int32) {
|
||||
_ = b[4] // bounds check elimination
|
||||
|
||||
b[0] = mint32
|
||||
b[1] = byte(i >> 24)
|
||||
b[2] = byte(i >> 16)
|
||||
b[3] = byte(i >> 8)
|
||||
b[4] = byte(i)
|
||||
}
|
||||
|
||||
func getMint32(b []byte) int32 {
|
||||
_ = b[4] // bounds check elimination
|
||||
|
||||
return (int32(b[1]) << 24) | (int32(b[2]) << 16) | (int32(b[3]) << 8) | (int32(b[4]))
|
||||
}
|
||||
|
||||
func putMint16(b []byte, i int16) {
|
||||
_ = b[2] // bounds check elimination
|
||||
|
||||
b[0] = mint16
|
||||
b[1] = byte(i >> 8)
|
||||
b[2] = byte(i)
|
||||
}
|
||||
|
||||
func getMint16(b []byte) (i int16) {
|
||||
_ = b[2] // bounds check elimination
|
||||
|
||||
return (int16(b[1]) << 8) | int16(b[2])
|
||||
}
|
||||
|
||||
func putMint8(b []byte, i int8) {
|
||||
_ = b[1] // bounds check elimination
|
||||
|
||||
b[0] = mint8
|
||||
b[1] = byte(i)
|
||||
}
|
||||
|
||||
func getMint8(b []byte) (i int8) {
|
||||
return int8(b[1])
|
||||
}
|
||||
|
||||
func putMuint64(b []byte, u uint64) {
|
||||
_ = b[8] // bounds check elimination
|
||||
|
||||
b[0] = muint64
|
||||
b[1] = byte(u >> 56)
|
||||
b[2] = byte(u >> 48)
|
||||
b[3] = byte(u >> 40)
|
||||
b[4] = byte(u >> 32)
|
||||
b[5] = byte(u >> 24)
|
||||
b[6] = byte(u >> 16)
|
||||
b[7] = byte(u >> 8)
|
||||
b[8] = byte(u)
|
||||
}
|
||||
|
||||
func getMuint64(b []byte) uint64 {
|
||||
_ = b[8] // bounds check elimination
|
||||
|
||||
return (uint64(b[1]) << 56) | (uint64(b[2]) << 48) |
|
||||
(uint64(b[3]) << 40) | (uint64(b[4]) << 32) |
|
||||
(uint64(b[5]) << 24) | (uint64(b[6]) << 16) |
|
||||
(uint64(b[7]) << 8) | (uint64(b[8]))
|
||||
}
|
||||
|
||||
func putMuint32(b []byte, u uint32) {
|
||||
_ = b[4] // bounds check elimination
|
||||
|
||||
b[0] = muint32
|
||||
b[1] = byte(u >> 24)
|
||||
b[2] = byte(u >> 16)
|
||||
b[3] = byte(u >> 8)
|
||||
b[4] = byte(u)
|
||||
}
|
||||
|
||||
func getMuint32(b []byte) uint32 {
|
||||
_ = b[4] // bounds check elimination
|
||||
|
||||
return (uint32(b[1]) << 24) | (uint32(b[2]) << 16) | (uint32(b[3]) << 8) | (uint32(b[4]))
|
||||
}
|
||||
|
||||
func putMuint16(b []byte, u uint16) {
|
||||
_ = b[2] // bounds check elimination
|
||||
|
||||
b[0] = muint16
|
||||
b[1] = byte(u >> 8)
|
||||
b[2] = byte(u)
|
||||
}
|
||||
|
||||
func getMuint16(b []byte) uint16 {
|
||||
_ = b[2] // bounds check elimination
|
||||
|
||||
return (uint16(b[1]) << 8) | uint16(b[2])
|
||||
}
|
||||
|
||||
func putMuint8(b []byte, u uint8) {
|
||||
_ = b[1] // bounds check elimination
|
||||
|
||||
b[0] = muint8
|
||||
b[1] = u
|
||||
}
|
||||
|
||||
func getMuint8(b []byte) uint8 {
|
||||
return b[1]
|
||||
}
|
||||
|
||||
func getUnix(b []byte) (sec int64, nsec int32) {
|
||||
sec = int64(binary.BigEndian.Uint64(b))
|
||||
nsec = int32(binary.BigEndian.Uint32(b[8:]))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func putUnix(b []byte, sec int64, nsec int32) {
|
||||
binary.BigEndian.PutUint64(b, uint64(sec))
|
||||
binary.BigEndian.PutUint32(b[8:], uint32(nsec))
|
||||
}
|
||||
|
||||
/* -----------------------------
|
||||
prefix utilities
|
||||
----------------------------- */
|
||||
|
||||
// write prefix and uint8
|
||||
func prefixu8(b []byte, pre byte, sz uint8) {
|
||||
_ = b[1] // bounds check elimination
|
||||
|
||||
b[0] = pre
|
||||
b[1] = sz
|
||||
}
|
||||
|
||||
// write prefix and big-endian uint16
|
||||
func prefixu16(b []byte, pre byte, sz uint16) {
|
||||
_ = b[2] // bounds check elimination
|
||||
|
||||
b[0] = pre
|
||||
b[1] = byte(sz >> 8)
|
||||
b[2] = byte(sz)
|
||||
}
|
||||
|
||||
// write prefix and big-endian uint32
|
||||
func prefixu32(b []byte, pre byte, sz uint32) {
|
||||
_ = b[4] // bounds check elimination
|
||||
|
||||
b[0] = pre
|
||||
b[1] = byte(sz >> 24)
|
||||
b[2] = byte(sz >> 16)
|
||||
b[3] = byte(sz >> 8)
|
||||
b[4] = byte(sz)
|
||||
}
|
||||
|
||||
func prefixu64(b []byte, pre byte, sz uint64) {
|
||||
_ = b[8] // bounds check elimination
|
||||
|
||||
b[0] = pre
|
||||
b[1] = byte(sz >> 56)
|
||||
b[2] = byte(sz >> 48)
|
||||
b[3] = byte(sz >> 40)
|
||||
b[4] = byte(sz >> 32)
|
||||
b[5] = byte(sz >> 24)
|
||||
b[6] = byte(sz >> 16)
|
||||
b[7] = byte(sz >> 8)
|
||||
b[8] = byte(sz)
|
||||
}
|
||||
386
vendor/github.com/tinylib/msgp/msgp/iter.go
generated
vendored
Normal file
386
vendor/github.com/tinylib/msgp/msgp/iter.go
generated
vendored
Normal file
@@ -0,0 +1,386 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"iter"
|
||||
"maps"
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// ReadArray returns an iterator that can be used to iterate over the elements
|
||||
// of an array in the MessagePack data while being read by the provided Reader.
|
||||
// The type parameter V specifies the type of the elements in the array.
|
||||
// The returned iterator implements the iter.Seq[V] interface,
|
||||
// allowing for sequential access to the array elements.
|
||||
// The iterator will always stop after one error has been encountered.
|
||||
func ReadArray[T any](m *Reader, readFn func() (T, error)) iter.Seq2[T, error] {
|
||||
return func(yield func(T, error) bool) {
|
||||
// Check if nil
|
||||
if m.IsNil() {
|
||||
m.ReadNil()
|
||||
return
|
||||
}
|
||||
// Regular array.
|
||||
var empty T
|
||||
length, err := m.ReadArrayHeader()
|
||||
if err != nil {
|
||||
yield(empty, fmt.Errorf("cannot read array header: %w", err))
|
||||
return
|
||||
}
|
||||
for range length {
|
||||
var v T
|
||||
v, err = readFn()
|
||||
if !yield(v, err) || err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WriteArray writes an array to the provided Writer.
|
||||
// The writeFn parameter specifies the function to use to write each element of the array.
|
||||
func WriteArray[T any](w *Writer, a []T, writeFn func(T) error) error {
|
||||
// Check if nil
|
||||
if a == nil {
|
||||
return w.WriteNil()
|
||||
}
|
||||
if uint64(len(a)) > math.MaxUint32 {
|
||||
return fmt.Errorf("array too large to encode: %d elements", len(a))
|
||||
}
|
||||
// Write array header
|
||||
err := w.WriteArrayHeader(uint32(len(a)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Write elements
|
||||
for _, v := range a {
|
||||
err = writeFn(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadMap returns an iterator that can be used to iterate over the elements
|
||||
// of a map in the MessagePack data while being read by the provided Reader.
|
||||
// The type parameters K and V specify the types of the keys and values in the map.
|
||||
// The returned iterator implements the iter.Seq2[K, V] interface,
|
||||
// allowing for sequential access to the map elements.
|
||||
// The returned function can be used to read any error that
|
||||
// occurred during iteration when iteration is done.
|
||||
func ReadMap[K, V any](m *Reader, readKey func() (K, error), readVal func() (V, error)) (iter.Seq2[K, V], func() error) {
|
||||
var err error
|
||||
return func(yield func(K, V) bool) {
|
||||
var sz uint32
|
||||
if m.IsNil() {
|
||||
err = m.ReadNil()
|
||||
return
|
||||
}
|
||||
sz, err = m.ReadMapHeader()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("cannot read map header: %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
for range sz {
|
||||
var k K
|
||||
k, err = readKey()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("cannot read key: %w", err)
|
||||
return
|
||||
}
|
||||
var v V
|
||||
v, err = readVal()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("cannot read value: %w", err)
|
||||
return
|
||||
}
|
||||
if !yield(k, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}, func() error { return err }
|
||||
}
|
||||
|
||||
// WriteMap writes a map to the provided Writer.
|
||||
// The writeKey and writeVal parameters specify the functions
|
||||
// to use to write each key and value of the map.
|
||||
func WriteMap[K comparable, V any](w *Writer, m map[K]V, writeKey func(K) error, writeVal func(V) error) error {
|
||||
if m == nil {
|
||||
return w.WriteNil()
|
||||
}
|
||||
if uint64(len(m)) > math.MaxUint32 {
|
||||
return fmt.Errorf("map too large to encode: %d elements", len(m))
|
||||
}
|
||||
|
||||
// Write map header
|
||||
err := w.WriteMapHeader(uint32(len(m)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Write elements
|
||||
for k, v := range m {
|
||||
err = writeKey(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = writeVal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteMapSorted writes a map to the provided Writer.
|
||||
// The keys of the map are sorted before writing.
|
||||
// This provides deterministic output, but will allocate to sort the keys.
|
||||
// The writeKey and writeVal parameters specify the functions
|
||||
// to use to write each key and value of the map.
|
||||
func WriteMapSorted[K cmp.Ordered, V any](w *Writer, m map[K]V, writeKey func(K) error, writeVal func(V) error) error {
|
||||
if m == nil {
|
||||
return w.WriteNil()
|
||||
}
|
||||
if uint64(len(m)) > math.MaxUint32 {
|
||||
return fmt.Errorf("map too large to encode: %d elements", len(m))
|
||||
}
|
||||
|
||||
// Write map header
|
||||
err := w.WriteMapHeader(uint32(len(m)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Write elements
|
||||
for _, k := range slices.Sorted(maps.Keys(m)) {
|
||||
err = writeKey(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = writeVal(m[k])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadArrayBytes returns an iterator that can be used to iterate over the elements
|
||||
// of an array in the MessagePack data while being read by the provided Reader.
|
||||
// The type parameter V specifies the type of the elements in the array.
|
||||
// After the iterator is exhausted, the remaining bytes in the buffer
|
||||
// and any error can be read by calling the returned function.
|
||||
func ReadArrayBytes[T any](b []byte, readFn func([]byte) (T, []byte, error)) (iter.Seq[T], func() (remain []byte, err error)) {
|
||||
if IsNil(b) {
|
||||
b, err := ReadNilBytes(b)
|
||||
return func(yield func(T) bool) {}, func() ([]byte, error) { return b, err }
|
||||
}
|
||||
sz, b, err := ReadArrayHeaderBytes(b)
|
||||
if err != nil || sz == 0 {
|
||||
return func(yield func(T) bool) {}, func() ([]byte, error) { return b, err }
|
||||
}
|
||||
return func(yield func(T) bool) {
|
||||
for range sz {
|
||||
var v T
|
||||
v, b, err = readFn(b)
|
||||
if err != nil || !yield(v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}, func() ([]byte, error) {
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
|
||||
// AppendArray writes an array to the provided buffer.
|
||||
// The writeFn parameter specifies the function to use to write each element of the array.
|
||||
// The returned buffer contains the encoded array.
|
||||
// The function panics if the array is larger than math.MaxUint32 elements.
|
||||
func AppendArray[T any](b []byte, a []T, writeFn func(b []byte, v T) []byte) []byte {
|
||||
if a == nil {
|
||||
return AppendNil(b)
|
||||
}
|
||||
if uint64(len(a)) > math.MaxUint32 {
|
||||
panic(fmt.Sprintf("array too large to encode: %d elements", len(a)))
|
||||
}
|
||||
b = AppendArrayHeader(b, uint32(len(a)))
|
||||
for _, v := range a {
|
||||
b = writeFn(b, v)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// ReadMapBytes returns an iterator over key/value
|
||||
// pairs from a MessagePack map encoded in b.
|
||||
// The iterator yields K,V pairs, and this function also returns
|
||||
// a closure to get the remaining bytes and any error.
|
||||
func ReadMapBytes[K any, V any](b []byte,
|
||||
readK func([]byte) (K, []byte, error),
|
||||
readV func([]byte) (V, []byte, error)) (iter.Seq2[K, V], func() (remain []byte, err error)) {
|
||||
var err error
|
||||
var sz uint32
|
||||
if IsNil(b) {
|
||||
b, err = ReadNilBytes(b)
|
||||
return func(yield func(K, V) bool) {}, func() ([]byte, error) { return b, err }
|
||||
}
|
||||
sz, b, err = ReadMapHeaderBytes(b)
|
||||
if err != nil || sz == 0 {
|
||||
return func(yield func(K, V) bool) {}, func() ([]byte, error) { return b, err }
|
||||
}
|
||||
|
||||
return func(yield func(K, V) bool) {
|
||||
for range sz {
|
||||
var k K
|
||||
k, b, err = readK(b)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("cannot read map key: %w", err)
|
||||
return
|
||||
}
|
||||
var v V
|
||||
v, b, err = readV(b)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("cannot read map value: %w", err)
|
||||
return
|
||||
}
|
||||
if !yield(k, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}, func() ([]byte, error) { return b, err }
|
||||
}
|
||||
|
||||
// AppendMap writes a map to the provided buffer.
|
||||
// The writeK and writeV parameters specify the functions to use to write each key and value of the map.
|
||||
// The returned buffer contains the encoded map.
|
||||
// The function panics if the map is larger than math.MaxUint32 elements.
|
||||
func AppendMap[K comparable, V any](b []byte, m map[K]V,
|
||||
writeK func(b []byte, k K) []byte,
|
||||
writeV func(b []byte, v V) []byte) []byte {
|
||||
if m == nil {
|
||||
return AppendNil(b)
|
||||
}
|
||||
if uint64(len(m)) > math.MaxUint32 {
|
||||
panic(fmt.Sprintf("map too large to encode: %d elements", len(m)))
|
||||
}
|
||||
b = AppendMapHeader(b, uint32(len(m)))
|
||||
for k, v := range m {
|
||||
b = writeK(b, k)
|
||||
b = writeV(b, v)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// AppendMapSorted writes a map to the provided buffer.
|
||||
// Keys are sorted before writing.
|
||||
// This provides deterministic output, but will allocate to sort the keys.
|
||||
// The writeK and writeV parameters specify the functions to use to write each key and value of the map.
|
||||
// The returned buffer contains the encoded map.
|
||||
// The function panics if the map is larger than math.MaxUint32 elements.
|
||||
func AppendMapSorted[K cmp.Ordered, V any](b []byte, m map[K]V,
|
||||
writeK func(b []byte, k K) []byte,
|
||||
writeV func(b []byte, v V) []byte) []byte {
|
||||
if m == nil {
|
||||
return AppendNil(b)
|
||||
}
|
||||
if uint64(len(m)) > math.MaxUint32 {
|
||||
panic(fmt.Sprintf("map too large to encode: %d elements", len(m)))
|
||||
}
|
||||
b = AppendMapHeader(b, uint32(len(m)))
|
||||
for _, k := range slices.Sorted(maps.Keys(m)) {
|
||||
b = writeK(b, k)
|
||||
b = writeV(b, m[k])
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// DecodePtr is a convenience type for decoding into a pointer.
|
||||
type DecodePtr[T any] interface {
|
||||
*T
|
||||
Decodable
|
||||
}
|
||||
|
||||
// DecoderFrom allows augmenting any type with a DecodeMsg method into a method
|
||||
// that reads from Reader and returns a T.
|
||||
// Provide an instance of T. This value isn't used.
|
||||
// See ReadArray/ReadMap "struct" examples for usage.
|
||||
func DecoderFrom[T any, PT DecodePtr[T]](r *Reader, _ T) func() (T, error) {
|
||||
return func() (T, error) {
|
||||
var t T
|
||||
tPtr := PT(&t)
|
||||
err := tPtr.DecodeMsg(r)
|
||||
return t, err
|
||||
}
|
||||
}
|
||||
|
||||
// FlexibleEncoder is a constraint for types where either T or *T implements Encodable
|
||||
type FlexibleEncoder[T any] interface {
|
||||
Encodable
|
||||
*T
|
||||
}
|
||||
|
||||
// EncoderTo allows augmenting any type with an EncodeMsg
|
||||
// method into a method that writes to Writer on each call.
|
||||
// Provide an instance of T. This value isn't used.
|
||||
// See ReadArray or ReadMap "struct" examples for usage.
|
||||
func EncoderTo[T any, _ FlexibleEncoder[T]](w *Writer, _ T) func(T) error {
|
||||
return func(t T) error {
|
||||
// Check if T implements Marshaler
|
||||
if marshaler, ok := any(t).(Encodable); ok {
|
||||
return marshaler.EncodeMsg(w)
|
||||
}
|
||||
// Check if *T implements Marshaler
|
||||
if ptrMarshaler, ok := any(&t).(Encodable); ok {
|
||||
return ptrMarshaler.EncodeMsg(w)
|
||||
}
|
||||
// The compiler should have asserted this.
|
||||
panic("type does not implement Marshaler")
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalPtr is a convenience type for unmarshaling into a pointer.
|
||||
type UnmarshalPtr[T any] interface {
|
||||
*T
|
||||
Unmarshaler
|
||||
}
|
||||
|
||||
// DecoderFromBytes allows augmenting any type with an UnmarshalMsg
|
||||
// method into a method that reads from []byte and returns a T.
|
||||
// Provide an instance of T. This value isn't used.
|
||||
// See ReadArrayBytes or ReadMapBytes "struct" examples for usage.
|
||||
func DecoderFromBytes[T any, PT UnmarshalPtr[T]](_ T) func([]byte) (T, []byte, error) {
|
||||
return func(b []byte) (T, []byte, error) {
|
||||
var t T
|
||||
tPtr := PT(&t)
|
||||
b, err := tPtr.UnmarshalMsg(b)
|
||||
return t, b, err
|
||||
}
|
||||
}
|
||||
|
||||
// FlexibleMarshaler is a constraint for types where either T or *T implements Marshaler
|
||||
type FlexibleMarshaler[T any] interface {
|
||||
Marshaler
|
||||
*T // Include *T in the interface
|
||||
}
|
||||
|
||||
// EncoderToBytes allows augmenting any type with a MarshalMsg method into a method
|
||||
// that reads from T and returns a []byte.
|
||||
// Provide an instance of T. This value isn't used.
|
||||
// See ReadArrayBytes or ReadMapBytes "struct" examples for usage.
|
||||
func EncoderToBytes[T any, _ FlexibleMarshaler[T]](_ T) func([]byte, T) []byte {
|
||||
return func(b []byte, t T) []byte {
|
||||
// Check if T implements Marshaler
|
||||
if marshaler, ok := any(t).(Marshaler); ok {
|
||||
b, _ = marshaler.MarshalMsg(b)
|
||||
return b
|
||||
}
|
||||
// Check if *T implements Marshaler
|
||||
if ptrMarshaler, ok := any(&t).(Marshaler); ok {
|
||||
b, _ = ptrMarshaler.MarshalMsg(b)
|
||||
return b
|
||||
}
|
||||
// The compiler should have asserted this.
|
||||
panic("type does not implement Marshaler")
|
||||
}
|
||||
}
|
||||
584
vendor/github.com/tinylib/msgp/msgp/json.go
generated
vendored
Normal file
584
vendor/github.com/tinylib/msgp/msgp/json.go
generated
vendored
Normal file
@@ -0,0 +1,584 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var (
|
||||
null = []byte("null")
|
||||
hex = []byte("0123456789abcdef")
|
||||
)
|
||||
|
||||
var defuns [_maxtype]func(jsWriter, *Reader) (int, error)
|
||||
|
||||
// note: there is an initialization loop if
|
||||
// this isn't set up during init()
|
||||
func init() {
|
||||
// since none of these functions are inline-able,
|
||||
// there is not much of a penalty to the indirect
|
||||
// call. however, this is best expressed as a jump-table...
|
||||
defuns = [_maxtype]func(jsWriter, *Reader) (int, error){
|
||||
StrType: rwString,
|
||||
BinType: rwBytes,
|
||||
MapType: rwMap,
|
||||
ArrayType: rwArray,
|
||||
Float64Type: rwFloat64,
|
||||
Float32Type: rwFloat32,
|
||||
BoolType: rwBool,
|
||||
IntType: rwInt,
|
||||
UintType: rwUint,
|
||||
NilType: rwNil,
|
||||
ExtensionType: rwExtension,
|
||||
Complex64Type: rwExtension,
|
||||
Complex128Type: rwExtension,
|
||||
TimeType: rwTime,
|
||||
}
|
||||
}
|
||||
|
||||
// this is the interface
|
||||
// used to write json
|
||||
type jsWriter interface {
|
||||
io.Writer
|
||||
io.ByteWriter
|
||||
WriteString(string) (int, error)
|
||||
}
|
||||
|
||||
// CopyToJSON reads MessagePack from 'src' and copies it
|
||||
// as JSON to 'dst' until EOF.
|
||||
func CopyToJSON(dst io.Writer, src io.Reader) (n int64, err error) {
|
||||
r := NewReader(src)
|
||||
n, err = r.WriteToJSON(dst)
|
||||
freeR(r)
|
||||
return
|
||||
}
|
||||
|
||||
// WriteToJSON translates MessagePack from 'r' and writes it as
|
||||
// JSON to 'w' until the underlying reader returns io.EOF. It returns
|
||||
// the number of bytes written, and an error if it stopped before EOF.
|
||||
func (m *Reader) WriteToJSON(w io.Writer) (n int64, err error) {
|
||||
var j jsWriter
|
||||
var bf *bufio.Writer
|
||||
if jsw, ok := w.(jsWriter); ok {
|
||||
j = jsw
|
||||
} else {
|
||||
bf = bufio.NewWriter(w)
|
||||
j = bf
|
||||
}
|
||||
var nn int
|
||||
for err == nil {
|
||||
nn, err = rwNext(j, m)
|
||||
n += int64(nn)
|
||||
}
|
||||
if err != io.EOF {
|
||||
if bf != nil {
|
||||
bf.Flush()
|
||||
}
|
||||
return
|
||||
}
|
||||
err = nil
|
||||
if bf != nil {
|
||||
err = bf.Flush()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func rwNext(w jsWriter, src *Reader) (int, error) {
|
||||
t, err := src.NextType()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return defuns[t](w, src)
|
||||
}
|
||||
|
||||
func rwMap(dst jsWriter, src *Reader) (n int, err error) {
|
||||
var comma bool
|
||||
var sz uint32
|
||||
var field []byte
|
||||
|
||||
sz, err = src.ReadMapHeader()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if sz == 0 {
|
||||
return dst.WriteString("{}")
|
||||
}
|
||||
|
||||
// This is potentially a recursive call.
|
||||
if done, err := src.recursiveCall(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
defer done()
|
||||
}
|
||||
|
||||
err = dst.WriteByte('{')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
var nn int
|
||||
for i := uint32(0); i < sz; i++ {
|
||||
if comma {
|
||||
err = dst.WriteByte(',')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
}
|
||||
|
||||
field, err = src.ReadMapKeyPtr()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
nn, err = rwquoted(dst, field)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = dst.WriteByte(':')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
nn, err = rwNext(dst, src)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !comma {
|
||||
comma = true
|
||||
}
|
||||
}
|
||||
|
||||
err = dst.WriteByte('}')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
return
|
||||
}
|
||||
|
||||
func rwArray(dst jsWriter, src *Reader) (n int, err error) {
|
||||
err = dst.WriteByte('[')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// This is potentially a recursive call.
|
||||
if done, err := src.recursiveCall(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
defer done()
|
||||
}
|
||||
|
||||
var sz uint32
|
||||
var nn int
|
||||
sz, err = src.ReadArrayHeader()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
comma := false
|
||||
for i := uint32(0); i < sz; i++ {
|
||||
if comma {
|
||||
err = dst.WriteByte(',')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
}
|
||||
nn, err = rwNext(dst, src)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
comma = true
|
||||
}
|
||||
|
||||
err = dst.WriteByte(']')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
return
|
||||
}
|
||||
|
||||
func rwNil(dst jsWriter, src *Reader) (int, error) {
|
||||
err := src.ReadNil()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return dst.Write(null)
|
||||
}
|
||||
|
||||
func rwFloat32(dst jsWriter, src *Reader) (int, error) {
|
||||
f, err := src.ReadFloat32()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
src.scratch = strconv.AppendFloat(src.scratch[:0], float64(f), 'f', -1, 32)
|
||||
return dst.Write(src.scratch)
|
||||
}
|
||||
|
||||
func rwFloat64(dst jsWriter, src *Reader) (int, error) {
|
||||
f, err := src.ReadFloat64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
src.scratch = strconv.AppendFloat(src.scratch[:0], f, 'f', -1, 64)
|
||||
return dst.Write(src.scratch)
|
||||
}
|
||||
|
||||
func rwInt(dst jsWriter, src *Reader) (int, error) {
|
||||
i, err := src.ReadInt64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
src.scratch = strconv.AppendInt(src.scratch[:0], i, 10)
|
||||
return dst.Write(src.scratch)
|
||||
}
|
||||
|
||||
func rwUint(dst jsWriter, src *Reader) (int, error) {
|
||||
u, err := src.ReadUint64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
src.scratch = strconv.AppendUint(src.scratch[:0], u, 10)
|
||||
return dst.Write(src.scratch)
|
||||
}
|
||||
|
||||
func rwBool(dst jsWriter, src *Reader) (int, error) {
|
||||
b, err := src.ReadBool()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if b {
|
||||
return dst.WriteString("true")
|
||||
}
|
||||
return dst.WriteString("false")
|
||||
}
|
||||
|
||||
func rwTime(dst jsWriter, src *Reader) (int, error) {
|
||||
t, err := src.ReadTime()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
bts, err := t.MarshalJSON()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return dst.Write(bts)
|
||||
}
|
||||
|
||||
func rwExtension(dst jsWriter, src *Reader) (n int, err error) {
|
||||
et, err := src.peekExtensionType()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// registered extensions can override
|
||||
// the JSON encoding
|
||||
if j, ok := extensionReg[et]; ok {
|
||||
var bts []byte
|
||||
e := j()
|
||||
err = src.ReadExtension(e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bts, err = json.Marshal(e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return dst.Write(bts)
|
||||
}
|
||||
|
||||
e := RawExtension{}
|
||||
e.Type = et
|
||||
err = src.ReadExtension(&e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var nn int
|
||||
err = dst.WriteByte('{')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
|
||||
nn, err = dst.WriteString(`"type":`)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
src.scratch = strconv.AppendInt(src.scratch[0:0], int64(e.Type), 10)
|
||||
nn, err = dst.Write(src.scratch)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
nn, err = dst.WriteString(`,"data":"`)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
enc := base64.NewEncoder(base64.StdEncoding, dst)
|
||||
|
||||
nn, err = enc.Write(e.Data)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = enc.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
nn, err = dst.WriteString(`"}`)
|
||||
n += nn
|
||||
return
|
||||
}
|
||||
|
||||
func rwString(dst jsWriter, src *Reader) (n int, err error) {
|
||||
lead, err := src.R.PeekByte()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var read int
|
||||
var p []byte
|
||||
if isfixstr(lead) {
|
||||
read = int(rfixstr(lead))
|
||||
src.R.Skip(1)
|
||||
goto write
|
||||
}
|
||||
|
||||
switch lead {
|
||||
case mstr8:
|
||||
p, err = src.R.Next(2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
read = int(p[1])
|
||||
case mstr16:
|
||||
p, err = src.R.Next(3)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
read = int(big.Uint16(p[1:]))
|
||||
case mstr32:
|
||||
p, err = src.R.Next(5)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
read = int(big.Uint32(p[1:]))
|
||||
default:
|
||||
err = badPrefix(StrType, lead)
|
||||
return
|
||||
}
|
||||
write:
|
||||
if uint64(read) > src.GetMaxStringLength() {
|
||||
err = ErrLimitExceeded
|
||||
return
|
||||
}
|
||||
p, err = src.R.Next(read)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n, err = rwquoted(dst, p)
|
||||
return
|
||||
}
|
||||
|
||||
func rwBytes(dst jsWriter, src *Reader) (n int, err error) {
|
||||
var nn int
|
||||
err = dst.WriteByte('"')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
src.scratch, err = src.ReadBytes(src.scratch[:0])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
enc := base64.NewEncoder(base64.StdEncoding, dst)
|
||||
nn, err = enc.Write(src.scratch)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = enc.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = dst.WriteByte('"')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
return
|
||||
}
|
||||
|
||||
// Below (c) The Go Authors, 2009-2014
|
||||
// Subject to the BSD-style license found at http://golang.org
|
||||
//
|
||||
// see: encoding/json/encode.go:(*encodeState).stringbytes()
|
||||
func rwquoted(dst jsWriter, s []byte) (n int, err error) {
|
||||
var nn int
|
||||
err = dst.WriteByte('"')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
start := 0
|
||||
for i := 0; i < len(s); {
|
||||
if b := s[i]; b < utf8.RuneSelf {
|
||||
if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if start < i {
|
||||
nn, err = dst.Write(s[start:i])
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
switch b {
|
||||
case '\\', '"':
|
||||
err = dst.WriteByte('\\')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
err = dst.WriteByte(b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
case '\n':
|
||||
err = dst.WriteByte('\\')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
err = dst.WriteByte('n')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
case '\r':
|
||||
err = dst.WriteByte('\\')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
err = dst.WriteByte('r')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
case '\t':
|
||||
err = dst.WriteByte('\\')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
err = dst.WriteByte('t')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
default:
|
||||
// This encodes bytes < 0x20 except for \t, \n and \r.
|
||||
// It also escapes <, >, and &
|
||||
// because they can lead to security holes when
|
||||
// user-controlled strings are rendered into JSON
|
||||
// and served to some browsers.
|
||||
nn, err = dst.WriteString(`\u00`)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = dst.WriteByte(hex[b>>4])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
err = dst.WriteByte(hex[b&0xF])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
}
|
||||
i++
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
c, size := utf8.DecodeRune(s[i:])
|
||||
if c == utf8.RuneError && size == 1 {
|
||||
if start < i {
|
||||
nn, err = dst.Write(s[start:i])
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
nn, err = dst.WriteString(`\ufffd`)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
i += size
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
// U+2028 is LINE SEPARATOR.
|
||||
// U+2029 is PARAGRAPH SEPARATOR.
|
||||
// They are both technically valid characters in JSON strings,
|
||||
// but don't work in JSONP, which has to be evaluated as JavaScript,
|
||||
// and can lead to security holes there. It is valid JSON to
|
||||
// escape them, so we do so unconditionally.
|
||||
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
|
||||
if c == '\u2028' || c == '\u2029' {
|
||||
if start < i {
|
||||
nn, err = dst.Write(s[start:i])
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
nn, err = dst.WriteString(`\u202`)
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = dst.WriteByte(hex[c&0xF])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
i += size
|
||||
start = i
|
||||
continue
|
||||
}
|
||||
i += size
|
||||
}
|
||||
if start < len(s) {
|
||||
nn, err = dst.Write(s[start:])
|
||||
n += nn
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
err = dst.WriteByte('"')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n++
|
||||
return
|
||||
}
|
||||
347
vendor/github.com/tinylib/msgp/msgp/json_bytes.go
generated
vendored
Normal file
347
vendor/github.com/tinylib/msgp/msgp/json_bytes.go
generated
vendored
Normal file
@@ -0,0 +1,347 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var unfuns [_maxtype]func(jsWriter, []byte, []byte, int) ([]byte, []byte, error)
|
||||
|
||||
func init() {
|
||||
// NOTE(pmh): this is best expressed as a jump table,
|
||||
// but gc doesn't do that yet. revisit post-go1.5.
|
||||
unfuns = [_maxtype]func(jsWriter, []byte, []byte, int) ([]byte, []byte, error){
|
||||
StrType: rwStringBytes,
|
||||
BinType: rwBytesBytes,
|
||||
MapType: rwMapBytes,
|
||||
ArrayType: rwArrayBytes,
|
||||
Float64Type: rwFloat64Bytes,
|
||||
Float32Type: rwFloat32Bytes,
|
||||
BoolType: rwBoolBytes,
|
||||
IntType: rwIntBytes,
|
||||
UintType: rwUintBytes,
|
||||
NilType: rwNullBytes,
|
||||
ExtensionType: rwExtensionBytes,
|
||||
Complex64Type: rwExtensionBytes,
|
||||
Complex128Type: rwExtensionBytes,
|
||||
TimeType: rwTimeBytes,
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalAsJSON takes raw messagepack and writes
|
||||
// it as JSON to 'w'. If an error is returned, the
|
||||
// bytes not translated will also be returned. If
|
||||
// no errors are encountered, the length of the returned
|
||||
// slice will be zero.
|
||||
func UnmarshalAsJSON(w io.Writer, msg []byte) ([]byte, error) {
|
||||
var (
|
||||
scratch []byte
|
||||
cast bool
|
||||
dst jsWriter
|
||||
err error
|
||||
)
|
||||
if jsw, ok := w.(jsWriter); ok {
|
||||
dst = jsw
|
||||
cast = true
|
||||
} else {
|
||||
dst = bufio.NewWriterSize(w, 512)
|
||||
}
|
||||
for len(msg) > 0 && err == nil {
|
||||
msg, scratch, err = writeNext(dst, msg, scratch, 0)
|
||||
}
|
||||
if !cast && err == nil {
|
||||
err = dst.(*bufio.Writer).Flush()
|
||||
}
|
||||
return msg, err
|
||||
}
|
||||
|
||||
func writeNext(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
if len(msg) < 1 {
|
||||
return msg, scratch, ErrShortBytes
|
||||
}
|
||||
t := getType(msg[0])
|
||||
if t == InvalidType {
|
||||
return msg, scratch, InvalidPrefixError(msg[0])
|
||||
}
|
||||
if t == ExtensionType {
|
||||
et, err := peekExtension(msg)
|
||||
if err != nil {
|
||||
return nil, scratch, err
|
||||
}
|
||||
if et == TimeExtension || et == MsgTimeExtension {
|
||||
t = TimeType
|
||||
}
|
||||
}
|
||||
return unfuns[t](w, msg, scratch, depth)
|
||||
}
|
||||
|
||||
func rwArrayBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
if depth >= recursionLimit {
|
||||
return msg, scratch, ErrRecursion
|
||||
}
|
||||
sz, msg, err := ReadArrayHeaderBytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
err = w.WriteByte('[')
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
for i := range sz {
|
||||
if i != 0 {
|
||||
err = w.WriteByte(',')
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
}
|
||||
msg, scratch, err = writeNext(w, msg, scratch, depth+1)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
}
|
||||
err = w.WriteByte(']')
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwMapBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
if depth >= recursionLimit {
|
||||
return msg, scratch, ErrRecursion
|
||||
}
|
||||
sz, msg, err := ReadMapHeaderBytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
err = w.WriteByte('{')
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
for i := range sz {
|
||||
if i != 0 {
|
||||
err = w.WriteByte(',')
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
}
|
||||
msg, scratch, err = rwMapKeyBytes(w, msg, scratch, depth)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
err = w.WriteByte(':')
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
msg, scratch, err = writeNext(w, msg, scratch, depth+1)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
}
|
||||
err = w.WriteByte('}')
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwMapKeyBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
msg, scratch, err := rwStringBytes(w, msg, scratch, depth)
|
||||
if err != nil {
|
||||
if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
|
||||
return rwBytesBytes(w, msg, scratch, depth)
|
||||
}
|
||||
}
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwStringBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
str, msg, err := ReadStringZC(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = rwquoted(w, str)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwBytesBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
bts, msg, err := ReadBytesZC(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
l := base64.StdEncoding.EncodedLen(len(bts))
|
||||
if cap(scratch) >= l {
|
||||
scratch = scratch[0:l]
|
||||
} else {
|
||||
scratch = make([]byte, l)
|
||||
}
|
||||
base64.StdEncoding.Encode(scratch, bts)
|
||||
err = w.WriteByte('"')
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = w.Write(scratch)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
err = w.WriteByte('"')
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwNullBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
msg, err := ReadNilBytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = w.Write(null)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwBoolBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
b, msg, err := ReadBoolBytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
if b {
|
||||
_, err = w.WriteString("true")
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = w.WriteString("false")
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwIntBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
i, msg, err := ReadInt64Bytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
scratch = strconv.AppendInt(scratch[0:0], i, 10)
|
||||
_, err = w.Write(scratch)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwUintBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
u, msg, err := ReadUint64Bytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
scratch = strconv.AppendUint(scratch[0:0], u, 10)
|
||||
_, err = w.Write(scratch)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwFloat32Bytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
var f float32
|
||||
var err error
|
||||
f, msg, err = ReadFloat32Bytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
scratch = strconv.AppendFloat(scratch[:0], float64(f), 'f', -1, 32)
|
||||
_, err = w.Write(scratch)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwFloat64Bytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
var f float64
|
||||
var err error
|
||||
f, msg, err = ReadFloat64Bytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
scratch = strconv.AppendFloat(scratch[:0], f, 'f', -1, 64)
|
||||
_, err = w.Write(scratch)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwTimeBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
var t time.Time
|
||||
var err error
|
||||
t, msg, err = ReadTimeBytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
bts, err := t.MarshalJSON()
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = w.Write(bts)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func rwExtensionBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
|
||||
var err error
|
||||
var et int8
|
||||
et, err = peekExtension(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
// if it's time.Time
|
||||
if et == TimeExtension || et == MsgTimeExtension {
|
||||
var tm time.Time
|
||||
tm, msg, err = ReadTimeBytes(msg)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
bts, err := tm.MarshalJSON()
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = w.Write(bts)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
// if the extension is registered,
|
||||
// use its canonical JSON form
|
||||
if f, ok := extensionReg[et]; ok {
|
||||
e := f()
|
||||
msg, err = ReadExtensionBytes(msg, e)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
bts, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
_, err = w.Write(bts)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
// otherwise, write `{"type": <num>, "data": "<base64data>"}`
|
||||
r := RawExtension{}
|
||||
r.Type = et
|
||||
msg, err = ReadExtensionBytes(msg, &r)
|
||||
if err != nil {
|
||||
return msg, scratch, err
|
||||
}
|
||||
scratch, err = writeExt(w, r, scratch)
|
||||
return msg, scratch, err
|
||||
}
|
||||
|
||||
func writeExt(w jsWriter, r RawExtension, scratch []byte) ([]byte, error) {
|
||||
_, err := w.WriteString(`{"type":`)
|
||||
if err != nil {
|
||||
return scratch, err
|
||||
}
|
||||
scratch = strconv.AppendInt(scratch[0:0], int64(r.Type), 10)
|
||||
_, err = w.Write(scratch)
|
||||
if err != nil {
|
||||
return scratch, err
|
||||
}
|
||||
_, err = w.WriteString(`,"data":"`)
|
||||
if err != nil {
|
||||
return scratch, err
|
||||
}
|
||||
l := base64.StdEncoding.EncodedLen(len(r.Data))
|
||||
if cap(scratch) >= l {
|
||||
scratch = scratch[0:l]
|
||||
} else {
|
||||
scratch = make([]byte, l)
|
||||
}
|
||||
base64.StdEncoding.Encode(scratch, r.Data)
|
||||
_, err = w.Write(scratch)
|
||||
if err != nil {
|
||||
return scratch, err
|
||||
}
|
||||
_, err = w.WriteString(`"}`)
|
||||
return scratch, err
|
||||
}
|
||||
390
vendor/github.com/tinylib/msgp/msgp/number.go
generated
vendored
Normal file
390
vendor/github.com/tinylib/msgp/msgp/number.go
generated
vendored
Normal file
@@ -0,0 +1,390 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/bits"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// The portable parts of the Number implementation
|
||||
|
||||
// Number can be
|
||||
// an int64, uint64, float32,
|
||||
// or float64 internally.
|
||||
// It can decode itself
|
||||
// from any of the native
|
||||
// messagepack number types.
|
||||
// The zero-value of Number
|
||||
// is Int(0). Using the equality
|
||||
// operator with Number compares
|
||||
// both the type and the value
|
||||
// of the number.
|
||||
type Number struct {
|
||||
// internally, this
|
||||
// is just a tagged union.
|
||||
// the raw bits of the number
|
||||
// are stored the same way regardless.
|
||||
bits uint64
|
||||
typ Type
|
||||
}
|
||||
|
||||
// AsInt sets the number to an int64.
|
||||
func (n *Number) AsInt(i int64) {
|
||||
// we always store int(0)
|
||||
// as {0, InvalidType} in
|
||||
// order to preserve
|
||||
// the behavior of the == operator
|
||||
if i == 0 {
|
||||
n.typ = InvalidType
|
||||
n.bits = 0
|
||||
return
|
||||
}
|
||||
|
||||
n.typ = IntType
|
||||
n.bits = uint64(i)
|
||||
}
|
||||
|
||||
// AsUint sets the number to a uint64.
|
||||
func (n *Number) AsUint(u uint64) {
|
||||
n.typ = UintType
|
||||
n.bits = u
|
||||
}
|
||||
|
||||
// AsFloat32 sets the value of the number
|
||||
// to a float32.
|
||||
func (n *Number) AsFloat32(f float32) {
|
||||
n.typ = Float32Type
|
||||
n.bits = uint64(math.Float32bits(f))
|
||||
}
|
||||
|
||||
// AsFloat64 sets the value of the
|
||||
// number to a float64.
|
||||
func (n *Number) AsFloat64(f float64) {
|
||||
n.typ = Float64Type
|
||||
n.bits = math.Float64bits(f)
|
||||
}
|
||||
|
||||
// Int casts the number as an int64, and
|
||||
// returns whether or not that was the
|
||||
// underlying type.
|
||||
func (n *Number) Int() (int64, bool) {
|
||||
return int64(n.bits), n.typ == IntType || n.typ == InvalidType
|
||||
}
|
||||
|
||||
// Uint casts the number as a uint64, and returns
|
||||
// whether or not that was the underlying type.
|
||||
func (n *Number) Uint() (uint64, bool) {
|
||||
return n.bits, n.typ == UintType
|
||||
}
|
||||
|
||||
// Float casts the number to a float64, and
|
||||
// returns whether that was the underlying
|
||||
// type (either a float64 or a float32).
|
||||
func (n *Number) Float() (float64, bool) {
|
||||
switch n.typ {
|
||||
case Float32Type:
|
||||
return float64(math.Float32frombits(uint32(n.bits))), true
|
||||
case Float64Type:
|
||||
return math.Float64frombits(n.bits), true
|
||||
default:
|
||||
return 0.0, false
|
||||
}
|
||||
}
|
||||
|
||||
// Type will return one of:
|
||||
// Float64Type, Float32Type, UintType, or IntType.
|
||||
func (n *Number) Type() Type {
|
||||
if n.typ == InvalidType {
|
||||
return IntType
|
||||
}
|
||||
return n.typ
|
||||
}
|
||||
|
||||
// DecodeMsg implements msgp.Decodable
|
||||
func (n *Number) DecodeMsg(r *Reader) error {
|
||||
typ, err := r.NextType()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch typ {
|
||||
case Float32Type:
|
||||
f, err := r.ReadFloat32()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n.AsFloat32(f)
|
||||
return nil
|
||||
case Float64Type:
|
||||
f, err := r.ReadFloat64()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n.AsFloat64(f)
|
||||
return nil
|
||||
case IntType:
|
||||
i, err := r.ReadInt64()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n.AsInt(i)
|
||||
return nil
|
||||
case UintType:
|
||||
u, err := r.ReadUint64()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n.AsUint(u)
|
||||
return nil
|
||||
default:
|
||||
return TypeError{Encoded: typ, Method: IntType}
|
||||
}
|
||||
}
|
||||
|
||||
// UnmarshalMsg implements msgp.Unmarshaler
|
||||
func (n *Number) UnmarshalMsg(b []byte) ([]byte, error) {
|
||||
typ := NextType(b)
|
||||
switch typ {
|
||||
case IntType:
|
||||
i, o, err := ReadInt64Bytes(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
n.AsInt(i)
|
||||
return o, nil
|
||||
case UintType:
|
||||
u, o, err := ReadUint64Bytes(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
n.AsUint(u)
|
||||
return o, nil
|
||||
case Float64Type:
|
||||
f, o, err := ReadFloat64Bytes(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
n.AsFloat64(f)
|
||||
return o, nil
|
||||
case Float32Type:
|
||||
f, o, err := ReadFloat32Bytes(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
n.AsFloat32(f)
|
||||
return o, nil
|
||||
default:
|
||||
return b, TypeError{Method: IntType, Encoded: typ}
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalMsg implements msgp.Marshaler
|
||||
func (n *Number) MarshalMsg(b []byte) ([]byte, error) {
|
||||
switch n.typ {
|
||||
case IntType:
|
||||
return AppendInt64(b, int64(n.bits)), nil
|
||||
case UintType:
|
||||
return AppendUint64(b, n.bits), nil
|
||||
case Float64Type:
|
||||
return AppendFloat64(b, math.Float64frombits(n.bits)), nil
|
||||
case Float32Type:
|
||||
return AppendFloat32(b, math.Float32frombits(uint32(n.bits))), nil
|
||||
default:
|
||||
return AppendInt64(b, 0), nil
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeMsg implements msgp.Encodable
|
||||
func (n *Number) EncodeMsg(w *Writer) error {
|
||||
switch n.typ {
|
||||
case IntType:
|
||||
return w.WriteInt64(int64(n.bits))
|
||||
case UintType:
|
||||
return w.WriteUint64(n.bits)
|
||||
case Float64Type:
|
||||
return w.WriteFloat64(math.Float64frombits(n.bits))
|
||||
case Float32Type:
|
||||
return w.WriteFloat32(math.Float32frombits(uint32(n.bits)))
|
||||
default:
|
||||
return w.WriteInt64(0)
|
||||
}
|
||||
}
|
||||
|
||||
// CoerceInt attempts to coerce the value of
|
||||
// the number into a signed integer and returns
|
||||
// whether it was successful.
|
||||
// "Success" implies that no precision in the value of
|
||||
// the number was lost, which means that the number was an integer or
|
||||
// a floating point that mapped exactly to an integer without rounding.
|
||||
func (n *Number) CoerceInt() (int64, bool) {
|
||||
switch n.typ {
|
||||
case InvalidType, IntType:
|
||||
// InvalidType just means un-initialized.
|
||||
return int64(n.bits), true
|
||||
case UintType:
|
||||
return int64(n.bits), n.bits <= math.MaxInt64
|
||||
case Float32Type:
|
||||
f := math.Float32frombits(uint32(n.bits))
|
||||
if n.isExactInt() && f <= math.MaxInt64 && f >= math.MinInt64 {
|
||||
return int64(f), true
|
||||
}
|
||||
if n.bits == 0 || n.bits == 1<<31 {
|
||||
return 0, true
|
||||
}
|
||||
case Float64Type:
|
||||
f := math.Float64frombits(n.bits)
|
||||
if n.isExactInt() && f <= math.MaxInt64 && f >= math.MinInt64 {
|
||||
return int64(f), true
|
||||
}
|
||||
return 0, n.bits == 0 || n.bits == 1<<63
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// CoerceUInt attempts to coerce the value of
|
||||
// the number into an unsigned integer and returns
|
||||
// whether it was successful.
|
||||
// "Success" implies that no precision in the value of
|
||||
// the number was lost, which means that the number was an integer or
|
||||
// a floating point that mapped exactly to an integer without rounding.
|
||||
func (n *Number) CoerceUInt() (uint64, bool) {
|
||||
switch n.typ {
|
||||
case InvalidType, IntType:
|
||||
// InvalidType just means un-initialized.
|
||||
if int64(n.bits) >= 0 {
|
||||
return n.bits, true
|
||||
}
|
||||
case UintType:
|
||||
return n.bits, true
|
||||
case Float32Type:
|
||||
f := math.Float32frombits(uint32(n.bits))
|
||||
if f >= 0 && f <= math.MaxUint64 && n.isExactInt() {
|
||||
return uint64(f), true
|
||||
}
|
||||
if n.bits == 0 || n.bits == 1<<31 {
|
||||
return 0, true
|
||||
}
|
||||
case Float64Type:
|
||||
f := math.Float64frombits(n.bits)
|
||||
if f >= 0 && f <= math.MaxUint64 && n.isExactInt() {
|
||||
return uint64(f), true
|
||||
}
|
||||
return 0, n.bits == 0 || n.bits == 1<<63
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// isExactInt will return true if the number represents an integer value.
|
||||
// NaN, Inf returns false.
|
||||
func (n *Number) isExactInt() bool {
|
||||
var eBits int // Exponent bits
|
||||
var mBits int // Mantissa bits
|
||||
|
||||
switch n.typ {
|
||||
case InvalidType, IntType, UintType:
|
||||
return true
|
||||
case Float32Type:
|
||||
eBits = 8
|
||||
mBits = 23
|
||||
case Float64Type:
|
||||
eBits = 11
|
||||
mBits = 52
|
||||
default:
|
||||
return false
|
||||
}
|
||||
// Calculate float parts
|
||||
exp := int(n.bits>>mBits) & ((1 << eBits) - 1)
|
||||
mant := n.bits & ((1 << mBits) - 1)
|
||||
if exp == 0 && mant == 0 {
|
||||
// Handle zero value.
|
||||
return true
|
||||
}
|
||||
|
||||
exp -= (1 << (eBits - 1)) - 1
|
||||
if exp < 0 || exp == 1<<(eBits-1) {
|
||||
// Negative exponent is never integer (except zero handled above)
|
||||
// Handles NaN (exp all 1s)
|
||||
return false
|
||||
}
|
||||
|
||||
if exp >= mBits {
|
||||
// If we have more exponent than mantissa bits it is always an integer.
|
||||
return true
|
||||
}
|
||||
// Check if all bits below the exponent are zero.
|
||||
return bits.TrailingZeros64(mant) >= mBits-exp
|
||||
}
|
||||
|
||||
// CoerceFloat returns the number as a float64.
|
||||
// If the number is an integer, it will be
|
||||
// converted to a float64 with the closest representation.
|
||||
func (n *Number) CoerceFloat() float64 {
|
||||
switch n.typ {
|
||||
case IntType:
|
||||
return float64(int64(n.bits))
|
||||
case UintType:
|
||||
return float64(n.bits)
|
||||
case Float32Type:
|
||||
return float64(math.Float32frombits(uint32(n.bits)))
|
||||
case Float64Type:
|
||||
return math.Float64frombits(n.bits)
|
||||
default:
|
||||
return 0.0
|
||||
}
|
||||
}
|
||||
|
||||
// Msgsize implements msgp.Sizer
|
||||
func (n *Number) Msgsize() int {
|
||||
switch n.typ {
|
||||
case Float32Type:
|
||||
return Float32Size
|
||||
case Float64Type:
|
||||
return Float64Size
|
||||
case IntType:
|
||||
return Int64Size
|
||||
case UintType:
|
||||
return Uint64Size
|
||||
default:
|
||||
return 1 // fixint(0)
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler
|
||||
func (n *Number) MarshalJSON() ([]byte, error) {
|
||||
t := n.Type()
|
||||
if t == InvalidType {
|
||||
return []byte{'0'}, nil
|
||||
}
|
||||
out := make([]byte, 0, 32)
|
||||
switch t {
|
||||
case Float32Type, Float64Type:
|
||||
f, _ := n.Float()
|
||||
return strconv.AppendFloat(out, f, 'f', -1, 64), nil
|
||||
case IntType:
|
||||
i, _ := n.Int()
|
||||
return strconv.AppendInt(out, i, 10), nil
|
||||
case UintType:
|
||||
u, _ := n.Uint()
|
||||
return strconv.AppendUint(out, u, 10), nil
|
||||
default:
|
||||
panic("(*Number).typ is invalid")
|
||||
}
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer
|
||||
func (n *Number) String() string {
|
||||
switch n.typ {
|
||||
case InvalidType:
|
||||
return "0"
|
||||
case Float32Type, Float64Type:
|
||||
f, _ := n.Float()
|
||||
return strconv.FormatFloat(f, 'f', -1, 64)
|
||||
case IntType:
|
||||
i, _ := n.Int()
|
||||
return strconv.FormatInt(i, 10)
|
||||
case UintType:
|
||||
u, _ := n.Uint()
|
||||
return strconv.FormatUint(u, 10)
|
||||
default:
|
||||
panic("(*Number).typ is invalid")
|
||||
}
|
||||
}
|
||||
15
vendor/github.com/tinylib/msgp/msgp/purego.go
generated
vendored
Normal file
15
vendor/github.com/tinylib/msgp/msgp/purego.go
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
//go:build (purego && !unsafe) || appengine
|
||||
|
||||
package msgp
|
||||
|
||||
// let's just assume appengine
|
||||
// uses 64-bit hardware...
|
||||
const smallint = false
|
||||
|
||||
func UnsafeString(b []byte) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func UnsafeBytes(s string) []byte {
|
||||
return []byte(s)
|
||||
}
|
||||
1619
vendor/github.com/tinylib/msgp/msgp/read.go
generated
vendored
Normal file
1619
vendor/github.com/tinylib/msgp/msgp/read.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1402
vendor/github.com/tinylib/msgp/msgp/read_bytes.go
generated
vendored
Normal file
1402
vendor/github.com/tinylib/msgp/msgp/read_bytes.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3989
vendor/github.com/tinylib/msgp/msgp/setof/generated.go
generated
vendored
Normal file
3989
vendor/github.com/tinylib/msgp/msgp/setof/generated.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
19
vendor/github.com/tinylib/msgp/msgp/setof/setof.go
generated
vendored
Normal file
19
vendor/github.com/tinylib/msgp/msgp/setof/setof.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
// Package setof allows serializing sets map[T]struct{} as arrays.
|
||||
//
|
||||
// Nil maps are preserved as a nil value on stream.
|
||||
//
|
||||
// A deterministic, sorted version is available, with slightly lower performance.
|
||||
|
||||
package setof
|
||||
|
||||
// ensure 'sz' extra bytes in 'b' can be appended without reallocating
|
||||
func ensure(b []byte, sz int) []byte {
|
||||
l := len(b)
|
||||
c := cap(b)
|
||||
if c-l < sz {
|
||||
o := make([]byte, l, l+sz)
|
||||
copy(o, b)
|
||||
return o
|
||||
}
|
||||
return b
|
||||
}
|
||||
49
vendor/github.com/tinylib/msgp/msgp/size.go
generated
vendored
Normal file
49
vendor/github.com/tinylib/msgp/msgp/size.go
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
package msgp
|
||||
|
||||
// The sizes provided
|
||||
// are the worst-case
|
||||
// encoded sizes for
|
||||
// each type. For variable-
|
||||
// length types ([]byte, string),
|
||||
// the total encoded size is
|
||||
// the prefix size plus the
|
||||
// length of the object.
|
||||
const (
|
||||
Int64Size = 9
|
||||
IntSize = Int64Size
|
||||
UintSize = Int64Size
|
||||
Int8Size = 2
|
||||
Int16Size = 3
|
||||
Int32Size = 5
|
||||
Uint8Size = 2
|
||||
ByteSize = Uint8Size
|
||||
Uint16Size = 3
|
||||
Uint32Size = 5
|
||||
Uint64Size = Int64Size
|
||||
Float64Size = 9
|
||||
Float32Size = 5
|
||||
Complex64Size = 10
|
||||
Complex128Size = 18
|
||||
|
||||
DurationSize = Int64Size
|
||||
TimeSize = 15
|
||||
BoolSize = 1
|
||||
NilSize = 1
|
||||
JSONNumberSize = Int64Size // Same as Float64Size
|
||||
|
||||
MapHeaderSize = 5
|
||||
ArrayHeaderSize = 5
|
||||
|
||||
BytesPrefixSize = 5
|
||||
StringPrefixSize = 5
|
||||
ExtensionPrefixSize = 6
|
||||
|
||||
// We cannot determine the exact size of the marshalled bytes,
|
||||
// so we assume 32 bytes
|
||||
BinaryMarshalerSize = BytesPrefixSize + 32
|
||||
BinaryAppenderSize
|
||||
TextMarshalerBinSize
|
||||
TextAppenderBinSize
|
||||
TextMarshalerStringSize = StringPrefixSize + 32
|
||||
TextAppenderStringSize
|
||||
)
|
||||
36
vendor/github.com/tinylib/msgp/msgp/unsafe.go
generated
vendored
Normal file
36
vendor/github.com/tinylib/msgp/msgp/unsafe.go
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
//go:build (!purego && !appengine) || (!appengine && purego && unsafe)
|
||||
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// NOTE:
|
||||
// all of the definition in this file
|
||||
// should be repeated in appengine.go,
|
||||
// but without using unsafe
|
||||
|
||||
const (
|
||||
// spec says int and uint are always
|
||||
// the same size, but that int/uint
|
||||
// size may not be machine word size
|
||||
smallint = unsafe.Sizeof(int(0)) == 4
|
||||
)
|
||||
|
||||
// UnsafeString returns the byte slice as a volatile string
|
||||
// THIS SHOULD ONLY BE USED BY THE CODE GENERATOR.
|
||||
// THIS IS EVIL CODE.
|
||||
// YOU HAVE BEEN WARNED.
|
||||
func UnsafeString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
// UnsafeBytes returns the string as a byte slice
|
||||
//
|
||||
// Deprecated:
|
||||
// Since this code is no longer used by the code generator,
|
||||
// UnsafeBytes(s) is precisely equivalent to []byte(s)
|
||||
func UnsafeBytes(s string) []byte {
|
||||
return []byte(s)
|
||||
}
|
||||
952
vendor/github.com/tinylib/msgp/msgp/write.go
generated
vendored
Normal file
952
vendor/github.com/tinylib/msgp/msgp/write.go
generated
vendored
Normal file
@@ -0,0 +1,952 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// min buffer size for the writer
|
||||
minWriterSize = 18
|
||||
)
|
||||
|
||||
// Sizer is an interface implemented
|
||||
// by types that can estimate their
|
||||
// size when MessagePack encoded.
|
||||
// This interface is optional, but
|
||||
// encoding/marshaling implementations
|
||||
// may use this as a way to pre-allocate
|
||||
// memory for serialization.
|
||||
type Sizer interface {
|
||||
Msgsize() int
|
||||
}
|
||||
|
||||
var (
|
||||
// Nowhere is an io.Writer to nowhere
|
||||
Nowhere io.Writer = nwhere{}
|
||||
|
||||
btsType = reflect.TypeOf(([]byte)(nil))
|
||||
writerPool = sync.Pool{
|
||||
New: func() any {
|
||||
return &Writer{buf: make([]byte, 2048)}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func popWriter(w io.Writer) *Writer {
|
||||
wr := writerPool.Get().(*Writer)
|
||||
wr.Reset(w)
|
||||
return wr
|
||||
}
|
||||
|
||||
func pushWriter(wr *Writer) {
|
||||
wr.w = nil
|
||||
wr.wloc = 0
|
||||
writerPool.Put(wr)
|
||||
}
|
||||
|
||||
// freeW frees a writer for use
|
||||
// by other processes. It is not necessary
|
||||
// to call freeW on a writer. However, maintaining
|
||||
// a reference to a *Writer after calling freeW on
|
||||
// it will cause undefined behavior.
|
||||
func freeW(w *Writer) { pushWriter(w) }
|
||||
|
||||
// Require ensures that cap(old)-len(old) >= extra.
|
||||
func Require(old []byte, extra int) []byte {
|
||||
l := len(old)
|
||||
c := cap(old)
|
||||
r := l + extra
|
||||
if c >= r {
|
||||
return old
|
||||
} else if l == 0 {
|
||||
return make([]byte, 0, extra)
|
||||
}
|
||||
// the new size is the greater
|
||||
// of double the old capacity
|
||||
// and the sum of the old length
|
||||
// and the number of new bytes
|
||||
// necessary.
|
||||
c <<= 1
|
||||
if c < r {
|
||||
c = r
|
||||
}
|
||||
n := make([]byte, l, c)
|
||||
copy(n, old)
|
||||
return n
|
||||
}
|
||||
|
||||
// nowhere writer
|
||||
type nwhere struct{}
|
||||
|
||||
func (n nwhere) Write(p []byte) (int, error) { return len(p), nil }
|
||||
|
||||
// Marshaler is the interface implemented
|
||||
// by types that know how to marshal themselves
|
||||
// as MessagePack. MarshalMsg appends the marshalled
|
||||
// form of the object to the provided
|
||||
// byte slice, returning the extended
|
||||
// slice and any errors encountered.
|
||||
type Marshaler interface {
|
||||
MarshalMsg([]byte) ([]byte, error)
|
||||
}
|
||||
|
||||
// Encodable is the interface implemented
|
||||
// by types that know how to write themselves
|
||||
// as MessagePack using a *msgp.Writer.
|
||||
type Encodable interface {
|
||||
EncodeMsg(*Writer) error
|
||||
}
|
||||
|
||||
// Writer is a buffered writer
|
||||
// that can be used to write
|
||||
// MessagePack objects to an io.Writer.
|
||||
// You must call *Writer.Flush() in order
|
||||
// to flush all of the buffered data
|
||||
// to the underlying writer.
|
||||
type Writer struct {
|
||||
w io.Writer
|
||||
buf []byte
|
||||
wloc int
|
||||
}
|
||||
|
||||
// NewWriter returns a new *Writer.
|
||||
func NewWriter(w io.Writer) *Writer {
|
||||
if wr, ok := w.(*Writer); ok {
|
||||
return wr
|
||||
}
|
||||
return popWriter(w)
|
||||
}
|
||||
|
||||
// NewWriterSize returns a writer with a custom buffer size.
|
||||
func NewWriterSize(w io.Writer, sz int) *Writer {
|
||||
// we must be able to require() 'minWriterSize'
|
||||
// contiguous bytes, so that is the
|
||||
// practical minimum buffer size
|
||||
if sz < minWriterSize {
|
||||
sz = minWriterSize
|
||||
}
|
||||
buf := make([]byte, sz)
|
||||
return NewWriterBuf(w, buf)
|
||||
}
|
||||
|
||||
// NewWriterBuf returns a writer with a provided buffer.
|
||||
// 'buf' is not used when the capacity is smaller than 18,
|
||||
// custom buffer is allocated instead.
|
||||
func NewWriterBuf(w io.Writer, buf []byte) *Writer {
|
||||
if cap(buf) < minWriterSize {
|
||||
buf = make([]byte, minWriterSize)
|
||||
}
|
||||
buf = buf[:cap(buf)]
|
||||
return &Writer{
|
||||
w: w,
|
||||
buf: buf,
|
||||
}
|
||||
}
|
||||
|
||||
// Encode encodes an Encodable to an io.Writer.
|
||||
func Encode(w io.Writer, e Encodable) error {
|
||||
wr := NewWriter(w)
|
||||
err := e.EncodeMsg(wr)
|
||||
if err == nil {
|
||||
err = wr.Flush()
|
||||
}
|
||||
freeW(wr)
|
||||
return err
|
||||
}
|
||||
|
||||
func (mw *Writer) flush() error {
|
||||
if mw.wloc == 0 {
|
||||
return nil
|
||||
}
|
||||
n, err := mw.w.Write(mw.buf[:mw.wloc])
|
||||
if err != nil {
|
||||
if n > 0 {
|
||||
mw.wloc = copy(mw.buf, mw.buf[n:mw.wloc])
|
||||
}
|
||||
return err
|
||||
}
|
||||
mw.wloc = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush flushes all of the buffered
|
||||
// data to the underlying writer.
|
||||
func (mw *Writer) Flush() error { return mw.flush() }
|
||||
|
||||
// Buffered returns the number bytes in the write buffer
|
||||
func (mw *Writer) Buffered() int { return len(mw.buf) - mw.wloc }
|
||||
|
||||
func (mw *Writer) avail() int { return len(mw.buf) - mw.wloc }
|
||||
|
||||
func (mw *Writer) bufsize() int { return len(mw.buf) }
|
||||
|
||||
// NOTE: this should only be called with
|
||||
// a number that is guaranteed to be less than
|
||||
// len(mw.buf). typically, it is called with a constant.
|
||||
//
|
||||
// NOTE: this is a hot code path
|
||||
func (mw *Writer) require(n int) (int, error) {
|
||||
c := len(mw.buf)
|
||||
wl := mw.wloc
|
||||
if c-wl < n {
|
||||
if err := mw.flush(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
wl = mw.wloc
|
||||
}
|
||||
mw.wloc += n
|
||||
return wl, nil
|
||||
}
|
||||
|
||||
func (mw *Writer) Append(b ...byte) error {
|
||||
if mw.avail() < len(b) {
|
||||
err := mw.flush()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
mw.wloc += copy(mw.buf[mw.wloc:], b)
|
||||
return nil
|
||||
}
|
||||
|
||||
// push one byte onto the buffer
|
||||
//
|
||||
// NOTE: this is a hot code path
|
||||
func (mw *Writer) push(b byte) error {
|
||||
if mw.wloc == len(mw.buf) {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
mw.buf[mw.wloc] = b
|
||||
mw.wloc++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mw *Writer) prefix8(b byte, u uint8) error {
|
||||
const need = 2
|
||||
if len(mw.buf)-mw.wloc < need {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
prefixu8(mw.buf[mw.wloc:], b, u)
|
||||
mw.wloc += need
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mw *Writer) prefix16(b byte, u uint16) error {
|
||||
const need = 3
|
||||
if len(mw.buf)-mw.wloc < need {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
prefixu16(mw.buf[mw.wloc:], b, u)
|
||||
mw.wloc += need
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mw *Writer) prefix32(b byte, u uint32) error {
|
||||
const need = 5
|
||||
if len(mw.buf)-mw.wloc < need {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
prefixu32(mw.buf[mw.wloc:], b, u)
|
||||
mw.wloc += need
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mw *Writer) prefix64(b byte, u uint64) error {
|
||||
const need = 9
|
||||
if len(mw.buf)-mw.wloc < need {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
prefixu64(mw.buf[mw.wloc:], b, u)
|
||||
mw.wloc += need
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write implements io.Writer, and writes
|
||||
// data directly to the buffer.
|
||||
func (mw *Writer) Write(p []byte) (int, error) {
|
||||
l := len(p)
|
||||
if mw.avail() < l {
|
||||
if err := mw.flush(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if l > len(mw.buf) {
|
||||
return mw.w.Write(p)
|
||||
}
|
||||
}
|
||||
mw.wloc += copy(mw.buf[mw.wloc:], p)
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// implements io.WriteString
|
||||
func (mw *Writer) writeString(s string) error {
|
||||
l := len(s)
|
||||
if mw.avail() < l {
|
||||
if err := mw.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
if l > len(mw.buf) {
|
||||
_, err := io.WriteString(mw.w, s)
|
||||
return err
|
||||
}
|
||||
}
|
||||
mw.wloc += copy(mw.buf[mw.wloc:], s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset changes the underlying writer used by the Writer
|
||||
func (mw *Writer) Reset(w io.Writer) {
|
||||
mw.buf = mw.buf[:cap(mw.buf)]
|
||||
mw.w = w
|
||||
mw.wloc = 0
|
||||
}
|
||||
|
||||
// WriteMapHeader writes a map header of the given
|
||||
// size to the writer
|
||||
func (mw *Writer) WriteMapHeader(sz uint32) error {
|
||||
switch {
|
||||
case sz <= 15:
|
||||
return mw.push(wfixmap(uint8(sz)))
|
||||
case sz <= math.MaxUint16:
|
||||
return mw.prefix16(mmap16, uint16(sz))
|
||||
default:
|
||||
return mw.prefix32(mmap32, sz)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteArrayHeader writes an array header of the
|
||||
// given size to the writer
|
||||
func (mw *Writer) WriteArrayHeader(sz uint32) error {
|
||||
switch {
|
||||
case sz <= 15:
|
||||
return mw.push(wfixarray(uint8(sz)))
|
||||
case sz <= math.MaxUint16:
|
||||
return mw.prefix16(marray16, uint16(sz))
|
||||
default:
|
||||
return mw.prefix32(marray32, sz)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteNil writes a nil byte to the buffer
|
||||
func (mw *Writer) WriteNil() error {
|
||||
return mw.push(mnil)
|
||||
}
|
||||
|
||||
// WriteFloat writes a float to the writer as either float64
|
||||
// or float32 when it represents the exact same value
|
||||
func (mw *Writer) WriteFloat(f float64) error {
|
||||
f32 := float32(f)
|
||||
if float64(f32) == f {
|
||||
return mw.prefix32(mfloat32, math.Float32bits(f32))
|
||||
}
|
||||
return mw.prefix64(mfloat64, math.Float64bits(f))
|
||||
}
|
||||
|
||||
// WriteFloat64 writes a float64 to the writer
|
||||
func (mw *Writer) WriteFloat64(f float64) error {
|
||||
return mw.prefix64(mfloat64, math.Float64bits(f))
|
||||
}
|
||||
|
||||
// WriteFloat32 writes a float32 to the writer
|
||||
func (mw *Writer) WriteFloat32(f float32) error {
|
||||
return mw.prefix32(mfloat32, math.Float32bits(f))
|
||||
}
|
||||
|
||||
// WriteDuration writes a time.Duration to the writer
|
||||
func (mw *Writer) WriteDuration(d time.Duration) error {
|
||||
return mw.WriteInt64(int64(d))
|
||||
}
|
||||
|
||||
// WriteInt64 writes an int64 to the writer
|
||||
func (mw *Writer) WriteInt64(i int64) error {
|
||||
if i >= 0 {
|
||||
switch {
|
||||
case i <= math.MaxInt8:
|
||||
return mw.push(wfixint(uint8(i)))
|
||||
case i <= math.MaxInt16:
|
||||
return mw.prefix16(mint16, uint16(i))
|
||||
case i <= math.MaxInt32:
|
||||
return mw.prefix32(mint32, uint32(i))
|
||||
default:
|
||||
return mw.prefix64(mint64, uint64(i))
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case i >= -32:
|
||||
return mw.push(wnfixint(int8(i)))
|
||||
case i >= math.MinInt8:
|
||||
return mw.prefix8(mint8, uint8(i))
|
||||
case i >= math.MinInt16:
|
||||
return mw.prefix16(mint16, uint16(i))
|
||||
case i >= math.MinInt32:
|
||||
return mw.prefix32(mint32, uint32(i))
|
||||
default:
|
||||
return mw.prefix64(mint64, uint64(i))
|
||||
}
|
||||
}
|
||||
|
||||
// WriteInt8 writes an int8 to the writer
|
||||
func (mw *Writer) WriteInt8(i int8) error { return mw.WriteInt64(int64(i)) }
|
||||
|
||||
// WriteInt16 writes an int16 to the writer
|
||||
func (mw *Writer) WriteInt16(i int16) error { return mw.WriteInt64(int64(i)) }
|
||||
|
||||
// WriteInt32 writes an int32 to the writer
|
||||
func (mw *Writer) WriteInt32(i int32) error { return mw.WriteInt64(int64(i)) }
|
||||
|
||||
// WriteInt writes an int to the writer
|
||||
func (mw *Writer) WriteInt(i int) error { return mw.WriteInt64(int64(i)) }
|
||||
|
||||
// WriteUint64 writes a uint64 to the writer
|
||||
func (mw *Writer) WriteUint64(u uint64) error {
|
||||
switch {
|
||||
case u <= (1<<7)-1:
|
||||
return mw.push(wfixint(uint8(u)))
|
||||
case u <= math.MaxUint8:
|
||||
return mw.prefix8(muint8, uint8(u))
|
||||
case u <= math.MaxUint16:
|
||||
return mw.prefix16(muint16, uint16(u))
|
||||
case u <= math.MaxUint32:
|
||||
return mw.prefix32(muint32, uint32(u))
|
||||
default:
|
||||
return mw.prefix64(muint64, u)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteByte is analogous to WriteUint8
|
||||
func (mw *Writer) WriteByte(u byte) error { return mw.WriteUint8(u) }
|
||||
|
||||
// WriteUint8 writes a uint8 to the writer
|
||||
func (mw *Writer) WriteUint8(u uint8) error { return mw.WriteUint64(uint64(u)) }
|
||||
|
||||
// WriteUint16 writes a uint16 to the writer
|
||||
func (mw *Writer) WriteUint16(u uint16) error { return mw.WriteUint64(uint64(u)) }
|
||||
|
||||
// WriteUint32 writes a uint32 to the writer
|
||||
func (mw *Writer) WriteUint32(u uint32) error { return mw.WriteUint64(uint64(u)) }
|
||||
|
||||
// WriteUint writes a uint to the writer
|
||||
func (mw *Writer) WriteUint(u uint) error { return mw.WriteUint64(uint64(u)) }
|
||||
|
||||
// WriteBytes writes binary as 'bin' to the writer
|
||||
func (mw *Writer) WriteBytes(b []byte) error {
|
||||
if uint64(len(b)) > math.MaxUint32 {
|
||||
return ErrLimitExceeded
|
||||
}
|
||||
sz := uint32(len(b))
|
||||
var err error
|
||||
switch {
|
||||
case sz <= math.MaxUint8:
|
||||
err = mw.prefix8(mbin8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
err = mw.prefix16(mbin16, uint16(sz))
|
||||
default:
|
||||
err = mw.prefix32(mbin32, sz)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = mw.Write(b)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteBytesHeader writes just the size header
|
||||
// of a MessagePack 'bin' object. The user is responsible
|
||||
// for then writing 'sz' more bytes into the stream.
|
||||
func (mw *Writer) WriteBytesHeader(sz uint32) error {
|
||||
switch {
|
||||
case sz <= math.MaxUint8:
|
||||
return mw.prefix8(mbin8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
return mw.prefix16(mbin16, uint16(sz))
|
||||
default:
|
||||
return mw.prefix32(mbin32, sz)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteBool writes a bool to the writer
|
||||
func (mw *Writer) WriteBool(b bool) error {
|
||||
if b {
|
||||
return mw.push(mtrue)
|
||||
}
|
||||
return mw.push(mfalse)
|
||||
}
|
||||
|
||||
// WriteString writes a messagepack string to the writer.
|
||||
// (This is NOT an implementation of io.StringWriter)
|
||||
func (mw *Writer) WriteString(s string) error {
|
||||
if uint64(len(s)) > math.MaxUint32 {
|
||||
return ErrLimitExceeded
|
||||
}
|
||||
|
||||
sz := uint32(len(s))
|
||||
var err error
|
||||
switch {
|
||||
case sz <= 31:
|
||||
err = mw.push(wfixstr(uint8(sz)))
|
||||
case sz <= math.MaxUint8:
|
||||
err = mw.prefix8(mstr8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
err = mw.prefix16(mstr16, uint16(sz))
|
||||
default:
|
||||
err = mw.prefix32(mstr32, sz)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return mw.writeString(s)
|
||||
}
|
||||
|
||||
// WriteStringHeader writes just the string size
|
||||
// header of a MessagePack 'str' object. The user
|
||||
// is responsible for writing 'sz' more valid UTF-8
|
||||
// bytes to the stream.
|
||||
func (mw *Writer) WriteStringHeader(sz uint32) error {
|
||||
switch {
|
||||
case sz <= 31:
|
||||
return mw.push(wfixstr(uint8(sz)))
|
||||
case sz <= math.MaxUint8:
|
||||
return mw.prefix8(mstr8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
return mw.prefix16(mstr16, uint16(sz))
|
||||
default:
|
||||
return mw.prefix32(mstr32, sz)
|
||||
}
|
||||
}
|
||||
|
||||
// WriteStringFromBytes writes a 'str' object
|
||||
// from a []byte.
|
||||
func (mw *Writer) WriteStringFromBytes(str []byte) error {
|
||||
if uint64(len(str)) > math.MaxUint32 {
|
||||
return ErrLimitExceeded
|
||||
}
|
||||
sz := uint32(len(str))
|
||||
var err error
|
||||
switch {
|
||||
case sz <= 31:
|
||||
err = mw.push(wfixstr(uint8(sz)))
|
||||
case sz <= math.MaxUint8:
|
||||
err = mw.prefix8(mstr8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
err = mw.prefix16(mstr16, uint16(sz))
|
||||
default:
|
||||
err = mw.prefix32(mstr32, sz)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = mw.Write(str)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteComplex64 writes a complex64 to the writer
|
||||
func (mw *Writer) WriteComplex64(f complex64) error {
|
||||
o, err := mw.require(10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext8
|
||||
mw.buf[o+1] = Complex64Extension
|
||||
big.PutUint32(mw.buf[o+2:], math.Float32bits(real(f)))
|
||||
big.PutUint32(mw.buf[o+6:], math.Float32bits(imag(f)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteComplex128 writes a complex128 to the writer
|
||||
func (mw *Writer) WriteComplex128(f complex128) error {
|
||||
o, err := mw.require(18)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext16
|
||||
mw.buf[o+1] = Complex128Extension
|
||||
big.PutUint64(mw.buf[o+2:], math.Float64bits(real(f)))
|
||||
big.PutUint64(mw.buf[o+10:], math.Float64bits(imag(f)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteMapStrStr writes a map[string]string to the writer
|
||||
func (mw *Writer) WriteMapStrStr(mp map[string]string) (err error) {
|
||||
err = mw.WriteMapHeader(uint32(len(mp)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for key, val := range mp {
|
||||
err = mw.WriteString(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = mw.WriteString(val)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteMapStrIntf writes a map[string]interface to the writer
|
||||
func (mw *Writer) WriteMapStrIntf(mp map[string]any) (err error) {
|
||||
err = mw.WriteMapHeader(uint32(len(mp)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for key, val := range mp {
|
||||
err = mw.WriteString(key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = mw.WriteIntf(val)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// WriteTime writes a time.Time object to the wire.
|
||||
//
|
||||
// Time is encoded as Unix time, which means that
|
||||
// location (time zone) data is removed from the object.
|
||||
// The encoded object itself is 12 bytes: 8 bytes for
|
||||
// a big-endian 64-bit integer denoting seconds
|
||||
// elapsed since "zero" Unix time, followed by 4 bytes
|
||||
// for a big-endian 32-bit signed integer denoting
|
||||
// the nanosecond offset of the time. This encoding
|
||||
// is intended to ease portability across languages.
|
||||
// (Note that this is *not* the standard time.Time
|
||||
// binary encoding, because its implementation relies
|
||||
// heavily on the internal representation used by the
|
||||
// time package.)
|
||||
func (mw *Writer) WriteTime(t time.Time) error {
|
||||
t = t.UTC()
|
||||
o, err := mw.require(15)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mext8
|
||||
mw.buf[o+1] = 12
|
||||
mw.buf[o+2] = TimeExtension
|
||||
putUnix(mw.buf[o+3:], t.Unix(), int32(t.Nanosecond()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteTimeExt will write t using the official msgpack extension spec.
|
||||
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
|
||||
func (mw *Writer) WriteTimeExt(t time.Time) error {
|
||||
// Time rounded towards zero.
|
||||
secPrec := t.Truncate(time.Second)
|
||||
remain := t.Sub(secPrec).Nanoseconds()
|
||||
asSecs := secPrec.Unix()
|
||||
switch {
|
||||
case remain == 0 && asSecs > 0 && asSecs <= math.MaxUint32:
|
||||
// 4 bytes
|
||||
o, err := mw.require(6)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext4
|
||||
mw.buf[o+1] = byte(msgTimeExtension)
|
||||
binary.BigEndian.PutUint32(mw.buf[o+2:], uint32(asSecs))
|
||||
return nil
|
||||
case asSecs < 0 || asSecs >= (1<<34):
|
||||
// 12 bytes
|
||||
o, err := mw.require(12 + 3)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mext8
|
||||
mw.buf[o+1] = 12
|
||||
mw.buf[o+2] = byte(msgTimeExtension)
|
||||
binary.BigEndian.PutUint32(mw.buf[o+3:], uint32(remain))
|
||||
binary.BigEndian.PutUint64(mw.buf[o+3+4:], uint64(asSecs))
|
||||
default:
|
||||
// 8 bytes
|
||||
o, err := mw.require(10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mw.buf[o] = mfixext8
|
||||
mw.buf[o+1] = byte(msgTimeExtension)
|
||||
binary.BigEndian.PutUint64(mw.buf[o+2:], uint64(asSecs)|(uint64(remain)<<34))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteJSONNumber writes the json.Number to the stream as either integer or float.
|
||||
func (mw *Writer) WriteJSONNumber(n json.Number) error {
|
||||
if n == "" {
|
||||
// The zero value outputs the 0 integer.
|
||||
return mw.push(0)
|
||||
}
|
||||
ii, err := n.Int64()
|
||||
if err == nil {
|
||||
return mw.WriteInt64(ii)
|
||||
}
|
||||
ff, err := n.Float64()
|
||||
if err == nil {
|
||||
return mw.WriteFloat(ff)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteIntf writes the concrete type of 'v'.
|
||||
// WriteIntf will error if 'v' is not one of the following:
|
||||
// - A bool, float, string, []byte, int, uint, or complex
|
||||
// - A map of supported types (with string keys)
|
||||
// - An array or slice of supported types
|
||||
// - A pointer to a supported type
|
||||
// - A type that satisfies the msgp.Encodable interface
|
||||
// - A type that satisfies the msgp.Extension interface
|
||||
func (mw *Writer) WriteIntf(v any) error {
|
||||
if v == nil {
|
||||
return mw.WriteNil()
|
||||
}
|
||||
switch v := v.(type) {
|
||||
|
||||
// preferred interfaces
|
||||
|
||||
case Encodable:
|
||||
return v.EncodeMsg(mw)
|
||||
case Extension:
|
||||
return mw.WriteExtension(v)
|
||||
|
||||
// concrete types
|
||||
|
||||
case bool:
|
||||
return mw.WriteBool(v)
|
||||
case float32:
|
||||
return mw.WriteFloat32(v)
|
||||
case float64:
|
||||
return mw.WriteFloat64(v)
|
||||
case complex64:
|
||||
return mw.WriteComplex64(v)
|
||||
case complex128:
|
||||
return mw.WriteComplex128(v)
|
||||
case uint8:
|
||||
return mw.WriteUint8(v)
|
||||
case uint16:
|
||||
return mw.WriteUint16(v)
|
||||
case uint32:
|
||||
return mw.WriteUint32(v)
|
||||
case uint64:
|
||||
return mw.WriteUint64(v)
|
||||
case uint:
|
||||
return mw.WriteUint(v)
|
||||
case int8:
|
||||
return mw.WriteInt8(v)
|
||||
case int16:
|
||||
return mw.WriteInt16(v)
|
||||
case int32:
|
||||
return mw.WriteInt32(v)
|
||||
case int64:
|
||||
return mw.WriteInt64(v)
|
||||
case int:
|
||||
return mw.WriteInt(v)
|
||||
case string:
|
||||
return mw.WriteString(v)
|
||||
case []byte:
|
||||
return mw.WriteBytes(v)
|
||||
case map[string]string:
|
||||
return mw.WriteMapStrStr(v)
|
||||
case map[string]any:
|
||||
return mw.WriteMapStrIntf(v)
|
||||
case time.Time:
|
||||
return mw.WriteTime(v)
|
||||
case time.Duration:
|
||||
return mw.WriteDuration(v)
|
||||
case json.Number:
|
||||
return mw.WriteJSONNumber(v)
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(v)
|
||||
if !isSupported(val.Kind()) || !val.IsValid() {
|
||||
return errors.New("msgp: type " + val.String() + " not supported")
|
||||
}
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Ptr:
|
||||
if val.IsNil() {
|
||||
return mw.WriteNil()
|
||||
}
|
||||
return mw.WriteIntf(val.Elem().Interface())
|
||||
case reflect.Slice:
|
||||
return mw.writeSlice(val)
|
||||
case reflect.Map:
|
||||
return mw.writeMap(val)
|
||||
}
|
||||
return &ErrUnsupportedType{T: val.Type()}
|
||||
}
|
||||
|
||||
func (mw *Writer) writeMap(v reflect.Value) (err error) {
|
||||
if v.Type().Key().Kind() != reflect.String {
|
||||
return errors.New("msgp: map keys must be strings")
|
||||
}
|
||||
ks := v.MapKeys()
|
||||
err = mw.WriteMapHeader(uint32(len(ks)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, key := range ks {
|
||||
val := v.MapIndex(key)
|
||||
err = mw.WriteString(key.String())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = mw.WriteIntf(val.Interface())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (mw *Writer) writeSlice(v reflect.Value) (err error) {
|
||||
// is []byte
|
||||
if v.Type().ConvertibleTo(btsType) {
|
||||
return mw.WriteBytes(v.Bytes())
|
||||
}
|
||||
|
||||
sz := uint32(v.Len())
|
||||
err = mw.WriteArrayHeader(sz)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for i := range sz {
|
||||
err = mw.WriteIntf(v.Index(int(i)).Interface())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// is the reflect.Kind encodable?
|
||||
func isSupported(k reflect.Kind) bool {
|
||||
switch k {
|
||||
case reflect.Func, reflect.Chan, reflect.Invalid, reflect.UnsafePointer:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// GuessSize guesses the size of the underlying
|
||||
// value of 'i'. If the underlying value is not
|
||||
// a simple builtin (or []byte), GuessSize defaults
|
||||
// to 512.
|
||||
func GuessSize(i any) int {
|
||||
if i == nil {
|
||||
return NilSize
|
||||
}
|
||||
|
||||
switch i := i.(type) {
|
||||
case Sizer:
|
||||
return i.Msgsize()
|
||||
case Extension:
|
||||
return ExtensionPrefixSize + i.Len()
|
||||
case float64:
|
||||
return Float64Size
|
||||
case float32:
|
||||
return Float32Size
|
||||
case uint8, uint16, uint32, uint64, uint:
|
||||
return UintSize
|
||||
case int8, int16, int32, int64, int:
|
||||
return IntSize
|
||||
case []byte:
|
||||
return BytesPrefixSize + len(i)
|
||||
case string:
|
||||
return StringPrefixSize + len(i)
|
||||
case complex64:
|
||||
return Complex64Size
|
||||
case complex128:
|
||||
return Complex128Size
|
||||
case bool:
|
||||
return BoolSize
|
||||
case map[string]any:
|
||||
s := MapHeaderSize
|
||||
for key, val := range i {
|
||||
s += StringPrefixSize + len(key) + GuessSize(val)
|
||||
}
|
||||
return s
|
||||
case map[string]string:
|
||||
s := MapHeaderSize
|
||||
for key, val := range i {
|
||||
s += 2*StringPrefixSize + len(key) + len(val)
|
||||
}
|
||||
return s
|
||||
default:
|
||||
return 512
|
||||
}
|
||||
}
|
||||
|
||||
// Temporary buffer for reading/writing binary data.
|
||||
var bytesPool = sync.Pool{New: func() any { return make([]byte, 0, 1024) }}
|
||||
|
||||
// WriteBinaryAppender will write the bytes from the given
|
||||
// encoding.BinaryAppender as a bin array.
|
||||
func (mw *Writer) WriteBinaryAppender(b encoding.BinaryAppender) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("msgp: panic during AppendBinary: %v", r)
|
||||
}
|
||||
}()
|
||||
dst := bytesPool.Get().([]byte)
|
||||
defer bytesPool.Put(dst) //nolint:staticcheck
|
||||
dst, err = b.AppendBinary(dst[:0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return mw.WriteBytes(dst)
|
||||
}
|
||||
|
||||
// WriteTextAppender will write the bytes from the given
|
||||
// encoding.TextAppender as a bin array.
|
||||
func (mw *Writer) WriteTextAppender(b encoding.TextAppender) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("msgp: panic during AppendText: %v", r)
|
||||
}
|
||||
}()
|
||||
dst := bytesPool.Get().([]byte)
|
||||
defer bytesPool.Put(dst) //nolint:staticcheck
|
||||
dst, err = b.AppendText(dst[:0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return mw.WriteBytes(dst)
|
||||
}
|
||||
|
||||
// WriteTextAppenderString will write the bytes from the given
|
||||
// encoding.TextAppender as a string.
|
||||
func (mw *Writer) WriteTextAppenderString(b encoding.TextAppender) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("msgp: panic during AppendText: %v", r)
|
||||
}
|
||||
}()
|
||||
dst := bytesPool.Get().([]byte)
|
||||
defer bytesPool.Put(dst) //nolint:staticcheck
|
||||
dst, err = b.AppendText(dst[:0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return mw.WriteStringFromBytes(dst)
|
||||
}
|
||||
566
vendor/github.com/tinylib/msgp/msgp/write_bytes.go
generated
vendored
Normal file
566
vendor/github.com/tinylib/msgp/msgp/write_bytes.go
generated
vendored
Normal file
@@ -0,0 +1,566 @@
|
||||
package msgp
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ensure 'sz' extra bytes in 'b' btw len(b) and cap(b)
|
||||
func ensure(b []byte, sz int) ([]byte, int) {
|
||||
l := len(b)
|
||||
c := cap(b)
|
||||
if c-l < sz {
|
||||
o := make([]byte, (2*c)+sz) // exponential growth
|
||||
n := copy(o, b)
|
||||
return o[:n+sz], n
|
||||
}
|
||||
return b[:l+sz], l
|
||||
}
|
||||
|
||||
// AppendMapHeader appends a map header with the
|
||||
// given size to the slice
|
||||
func AppendMapHeader(b []byte, sz uint32) []byte {
|
||||
switch {
|
||||
case sz <= 15:
|
||||
return append(b, wfixmap(uint8(sz)))
|
||||
|
||||
case sz <= math.MaxUint16:
|
||||
o, n := ensure(b, 3)
|
||||
prefixu16(o[n:], mmap16, uint16(sz))
|
||||
return o
|
||||
|
||||
default:
|
||||
o, n := ensure(b, 5)
|
||||
prefixu32(o[n:], mmap32, sz)
|
||||
return o
|
||||
}
|
||||
}
|
||||
|
||||
// AppendArrayHeader appends an array header with
|
||||
// the given size to the slice
|
||||
func AppendArrayHeader(b []byte, sz uint32) []byte {
|
||||
switch {
|
||||
case sz <= 15:
|
||||
return append(b, wfixarray(uint8(sz)))
|
||||
|
||||
case sz <= math.MaxUint16:
|
||||
o, n := ensure(b, 3)
|
||||
prefixu16(o[n:], marray16, uint16(sz))
|
||||
return o
|
||||
|
||||
default:
|
||||
o, n := ensure(b, 5)
|
||||
prefixu32(o[n:], marray32, sz)
|
||||
return o
|
||||
}
|
||||
}
|
||||
|
||||
// AppendNil appends a 'nil' byte to the slice
|
||||
func AppendNil(b []byte) []byte { return append(b, mnil) }
|
||||
|
||||
// AppendFloat appends a float to the slice as either float64
|
||||
// or float32 when it represents the exact same value
|
||||
func AppendFloat(b []byte, f float64) []byte {
|
||||
f32 := float32(f)
|
||||
if float64(f32) == f {
|
||||
return AppendFloat32(b, f32)
|
||||
}
|
||||
return AppendFloat64(b, f)
|
||||
}
|
||||
|
||||
// AppendFloat64 appends a float64 to the slice
|
||||
func AppendFloat64(b []byte, f float64) []byte {
|
||||
o, n := ensure(b, Float64Size)
|
||||
prefixu64(o[n:], mfloat64, math.Float64bits(f))
|
||||
return o
|
||||
}
|
||||
|
||||
// AppendFloat32 appends a float32 to the slice
|
||||
func AppendFloat32(b []byte, f float32) []byte {
|
||||
o, n := ensure(b, Float32Size)
|
||||
prefixu32(o[n:], mfloat32, math.Float32bits(f))
|
||||
return o
|
||||
}
|
||||
|
||||
// AppendDuration appends a time.Duration to the slice
|
||||
func AppendDuration(b []byte, d time.Duration) []byte {
|
||||
return AppendInt64(b, int64(d))
|
||||
}
|
||||
|
||||
// AppendInt64 appends an int64 to the slice
|
||||
func AppendInt64(b []byte, i int64) []byte {
|
||||
if i >= 0 {
|
||||
switch {
|
||||
case i <= math.MaxInt8:
|
||||
return append(b, wfixint(uint8(i)))
|
||||
case i <= math.MaxInt16:
|
||||
o, n := ensure(b, 3)
|
||||
putMint16(o[n:], int16(i))
|
||||
return o
|
||||
case i <= math.MaxInt32:
|
||||
o, n := ensure(b, 5)
|
||||
putMint32(o[n:], int32(i))
|
||||
return o
|
||||
default:
|
||||
o, n := ensure(b, 9)
|
||||
putMint64(o[n:], i)
|
||||
return o
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case i >= -32:
|
||||
return append(b, wnfixint(int8(i)))
|
||||
case i >= math.MinInt8:
|
||||
o, n := ensure(b, 2)
|
||||
putMint8(o[n:], int8(i))
|
||||
return o
|
||||
case i >= math.MinInt16:
|
||||
o, n := ensure(b, 3)
|
||||
putMint16(o[n:], int16(i))
|
||||
return o
|
||||
case i >= math.MinInt32:
|
||||
o, n := ensure(b, 5)
|
||||
putMint32(o[n:], int32(i))
|
||||
return o
|
||||
default:
|
||||
o, n := ensure(b, 9)
|
||||
putMint64(o[n:], i)
|
||||
return o
|
||||
}
|
||||
}
|
||||
|
||||
// AppendInt appends an int to the slice
|
||||
func AppendInt(b []byte, i int) []byte { return AppendInt64(b, int64(i)) }
|
||||
|
||||
// AppendInt8 appends an int8 to the slice
|
||||
func AppendInt8(b []byte, i int8) []byte { return AppendInt64(b, int64(i)) }
|
||||
|
||||
// AppendInt16 appends an int16 to the slice
|
||||
func AppendInt16(b []byte, i int16) []byte { return AppendInt64(b, int64(i)) }
|
||||
|
||||
// AppendInt32 appends an int32 to the slice
|
||||
func AppendInt32(b []byte, i int32) []byte { return AppendInt64(b, int64(i)) }
|
||||
|
||||
// AppendUint64 appends a uint64 to the slice
|
||||
func AppendUint64(b []byte, u uint64) []byte {
|
||||
switch {
|
||||
case u <= (1<<7)-1:
|
||||
return append(b, wfixint(uint8(u)))
|
||||
|
||||
case u <= math.MaxUint8:
|
||||
o, n := ensure(b, 2)
|
||||
putMuint8(o[n:], uint8(u))
|
||||
return o
|
||||
|
||||
case u <= math.MaxUint16:
|
||||
o, n := ensure(b, 3)
|
||||
putMuint16(o[n:], uint16(u))
|
||||
return o
|
||||
|
||||
case u <= math.MaxUint32:
|
||||
o, n := ensure(b, 5)
|
||||
putMuint32(o[n:], uint32(u))
|
||||
return o
|
||||
|
||||
default:
|
||||
o, n := ensure(b, 9)
|
||||
putMuint64(o[n:], u)
|
||||
return o
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// AppendUint appends a uint to the slice
|
||||
func AppendUint(b []byte, u uint) []byte { return AppendUint64(b, uint64(u)) }
|
||||
|
||||
// AppendUint8 appends a uint8 to the slice
|
||||
func AppendUint8(b []byte, u uint8) []byte { return AppendUint64(b, uint64(u)) }
|
||||
|
||||
// AppendByte is analogous to AppendUint8
|
||||
func AppendByte(b []byte, u byte) []byte { return AppendUint8(b, u) }
|
||||
|
||||
// AppendUint16 appends a uint16 to the slice
|
||||
func AppendUint16(b []byte, u uint16) []byte { return AppendUint64(b, uint64(u)) }
|
||||
|
||||
// AppendUint32 appends a uint32 to the slice
|
||||
func AppendUint32(b []byte, u uint32) []byte { return AppendUint64(b, uint64(u)) }
|
||||
|
||||
// AppendBytes appends bytes to the slice as MessagePack 'bin' data
|
||||
func AppendBytes(b []byte, bts []byte) []byte {
|
||||
sz := len(bts)
|
||||
var o []byte
|
||||
var n int
|
||||
switch {
|
||||
case sz <= math.MaxUint8:
|
||||
o, n = ensure(b, 2+sz)
|
||||
prefixu8(o[n:], mbin8, uint8(sz))
|
||||
n += 2
|
||||
case sz <= math.MaxUint16:
|
||||
o, n = ensure(b, 3+sz)
|
||||
prefixu16(o[n:], mbin16, uint16(sz))
|
||||
n += 3
|
||||
default:
|
||||
o, n = ensure(b, 5+sz)
|
||||
prefixu32(o[n:], mbin32, uint32(sz))
|
||||
n += 5
|
||||
}
|
||||
return o[:n+copy(o[n:], bts)]
|
||||
}
|
||||
|
||||
// AppendBytesHeader appends an 'bin' header with
|
||||
// the given size to the slice.
|
||||
func AppendBytesHeader(b []byte, sz uint32) []byte {
|
||||
var o []byte
|
||||
var n int
|
||||
switch {
|
||||
case sz <= math.MaxUint8:
|
||||
o, n = ensure(b, 2)
|
||||
prefixu8(o[n:], mbin8, uint8(sz))
|
||||
return o
|
||||
case sz <= math.MaxUint16:
|
||||
o, n = ensure(b, 3)
|
||||
prefixu16(o[n:], mbin16, uint16(sz))
|
||||
return o
|
||||
}
|
||||
o, n = ensure(b, 5)
|
||||
prefixu32(o[n:], mbin32, sz)
|
||||
return o
|
||||
}
|
||||
|
||||
// AppendBool appends a bool to the slice
|
||||
func AppendBool(b []byte, t bool) []byte {
|
||||
if t {
|
||||
return append(b, mtrue)
|
||||
}
|
||||
return append(b, mfalse)
|
||||
}
|
||||
|
||||
// AppendString appends a string as a MessagePack 'str' to the slice
|
||||
func AppendString(b []byte, s string) []byte {
|
||||
sz := len(s)
|
||||
var n int
|
||||
var o []byte
|
||||
switch {
|
||||
case sz <= 31:
|
||||
o, n = ensure(b, 1+sz)
|
||||
o[n] = wfixstr(uint8(sz))
|
||||
n++
|
||||
case sz <= math.MaxUint8:
|
||||
o, n = ensure(b, 2+sz)
|
||||
prefixu8(o[n:], mstr8, uint8(sz))
|
||||
n += 2
|
||||
case sz <= math.MaxUint16:
|
||||
o, n = ensure(b, 3+sz)
|
||||
prefixu16(o[n:], mstr16, uint16(sz))
|
||||
n += 3
|
||||
default:
|
||||
o, n = ensure(b, 5+sz)
|
||||
prefixu32(o[n:], mstr32, uint32(sz))
|
||||
n += 5
|
||||
}
|
||||
return o[:n+copy(o[n:], s)]
|
||||
}
|
||||
|
||||
// AppendStringFromBytes appends a []byte
|
||||
// as a MessagePack 'str' to the slice 'b.'
|
||||
func AppendStringFromBytes(b []byte, str []byte) []byte {
|
||||
sz := len(str)
|
||||
var n int
|
||||
var o []byte
|
||||
switch {
|
||||
case sz <= 31:
|
||||
o, n = ensure(b, 1+sz)
|
||||
o[n] = wfixstr(uint8(sz))
|
||||
n++
|
||||
case sz <= math.MaxUint8:
|
||||
o, n = ensure(b, 2+sz)
|
||||
prefixu8(o[n:], mstr8, uint8(sz))
|
||||
n += 2
|
||||
case sz <= math.MaxUint16:
|
||||
o, n = ensure(b, 3+sz)
|
||||
prefixu16(o[n:], mstr16, uint16(sz))
|
||||
n += 3
|
||||
default:
|
||||
o, n = ensure(b, 5+sz)
|
||||
prefixu32(o[n:], mstr32, uint32(sz))
|
||||
n += 5
|
||||
}
|
||||
return o[:n+copy(o[n:], str)]
|
||||
}
|
||||
|
||||
// AppendComplex64 appends a complex64 to the slice as a MessagePack extension
|
||||
func AppendComplex64(b []byte, c complex64) []byte {
|
||||
o, n := ensure(b, Complex64Size)
|
||||
o[n] = mfixext8
|
||||
o[n+1] = Complex64Extension
|
||||
big.PutUint32(o[n+2:], math.Float32bits(real(c)))
|
||||
big.PutUint32(o[n+6:], math.Float32bits(imag(c)))
|
||||
return o
|
||||
}
|
||||
|
||||
// AppendComplex128 appends a complex128 to the slice as a MessagePack extension
|
||||
func AppendComplex128(b []byte, c complex128) []byte {
|
||||
o, n := ensure(b, Complex128Size)
|
||||
o[n] = mfixext16
|
||||
o[n+1] = Complex128Extension
|
||||
big.PutUint64(o[n+2:], math.Float64bits(real(c)))
|
||||
big.PutUint64(o[n+10:], math.Float64bits(imag(c)))
|
||||
return o
|
||||
}
|
||||
|
||||
// AppendTime appends a time.Time to the slice as a MessagePack extension
|
||||
func AppendTime(b []byte, t time.Time) []byte {
|
||||
o, n := ensure(b, TimeSize)
|
||||
t = t.UTC()
|
||||
o[n] = mext8
|
||||
o[n+1] = 12
|
||||
o[n+2] = TimeExtension
|
||||
putUnix(o[n+3:], t.Unix(), int32(t.Nanosecond()))
|
||||
return o
|
||||
}
|
||||
|
||||
// AppendTimeExt will write t using the official msgpack extension spec.
|
||||
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
|
||||
func AppendTimeExt(b []byte, t time.Time) []byte {
|
||||
// Time rounded towards zero.
|
||||
secPrec := t.Truncate(time.Second)
|
||||
remain := t.Sub(secPrec).Nanoseconds()
|
||||
asSecs := secPrec.Unix()
|
||||
switch {
|
||||
case remain == 0 && asSecs > 0 && asSecs <= math.MaxUint32:
|
||||
// 4 bytes
|
||||
o, n := ensure(b, 2+4)
|
||||
o[n+0] = mfixext4
|
||||
o[n+1] = byte(msgTimeExtension)
|
||||
binary.BigEndian.PutUint32(o[n+2:], uint32(asSecs))
|
||||
return o
|
||||
case asSecs < 0 || asSecs >= (1<<34):
|
||||
// 12 bytes
|
||||
o, n := ensure(b, 3+12)
|
||||
o[n+0] = mext8
|
||||
o[n+1] = 12
|
||||
o[n+2] = byte(msgTimeExtension)
|
||||
binary.BigEndian.PutUint32(o[n+3:], uint32(remain))
|
||||
binary.BigEndian.PutUint64(o[n+3+4:], uint64(asSecs))
|
||||
return o
|
||||
default:
|
||||
// 8 bytes
|
||||
o, n := ensure(b, 2+8)
|
||||
o[n+0] = mfixext8
|
||||
o[n+1] = byte(msgTimeExtension)
|
||||
binary.BigEndian.PutUint64(o[n+2:], uint64(asSecs)|(uint64(remain)<<34))
|
||||
return o
|
||||
}
|
||||
}
|
||||
|
||||
// AppendMapStrStr appends a map[string]string to the slice
|
||||
// as a MessagePack map with 'str'-type keys and values
|
||||
func AppendMapStrStr(b []byte, m map[string]string) []byte {
|
||||
sz := uint32(len(m))
|
||||
b = AppendMapHeader(b, sz)
|
||||
for key, val := range m {
|
||||
b = AppendString(b, key)
|
||||
b = AppendString(b, val)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// AppendMapStrIntf appends a map[string]interface{} to the slice
|
||||
// as a MessagePack map with 'str'-type keys.
|
||||
func AppendMapStrIntf(b []byte, m map[string]any) ([]byte, error) {
|
||||
sz := uint32(len(m))
|
||||
b = AppendMapHeader(b, sz)
|
||||
var err error
|
||||
for key, val := range m {
|
||||
b = AppendString(b, key)
|
||||
b, err = AppendIntf(b, val)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// AppendIntf appends the concrete type of 'i' to the
|
||||
// provided []byte. 'i' must be one of the following:
|
||||
// - 'nil'
|
||||
// - A bool, float, string, []byte, int, uint, or complex
|
||||
// - A map[string]T where T is another supported type
|
||||
// - A []T, where T is another supported type
|
||||
// - A *T, where T is another supported type
|
||||
// - A type that satisfies the msgp.Marshaler interface
|
||||
// - A type that satisfies the msgp.Extension interface
|
||||
func AppendIntf(b []byte, i any) ([]byte, error) {
|
||||
if i == nil {
|
||||
return AppendNil(b), nil
|
||||
}
|
||||
|
||||
// all the concrete types
|
||||
// for which we have methods
|
||||
switch i := i.(type) {
|
||||
case Marshaler:
|
||||
return i.MarshalMsg(b)
|
||||
case Extension:
|
||||
return AppendExtension(b, i)
|
||||
case bool:
|
||||
return AppendBool(b, i), nil
|
||||
case float32:
|
||||
return AppendFloat32(b, i), nil
|
||||
case float64:
|
||||
return AppendFloat64(b, i), nil
|
||||
case complex64:
|
||||
return AppendComplex64(b, i), nil
|
||||
case complex128:
|
||||
return AppendComplex128(b, i), nil
|
||||
case string:
|
||||
return AppendString(b, i), nil
|
||||
case []byte:
|
||||
return AppendBytes(b, i), nil
|
||||
case int8:
|
||||
return AppendInt8(b, i), nil
|
||||
case int16:
|
||||
return AppendInt16(b, i), nil
|
||||
case int32:
|
||||
return AppendInt32(b, i), nil
|
||||
case int64:
|
||||
return AppendInt64(b, i), nil
|
||||
case int:
|
||||
return AppendInt64(b, int64(i)), nil
|
||||
case uint:
|
||||
return AppendUint64(b, uint64(i)), nil
|
||||
case uint8:
|
||||
return AppendUint8(b, i), nil
|
||||
case uint16:
|
||||
return AppendUint16(b, i), nil
|
||||
case uint32:
|
||||
return AppendUint32(b, i), nil
|
||||
case uint64:
|
||||
return AppendUint64(b, i), nil
|
||||
case time.Time:
|
||||
return AppendTime(b, i), nil
|
||||
case time.Duration:
|
||||
return AppendDuration(b, i), nil
|
||||
case map[string]any:
|
||||
return AppendMapStrIntf(b, i)
|
||||
case map[string]string:
|
||||
return AppendMapStrStr(b, i), nil
|
||||
case json.Number:
|
||||
return AppendJSONNumber(b, i)
|
||||
case []any:
|
||||
b = AppendArrayHeader(b, uint32(len(i)))
|
||||
var err error
|
||||
for _, k := range i {
|
||||
b, err = AppendIntf(b, k)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
var err error
|
||||
v := reflect.ValueOf(i)
|
||||
switch v.Kind() {
|
||||
case reflect.Map:
|
||||
if v.Type().Key().Kind() != reflect.String {
|
||||
return b, errors.New("msgp: map keys must be strings")
|
||||
}
|
||||
ks := v.MapKeys()
|
||||
b = AppendMapHeader(b, uint32(len(ks)))
|
||||
for _, key := range ks {
|
||||
val := v.MapIndex(key)
|
||||
b = AppendString(b, key.String())
|
||||
b, err = AppendIntf(b, val.Interface())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
case reflect.Array, reflect.Slice:
|
||||
l := v.Len()
|
||||
b = AppendArrayHeader(b, uint32(l))
|
||||
for i := range l {
|
||||
b, err = AppendIntf(b, v.Index(i).Interface())
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
case reflect.Ptr:
|
||||
if v.IsNil() {
|
||||
return AppendNil(b), err
|
||||
}
|
||||
b, err = AppendIntf(b, v.Elem().Interface())
|
||||
return b, err
|
||||
default:
|
||||
return b, &ErrUnsupportedType{T: v.Type()}
|
||||
}
|
||||
}
|
||||
|
||||
// AppendJSONNumber appends a json.Number to the slice.
|
||||
// An error will be returned if the json.Number returns error as both integer and float.
|
||||
func AppendJSONNumber(b []byte, n json.Number) ([]byte, error) {
|
||||
if n == "" {
|
||||
// The zero value outputs the 0 integer.
|
||||
return append(b, 0), nil
|
||||
}
|
||||
ii, err := n.Int64()
|
||||
if err == nil {
|
||||
return AppendInt64(b, ii), nil
|
||||
}
|
||||
ff, err := n.Float64()
|
||||
if err == nil {
|
||||
return AppendFloat(b, ff), nil
|
||||
}
|
||||
return b, err
|
||||
}
|
||||
|
||||
// AppendBytesTwoPrefixed will add the length to a bin section written with
|
||||
// 2 bytes of space saved for a bin8 header.
|
||||
// If the sz cannot fit inside a bin8, the data will be moved to make space for the header.
|
||||
func AppendBytesTwoPrefixed(b []byte, sz int) []byte {
|
||||
off := len(b) - sz - 2
|
||||
switch {
|
||||
case sz <= math.MaxUint8:
|
||||
// Just write header...
|
||||
prefixu8(b[off:], mbin8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
// Scoot one
|
||||
b = append(b, 0)
|
||||
copy(b[off+1:], b[off:])
|
||||
prefixu16(b[off:], mbin16, uint16(sz))
|
||||
default:
|
||||
// Scoot three
|
||||
b = append(b, 0, 0, 0)
|
||||
copy(b[off+3:], b[off:])
|
||||
prefixu32(b[off:], mbin32, uint32(sz))
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// AppendBytesStringTwoPrefixed will add the length to a string section written with
|
||||
// 2 bytes of space saved for a str8 header.
|
||||
// If the sz cannot fit inside a str8, the data will be moved to make space for the header.
|
||||
func AppendBytesStringTwoPrefixed(b []byte, sz int) []byte {
|
||||
off := len(b) - sz - 2
|
||||
switch {
|
||||
case sz <= math.MaxUint8:
|
||||
// Just write header...
|
||||
prefixu8(b[off:], mstr8, uint8(sz))
|
||||
case sz <= math.MaxUint16:
|
||||
// Scoot one
|
||||
b = append(b, 0)
|
||||
copy(b[off+1:], b[off:])
|
||||
prefixu16(b[off:], mstr16, uint16(sz))
|
||||
default:
|
||||
// Scoot three
|
||||
b = append(b, 0, 0, 0)
|
||||
copy(b[off+3:], b[off:])
|
||||
prefixu32(b[off:], mstr32, uint32(sz))
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in New Issue
Block a user