serial

package module
v0.0.0-...-ed9024c Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: BSD-3-Clause Imports: 1 Imported by: 6

README

Serial

Collection of helper functions for serializing data.

GoDoc

Documentation

Overview

Package serial provides helper function for serializing data.

Index

Constants

View Source
const (
	// ErrLengthsDoNotMatch is returned when a ByteSlicesPrefixer is given data
	// with the wrong number of byte slices.
	ErrLengthsDoNotMatch = errors.String("Lengths do not match")
	// ErrLengthTooLong is returned if a ByteSlicesPrefixer length is above 8.
	ErrLengthTooLong = errors.String("Length cannot exceed 8 bytes")
	// ErrIncorrectZero is returned when a BySlicesPrefixer has a zero in any but
	// the last position.
	ErrIncorrectZero = errors.String("Zero is only valid as final length")
	// ErrBadFormat is returned during ByteSlicesPrefixer Unmarshal if the format
	// does not match the description.
	ErrBadFormat = errors.String("Not properly formatted")
)

Variables

This section is empty.

Functions

func MarshalBoolSlice

func MarshalBoolSlice(bls []bool, b []byte)

MarshalBoolSlice marshals a slice of bools into a byte slice. Each bool takes only one bit. It does not check the slice length and will panic if the slice is not long enough. The required length with be the length of the bool slice divided by 8 rounded up plus 4.

func MarshalByteSlices

func MarshalByteSlices(prefixLens []int, data [][]byte) ([]byte, error)

MarshalByteSlices takes a slice of byte slices and marshals them into a a single byte slice. The prefixLens determine how many bytes to use for length headers. Positive values set the length in bytes, values less than or equal to 0 will result in no prefix being added.

func MarshalUint16

func MarshalUint16(ui uint16, b []byte) []byte

MarshalUint16 takes a unit and a byte slice and writes the uint16 to the first 2 bytes of the slice. It does not check the slice length and will panic if the slice does not have a length of at least 2.

func MarshalUint32

func MarshalUint32(ui uint32, b []byte) []byte

MarshalUint32 takes a unit and a byte slice and writes the uint32 to the first 4 bytes of the slice. It does not check the slice length and will panic if the slice does not have a length of at least 4.

func UnmarshalBoolSlice

func UnmarshalBoolSlice(b []byte) []bool

UnmarshalBoolSlice will unmarshal a slice of bools from a byte slice. If the byte slice is malformed, it may panic.

func UnmarshalByteSlices

func UnmarshalByteSlices(prefixLens []int, b []byte) ([][]byte, error)

UnmarshalByteSlices takes a byte slice and breaks it into a slice of byte slices. The prefixes determine what length headers to expect. Postive values are interpreted as the number of bytes to read as the length header. Negative values indicate the absolute length (so -5 means the slice is 5 bytes long) and 0 is only valid for the final value and means consume the rest.

func UnmarshalUint16

func UnmarshalUint16(b []byte) uint16

UnmarshalUint16 reads the first 2 bytes of a slice into a uint16. It does not check the slice length and will panic if the slice does not have a length of at least 2.

func UnmarshalUint32

func UnmarshalUint32(b []byte) uint32

UnmarshalUint32 reads the first 4 bytes of a slice into a uint32. It does not check the slice length and will panic if the slice does not have a length of at least 4.

Types

type ByteSlicesPrefixer

type ByteSlicesPrefixer []int

ByteSlicesPrefixer describes how to prefix a [][]byte for serialization.

func (ByteSlicesPrefixer) Marshal

func (pre ByteSlicesPrefixer) Marshal(data [][]byte) ([]byte, error)

Marshal uses the ByteSlicesPrefixer to marshal a [][]byte and insert length information.

func (ByteSlicesPrefixer) Unmarshal

func (pre ByteSlicesPrefixer) Unmarshal(b []byte) ([][]byte, error)

Unmarshal uses the ByteSlicesPrefixer to unmarshal a []byte int a [][]byte by extracting length information.

type SlicesPacker

type SlicesPacker struct {
	Count int
	Size  int
}

SlicesPacker describes the number of bytes to use to describe the count of slices and Size describes the number of bytes to use to describe the size of each slice

func (SlicesPacker) Marshal

func (s SlicesPacker) Marshal(data [][]byte) ([]byte, error)

Marshal a [][]byte into []byte inserting a header with total number of slices and prefixing the length of each slice.

func (SlicesPacker) Unmarshal

func (s SlicesPacker) Unmarshal(data []byte) ([][]byte, error)

Unmarshal a []byte to a [][]byte using the SlicesPacker to extract the total count and the size of each.

Jump to

Keyboard shortcuts

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