encoding

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package encoding provides types and functions to encode and decode documents and values.

Encoding values

Each type is encoded in a way that allows ordering to be preserved. That way, if vA < vB, where vA and vB are two unencoded values of the same type, then eA < eB, where eA and eB are the respective encoded values of vA and vB.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeArray

func DecodeArray(buf []byte) document.Array

DecodeArray takes a byte slice and returns a lazily decoded array. If buf is malformed, an error will be returned when calling one of the array method.

func DecodeBlob added in v0.5.0

func DecodeBlob(buf []byte) ([]byte, error)

DecodeBlob takes a byte slice and returns it. It is present to ease code generation.

func DecodeBool

func DecodeBool(buf []byte) (bool, error)

DecodeBool takes a byte slice and decodes it into a boolean.

func DecodeDocument

func DecodeDocument(buf []byte) document.Document

DecodeDocument takes a byte slice and returns a lazily decoded document. If buf is malformed, an error will be returned when calling one of the document method.

func DecodeFloat64

func DecodeFloat64(buf []byte) (float64, error)

DecodeFloat64 takes a byte slice and decodes it into an float64.

func DecodeInt

func DecodeInt(buf []byte) (int, error)

DecodeInt takes a byte slice and decodes it into an int.

func DecodeInt16

func DecodeInt16(buf []byte) (int16, error)

DecodeInt16 takes a byte slice and decodes it into an int16.

func DecodeInt32

func DecodeInt32(buf []byte) (int32, error)

DecodeInt32 takes a byte slice and decodes it into an int32.

func DecodeInt64

func DecodeInt64(buf []byte) (int64, error)

DecodeInt64 takes a byte slice and decodes it into an int64.

func DecodeInt8

func DecodeInt8(buf []byte) (int8, error)

DecodeInt8 takes a byte slice and decodes it into an int8.

func DecodeText added in v0.5.0

func DecodeText(buf []byte) (string, error)

DecodeText takes a byte slice and decodes it into a string.

func DecodeUint

func DecodeUint(buf []byte) (uint, error)

DecodeUint takes a byte slice and decodes it into a uint.

func DecodeUint16

func DecodeUint16(buf []byte) (uint16, error)

DecodeUint16 takes a byte slice and decodes it into a uint16.

func DecodeUint32

func DecodeUint32(buf []byte) (uint32, error)

DecodeUint32 takes a byte slice and decodes it into a uint32.

func DecodeUint64

func DecodeUint64(buf []byte) (uint64, error)

DecodeUint64 takes a byte slice and decodes it into a uint64.

func DecodeUint8

func DecodeUint8(buf []byte) (uint8, error)

DecodeUint8 takes a byte slice and decodes it into a uint8.

func DecodeValue

func DecodeValue(t document.ValueType, data []byte) (document.Value, error)

DecodeValue takes some encoded data and decodes it to the target type t.

func EncodeArray

func EncodeArray(a document.Array) ([]byte, error)

EncodeArray encodes a into its binary representation.

func EncodeBlob added in v0.5.0

func EncodeBlob(x []byte) []byte

EncodeBlob takes a blob and returns it. It is present to ease code generation.

func EncodeBool

func EncodeBool(x bool) []byte

EncodeBool takes a bool and returns its binary representation.

func EncodeDocument

func EncodeDocument(d document.Document) ([]byte, error)

EncodeDocument takes a document and encodes it using the encoding.Format type.

func EncodeFloat64

func EncodeFloat64(x float64) []byte

EncodeFloat64 takes an float64 and returns its binary representation.

func EncodeInt

func EncodeInt(x int) []byte

EncodeInt takes an int and returns its binary representation.

func EncodeInt16

func EncodeInt16(x int16) []byte

EncodeInt16 takes an int16 and returns its binary representation.

func EncodeInt32

func EncodeInt32(x int32) []byte

EncodeInt32 takes an int32 and returns its binary representation.

