Documentation
¶
Index ¶
- Constants
- Variables
- func BinaryMarshal(b Binary, buf *Buffer)
- func Bool(b *bool, buf *Buffer)
- func Byte(b *byte, buf *Buffer)
- func ByteSlice(s *[]byte, buf *Buffer)
- func FInt(n *int, buf *Buffer)
- func FInt64(n *int64, buf *Buffer)
- func FUInt16(n *uint16, buf *Buffer)
- func FUInt32(n *uint32, buf *Buffer)
- func FUInt64(n *uint64, buf *Buffer)
- func Float64(n *float64, buf *Buffer)
- func FromBytes[T any](data []byte, fn PackFn[T]) *T
- func FromBytesInto[T any](data []byte, obj *T, fn PackFn[T]) bool
- func Int(n *int, buf *Buffer)
- func IntEnum[T IntBased](n *T, buf *Buffer)
- func Map[K comparable, T any](m *map[K]T, keyFn PackFn[K], valFn PackFn[T], buf *Buffer)
- func PackUUID(id *UUID, buf *Buffer)
- func Rune(r *rune, buf *Buffer)
- func Slice[T any](list *[]T, fn PackFn[T], buf *Buffer)
- func String(s *string, buf *Buffer)
- func StringZ(s *string, buf *Buffer)
- func Time(t *time.Time, buf *Buffer)
- func ToBytes[T any](obj *T, fn PackFn[T]) []byte
- func UInt(n *uint, buf *Buffer)
- func UnixTime(t *time.Time, buf *Buffer)
- func UnixTimeKey(t *time.Time, buf *Buffer)
- func UnixTimeMilli(t *time.Time, buf *Buffer)
- func UnixTimeMilliKey(t *time.Time, buf *Buffer)
- func VInt64(n *int64, buf *Buffer)
- func VUInt64(n *uint64, buf *Buffer)
- func Version(current int, buf *Buffer) int
- type Binary
- type Buffer
- type IntBased
- type PackFn
- type UUID
Constants ¶
const UUID_SIZE = 16
Variables ¶
var BigEndian = binary.BigEndian
var GenericError = errors.New("Deserialization error")
var InvalidUUIDSize = errors.New("InvalidUUIDSize")
Functions ¶
func BinaryMarshal ¶
BinaryMarshal implements serialization for an object that implements the BinaryMarshaler and BinaryUnmarshaler interfaces from the standard library.
func ByteSlice ¶
ByteSlice implements serialization for a byte slice. It's more or less just like String.
func FInt ¶
FInt implements fixed size serialization of int (as 64 bits). It writes data in big endian, making it suitable for int keys to bolt.
func FInt64 ¶
FInt64 implements fixed size serialization of int64. It writes data in big endian, making it suitable for int keys to bolt.
func FUInt16 ¶
FUInt16 implements fixed size serialization of uint16. It writes data in big endian, making it suitable for int keys to bolt.
func FUInt32 ¶
FUInt32 implements fixed size serialization of uint32. It writes data in big endian, making it suitable for int keys to bolt.
func FUInt64 ¶
FUInt64 implements fixed size serialization of uint64. It writes data in big endian, making it suitable for int keys to bolt.
func Int ¶
Int implements varint encoding for int (as int64). Varint users fewer bytes for small values.
func Slice ¶
Slice is a helper for serialization a slice of some type, given its serialization function. It starts by reading/writing the length of the slice, then uses the provided serialization function to serialize each individual item in the slice.
func String ¶
String implements serialization for a string by first writing out the length in bytes (as a varint) then dumping the actual bytes into the buffer. When deserializing, it starts by reading the length (as a varint) then taking a slice of the input buffer and cloning it to a string
func StringZ ¶
StringZ implement serialization for a string using null-byte termination. This allows is to be used in the key of a boltdb key
func Time ¶
Time implement serialization for the std library's Time object using the Binary Marshalling interface
func UInt ¶
UInt implements varint encoding for uint (as uint64). Varint users fewer bytes for small values.
func UnixTime ¶
UnixTime serializes Time as a unix timestamp, in other wrods, the resolution is truncated to the seconds level, and the location data is omitted. It also uses variable encoding to take as little space as possible.
It can store a reasonably accurate timestamp in 5 or 6 bytes.
If you require subsecond accuracy, don't use this function.
func UnixTimeKey ¶
UnixTimeKey is similar to UnixTime, but uses fixed encoding so the value is suitable for a bucket key so we can iterate by timestamp
If you require subsecond accuracy, don't use this function.
func UnixTimeMilli ¶
UnixTimeMilli is similar to UnixTime but truncates to the MilliSecond level making it more suitable for cases where sub-second accuracy is required
func UnixTimeMilliKey ¶
UnixTimeMilliKey is similar to UnixTimeMilli, but uses fixed encoding so the value is suitable for a bucket key so we can iterate by timestamp
func VInt64 ¶
VInt64 implements varint encoding for int64. Varint users fewer bytes for small values.
Types ¶
type Binary ¶
type Binary interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
type Buffer ¶
type Buffer struct {
Data []byte
Pos int // reading position; not used for writing
Error bool // TODO: something better than this to report errors with more info?
Writing bool
}
Buffer is a byte buffer used to serialize data into, or deserialize data from, depending on the mode.
func NewReader ¶
NewReader prepares a Buffer for deserializing data from the backing byte buffer. The caller owns the data.
func NewWriter ¶
func NewWriter() *Buffer
NewWriter prepares a buffer for serializing data into. The backing buffer is owned by Buffer, but when serialization is done, the caller may use it.
func (*Buffer) EnsureSpace ¶
Ensure there's at least n bytes in the buffer starting from current position
func (*Buffer) ReadBytes ¶
ReadBytes does not expand the buffer to fit the required size. Instead, if there's no enough size, it sets the error flag.
func (*Buffer) ReadingDone ¶
func (*Buffer) WriteBytes ¶
type PackFn ¶ added in v0.1.3
PackFn is a generic serialization function that can be used either to serialize or deserialize data, depending on the buffer's mode.
type UUID ¶
UUID is a continous buffer of 16 bytes
func GenerateUUID ¶
func GenerateUUID() UUID
GenerateUUID creates a UUID (a 16 byte buffer) and fills it with bytes from the cryptographically secure random generator provided by the OS
func (*UUID) FromString ¶
func (UUID) MarshalJSON ¶
MarshalJSON implements the json marshalling interface for UUID
func (*UUID) UnmarshalJSON ¶
UnmarshalJSON implements the json marshalling interface for UUID