custom

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

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 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 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 EncodeDocument

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

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

func EncodeValue

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

EncodeValue encodes any value to a binary representation.

Types

type Codec

type Codec struct{}

A Codec is a custom implementation of an encoding.Codec.

func NewCodec

func NewCodec() Codec

NewCodec creates a custom codec.

func (Codec) NewDocument

func (c Codec) NewDocument(data []byte) document.Document

NewDocument implements the encoding.Codec interface.

func (Codec) NewEncoder

func (c Codec) NewEncoder(w io.Writer) encoding.Encoder

NewEncoder implements the encoding.Codec interface.

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.

func (EncodedArray) MarshalJSON

func (e EncodedArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

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(field 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.

func (EncodedDocument) MarshalJSON

func (e EncodedDocument) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder encodes Genji documents and values in MessagePack.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates an Encoder that writes in the given writer.

func (*Encoder) Close

func (e *Encoder) Close()

Close does nothing.

func (*Encoder) EncodeDocument

func (e *Encoder) EncodeDocument(d document.Document) error

EncodeDocument encodes d.

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