func EncodeInt64

func EncodeInt64(x int64) []byte

EncodeInt64 takes an int64 and returns its binary representation.

func EncodeInt8

func EncodeInt8(x int8) []byte

EncodeInt8 takes an int8 and returns its binary representation.

func EncodeText added in v0.5.0

func EncodeText(x string) []byte

EncodeText takes a string and returns its binary representation.

func EncodeUint

func EncodeUint(x uint) []byte

EncodeUint takes an uint and returns its binary representation.

func EncodeUint16

func EncodeUint16(x uint16) []byte

EncodeUint16 takes an uint16 and returns its binary representation.

func EncodeUint32

func EncodeUint32(x uint32) []byte

EncodeUint32 takes an uint32 and returns its binary representation.

func EncodeUint64

func EncodeUint64(x uint64) []byte

EncodeUint64 takes an uint64 and returns its binary representation.

func EncodeUint8

func EncodeUint8(x uint8) []byte

EncodeUint8 takes an uint8 and returns its binary representation.

func EncodeValue

func EncodeValue(v document.Value) ([]byte, error)

EncodeValue encodes any value to a binary representation.

Types

type EncodedArray

type EncodedArray []byte

An EncodedArray implements the document.Array interface on top of an encoded representation of an array. It is useful to avoid decoding the entire array when only a few values are needed.

func (EncodedArray) GetByIndex

func (e EncodedArray) GetByIndex(i int) (document.Value, error)

GetByIndex returns a value by index of the array.

func (EncodedArray) Iterate

func (e EncodedArray) Iterate(fn func(i int, value document.Value) error) error

Iterate goes through all the values of the array and calls the given function by passing each one of them. If the given function returns an error, the iteration stops.

type EncodedDocument

type EncodedDocument []byte

An EncodedDocument implements the document.Document interface on top of an encoded representation of a document. It is useful to avoid decoding the entire document when only a few fields are needed.

func (EncodedDocument) GetByField

func (e EncodedDocument) GetByField(field string) (document.Value, error)

GetByField decodes the selected field.

func (EncodedDocument) Iterate

func (e EncodedDocument) Iterate(fn func(name string, value document.Value) error) error

Iterate decodes each fields one by one and passes them to fn until the end of the document or until fn returns an error.

type FieldHeader

type FieldHeader struct {
	// Size of the name of the field
	NameSize uint64
	// Name of the field
	Name []byte
	// Type of the field, corresponds to the Type
	Type uint64
	// Size of the data of the field
	Size uint64
	// Offset describing where the field is located, starting
	// from the end of the format header.
	Offset uint64

	NameString string // used for encoding
	// contains filtered or unexported fields
}

FieldHeader represents the metadata of a field.

func (*FieldHeader) Decode

func (f *FieldHeader) Decode(data []byte) (int, error)

Decode the data into the field header.

func (*FieldHeader) WriteTo

func (f *FieldHeader) WriteTo(w io.Writer) (int64, error)

WriteTo encodes the field header into w.

type Format

type Format struct {
	Header Header
	Body   []byte
}

Format is an encoding format used to encode and decode documents. It is composed of a header and a body. The header defines a list of fields, offsets and relevant metadata. The body contains each fields data one concatenated one after another.

func (*Format) Decode

func (f *Format) Decode(data []byte) error

Decode the given data into the format.

type Header struct {
	// Size of the header
	Size uint64
	// Number of fields
	FieldsCount uint64
	// List of headers for all the fields.
	FieldHeaders []FieldHeader
}

A Header contains a representation of a document's metadata.

func (*Header) BodySize

func (h *Header) BodySize() int

BodySize returns the size of the body.

func (*Header) Decode

func (h *Header) Decode(data []byte) (int, error)

Decode data into the header.

func (*Header) WriteTo

func (h *Header) WriteTo(w io.Writer) (int64, error)

WriteTo encodes the header into w.

Jump to

Keyboard shortcuts

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