encoding

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2015 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoData = errors.New("no data")
)

Functions

func DecInt64

func DecInt64(b []byte) int64

DecInt64 decodes a slice of 8 bytes into an int64. If len(b) < 8, the slice is padded with zeros.

func DecUint64

func DecUint64(b []byte) uint64

DecUint64 decodes a slice of 8 bytes into a uint64. If len(b) < 8, the slice is padded with zeros.

func EncInt64

func EncInt64(i int64) (b []byte)

EncInt64 encodes an int64 as a slice of 8 bytes.

func EncUint64

func EncUint64(i uint64) (b []byte)

EncUint64 encodes a uint64 as a slice of 8 bytes.

func Marshal

func Marshal(v interface{}) []byte

Marshal encodes a value as a byte slice. The encoding rules are as follows:

Most types are encoded as their binary representation.

Integers are little-endian, and are always encoded as 8 bytes, i.e. their int64 or uint64 equivalent.

Booleans are encoded as one byte, either zero (false) or non-zero (true).

Nil pointers are represented by a zero.

Valid pointers are prefaced by a non-zero, followed by the dereferenced value.

Variable-length types, such as strings and slices, are prefaced by 8 bytes containing their length.

Slices and structs are simply the concatenation of their encoded elements. Byte slices are not subject to the 8-byte integer rule; they are encoded as their literal representation, one byte per byte. The ordering of struct fields is determined by their type definition. For example:

type foo struct { S string I int }

Marshal(foo{"bar", 3}) = append(Marshal("bar"), Marshal(3)...)

func MarshalAll

func MarshalAll(v ...interface{}) (b []byte)

MarshalAll marshals all of its inputs and returns their concatenation.

func ReadObject

func ReadObject(r io.Reader, obj interface{}, maxLen uint64) error

ReadObject reads and decodes a length-prefixed and marshalled object.

func ReadPrefix

func ReadPrefix(r io.Reader, maxLen uint64) ([]byte, error)

ReadPrefix reads an 8-byte length prefixes, followed by the number of bytes specified in the prefix. The operation is aborted if the prefix exceeds a specified maximum length.

func Unmarshal

func Unmarshal(b []byte, v interface{}) (err error)

Unmarshal decodes a byte slice into the provided interface. The interface must be a pointer. The decoding rules are the inverse of those described under Marshal.

func WriteObject

func WriteObject(w io.Writer, obj interface{}) error

WriteObject encodes an object and prepends it with a 4-byte length before writing it.

func WritePrefix

func WritePrefix(w io.Writer, data []byte) error

WritePrefix prepends data with a 4-byte length before writing it.

Types

type ReadWriter added in v0.3.0

type ReadWriter interface {
	Reader
	Writer
}

A ReadWriter can both read and write objects.

type Reader added in v0.3.0

type Reader interface {
	ReadObject(obj interface{}, maxLen uint64) error
}

A Reader can decode an object from its input. maxLen specifies the maximum length of the object being decoded.

type SiaMarshaler

type SiaMarshaler interface {
	MarshalSia() []byte
}

A Marshaler can be encoded as a byte slice. Marshaler and Unmarshaler are separate interfaces because Unmarshaler must have a pointer receiver, while Marshaler does not.

type SiaUnmarshaler

type SiaUnmarshaler interface {
	UnmarshalSia([]byte) int
}

An Unmarshaler can be decoded from a byte slice. UnmarshalSia may be passed a byte slice containing more than one encoded type. It should return the number of bytes used to decode itself. UnmarshalSia can panic; the panic will be caught by Unmarshal. Alternatively, if UnmarshalSia returns a negative value, Unmarshal will always fail.

type Writer added in v0.3.0

type Writer interface {
	WriteObject(obj interface{}) error
}

A Writer can encode objects to its output.

Jump to

Keyboard shortcuts

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