polo

package module
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0, MIT Imports: 11 Imported by: 2

README

image

go version license go docs latest tag

issue count pulls count test status

go-polo

go-polo is a Go implementation of the POLO Encoding and Object Serialization Scheme. POLO stands for Prefix Ordered Lookup Offsets.

It is intended for use in projects that prioritize deterministic serialization, minimal wire sizes and serialization safety. POLO follows a very strict specification that is optimized for partial decoding and differential messaging. This implementation is compliant with the POLO Specification that describes both the encoding (wire) format as well as implementation guidelines for several languages.

Installation

Install the latest release using the following command

go get -u github.com/sarvalabs/go-polo

Features

Deterministic Serialization

POLO's strict specification is intended to create the same serialized wire for an object regardless of implementation. This is critical for cryptographic security with operations such as hashing which is used to guarantee data consistency and tamper proofing.

High Wire Efficiency

POLO has a highly optimized wire format allows messages to be relatively small, even surpassing Protocol Buffers occassionaly. This is mainly because it supports a larger type based wire tagging that allows some information (especially metadata) to be passed around inferentially and thus reducing the total amount of information actually present in the wire.

This is augmented by the fact that POLO supports Atomic Encoding, because of which simple objects such as integers & strings can be encoded without needing to be wrapped within a larger (structural) message

Partial Encoding/Decoding Constructs

The POLO wire format prefixes all metadata for the wire in the front, this metadata includes the wire type information as well as the offset position of the data. This kind of lookup based offset tags allows us to directly access the data for a particular field order. This capability is currently supported using the Document and Raw constructs.

The Document construct is used to create a string indexed collection of Raw object and it collapses into a special form of the POLO wire called the document-encoded wire with the WireDoc wire type. It's unique in that it preserves the field name (string index) for the data unlike regular POLO encoding and is similar to encoding schemes like JSON or YAML and consequently consumes more wire space.

The Raw construct is useful for capturing the wire data for a specific field in the struct. It can also be used to define a structure that 'skips' the unrequired fields by capturing their raw wire instead of decoding them.

Partial encodign/decoding capabilities for this implementation are unfinished can be extended to support field order based access/write for regular POLO encoded wires in the future.

Custom Encoding/Decoding Buffers

POLO describes two buffers, Polorizer and Depolorizer which are write-only and read-only respectively, allowing sequential encoding/decoding of objects and wire elements into them. This capability can be leveraged to implement the Polorizable and Depolorizable interfaces which describe the custom serialization form for an object.

Note: This capability can be dangerous if not implemented correctly, it generally recommended that both interfaces be implemented and are evenly capable of encoding/decoding the same contents to avoid inconsistency. It is intended to be used for object such as Go Interfaces which are not supported by default when using the reflection based Polorize and Depolorize functions.

Differential Messaging (Coming Soon)

POLO's partially encoding and field order based indexing (and string based indexing for document encoded wires) allows the possibility for messaging that only allows the transmission of the difference between two states, this is useful for any version managment system where the same data is incrementally updated and transmitted, the ability to index the difference and only transmit the difference can result in massive reduction in the wire sizes for these use cases that often re-transmit already available information.

Examples

Simple Polorization & Depolorization (Encoding/Decoding)

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/polo_test.go#L16-L64

Custom Polorization & Depolorization (Encoding/Decoding)

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/polo_test.go#L66-L173

Wire Decoding with Any

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/polo_test.go#L175-L212

Polorizer (Encoding Buffer)

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/polorizer_test.go#L12-L42

Depolorizer (Decoding Buffer)

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/depolorizer_test.go#L13-L70

Document Encoding

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/document_test.go#L12-L74

Document Decoding

https://github.com/sarvalabs/go-polo/blob/22a975e4d1d5329e16aaedd7207aee382e64d30e/document_test.go#L76-L124

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as below, without any additional terms or conditions.

License

© 2024 Sarva Labs Inc. & MOI Protocol Developers.

This project is licensed under either of

at your option.

The SPDX license identifier for this project is MIT OR Apache-2.0.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrObjectNotPtr is an error for when a non pointer object is passed to the Depolorize function
	ErrObjectNotPtr = errors.New("object not a pointer")
	// ErrObjectNotSettable is an error for when a non-settable pointer is passed to the Depolorize function
	ErrObjectNotSettable = errors.New("object is not settable")
	// ErrInsufficientWire is an error for when the data in depolorizer is exhausted
	ErrInsufficientWire = errors.New("insufficient data in wire for decode")
)

Functions

func Depolorize

func Depolorize(object any, data []byte, options ...EncodingOptions) error

Depolorize deserializes a POLO encoded byte slice into an object. Throws an error if the wire cannot be parsed or if the object is not a pointer.

Example

ExampleDepolorize is an example for using the Depolorize function to decode a Fruit object from its POLO wire form using Go reflection

wire := []byte{
	14, 79, 6, 99, 142, 1, 111, 114, 97, 110, 103, 101, 1, 44, 63, 6, 150, 1, 116,
	97, 110, 103, 101, 114, 105, 110, 101, 109, 97, 110, 100, 97, 114, 105, 110,
}

// Create a new instance of Fruit
object := new(Fruit)
// Deserialize the wire into the Fruit object (must be a pointer)
if err := Depolorize(object, wire); err != nil {
	log.Fatalln(err)
}

// Print the deserialized object
fmt.Println(object)
Output:
&{orange 300 [tangerine mandarin]}

func IsWireType

func IsWireType[T Raw | Any | []byte](wire T, kind WireType) bool

IsWireType returns whether the POLO data is of a certain wire type

func KeySort added in v0.4.4

func KeySort(keys []Key, ch chan Key)

KeySort accepts a slice of Key values and a channel to return them on. They are returned in sorted order and are merge sorted recursively.

func Polorize

func Polorize(object any, options ...EncodingOptions) ([]byte, error)

Polorize serializes an object into its POLO byte form. Accepts EncodingOptions to modify the encoding behaviour. Returns an error if object is an unsupported type such as functions or channels.

Example

ExamplePolorize is an example for using the Polorize function to encode a Fruit object into its POLO wire form using Go reflection

// Create a Fruit object
orange := &Fruit{"orange", 300, []string{"tangerine", "mandarin"}}

// Serialize the Fruit object
wire, err := Polorize(orange)
if err != nil {
	log.Fatalln(err)
}

// Print the serialized bytes
fmt.Println(wire)
Output:
[14 79 6 99 142 1 111 114 97 110 103 101 1 44 63 6 150 1 116 97 110 103 101 114 105 110 101 109 97 110 100 97 114 105 110]

func ValueCmp added in v0.4.2

func ValueCmp(a, b reflect.Value) int

ValueCmp returns an integer representing the comparison between two reflect.Value objects. Assumes that a and b can only have a type that is comparable. (will panic otherwise). Returns 1 (a > b); 0 (a == b); -1 (a < b)

func ValueLt added in v0.4.2

func ValueLt(a, b reflect.Value) bool

ValueLt is returns a < b, for two reflected values a & b

func ValueSort added in v0.4.2

func ValueSort(keys []reflect.Value) func(int, int) bool

ValueSort is used by the sort package to sort a slice of reflect.Value objects. Assumes that the reflect.Value objects can only be types which are comparable i.e, can be used as a map key. (will panic otherwise)

Types

type Any

type Any []byte

Any is some raw POLO encoded data. The data of Any can have any WireType

type Depolorizable

type Depolorizable interface {
	Depolorize(*Depolorizer) error
}

Depolorizable is an interface for an object that deserialize its contents from a Depolorizer

type Depolorizer

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

Depolorizer is a decoding buffer that can sequentially depolorize object from it. It can check whether there are elements left in the buffer with Done() and peek the WireType of the next element with Peek().

Example

ExampleDepolorizer is an example for using the Depolorizer to decode the fields of a Fruit object using a Depolorizer which allows sequential decoding of data from a read-only buffer

wire := []byte{
	14, 79, 6, 99, 142, 1, 111, 114, 97, 110, 103, 101, 1, 44, 63, 6, 150, 1, 116,
	97, 110, 103, 101, 114, 105, 110, 101, 109, 97, 110, 100, 97, 114, 105, 110,
}

// Create a new instance of Fruit
object := new(Fruit)

// Create a new Depolorizer from the data
depolorizer, err := NewDepolorizer(wire)
if err != nil {
	log.Fatalln("invalid wire:", err)
}

depolorizer, err = depolorizer.DepolorizePacked()
if err != nil {
	log.Fatalln("invalid wire:", err)
}

// Decode the Name field as a string
object.Name, err = depolorizer.DepolorizeString()
if err != nil {
	log.Fatalln("invalid field 'Name':", err)
}

// Decode the Cost field as a string
Cost, err := depolorizer.DepolorizeInt64()
if err != nil {
	log.Fatalln("invalid field 'Cost':", err)
}

object.Cost = int(Cost)

// Decode a new Depolorizer to deserialize the Alias field (slice)
aliases, err := depolorizer.DepolorizePacked()
if err != nil {
	log.Fatalln("invalid field 'Alias':", err)
}

// Decode each element from the Alias decoder as a string
for !aliases.Done() {
	alias, err := aliases.DepolorizeString()
	if err != nil {
		log.Fatalln("invalid field element 'Alias':", err)
	}

	object.Alias = append(object.Alias, alias)
}

// Print the deserialized object
fmt.Println(object)
Output:
&{orange 300 [tangerine mandarin]}

func NewDepolorizer

func NewDepolorizer(data []byte, options ...EncodingOptions) (*Depolorizer, error)

NewDepolorizer returns a new Depolorizer for some given bytes. Returns an error if the given bytes is malformed for a POLO wire.

If the given data is a compound wire, the only element in the Depolorizer will be the compound data, and it will need to be unwrapped into another Depolorizer with DepolorizePacked() before decoding its elements

func (*Depolorizer) Depolorize

func (depolorizer *Depolorizer) Depolorize(object any) error

Depolorize decodes a value from the Depolorizer. Decodes the data in the wire into the given object using Go reflection. Returns an error if the object is not a pointer or if a decode error occurs.

func (*Depolorizer) DepolorizeAny

func (depolorizer *Depolorizer) DepolorizeAny() (Any, error)

DepolorizeAny attempts to decode an Any from the Depolorizer, consuming one wire element. If a WireNull is encountered, it is returned as Any{0}. Returns an error if there are no elements left. Will succeed regardless of the WireType.

func (*Depolorizer) DepolorizeBigInt

func (depolorizer *Depolorizer) DepolorizeBigInt() (*big.Int, error)

DepolorizeBigInt attempts to decode a big.Int from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt or WireNegInt. Returns a nil big.Int if the element is a WireNull.

func (*Depolorizer) DepolorizeBool

func (depolorizer *Depolorizer) DepolorizeBool() (bool, error)

DepolorizeBool attempts to decode a bool value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireTrue or WireFalse. Returns a false if the element is a WireNull.

func (*Depolorizer) DepolorizeBytes

func (depolorizer *Depolorizer) DepolorizeBytes() ([]byte, error)

DepolorizeBytes attempts to decode a bytes value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireWord. Returns a nil byte slice if the element is a WireNull.

If PackedBytes option is used in the decoder configuration, WirePack containing u8 elements will be successfully interpreted as a byte slice.

func (*Depolorizer) DepolorizeBytes32 added in v0.4.7

func (depolorizer *Depolorizer) DepolorizeBytes32() ([32]byte, error)

DepolorizeBytes32 attempts to decode a 32-byte value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireWord. Returns a zero byte array if the element is a WireNull.

If PackedBytes option is used in the decoder configuration, WirePack containing u8 elements will be successfully interpreted as a byte array.

func (*Depolorizer) DepolorizeDocument

func (depolorizer *Depolorizer) DepolorizeDocument() (Document, error)

DepolorizeDocument attempts to decode a Document from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireDoc. Returns nil Document if the element is a WireNull.

func (*Depolorizer) DepolorizeFloat32

func (depolorizer *Depolorizer) DepolorizeFloat32() (float32, error)

DepolorizeFloat32 attempts to decode a single point precision float from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireFloat. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeFloat64

func (depolorizer *Depolorizer) DepolorizeFloat64() (float64, error)

DepolorizeFloat64 attempts to decode a double point precision float from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireFloat. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeInt

func (depolorizer *Depolorizer) DepolorizeInt() (int, error)

DepolorizeInt attempts to decode an int value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt or WireNegInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeInt8 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeInt8() (int8, error)

DepolorizeInt8 attempts to decode an 8-bit signed integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt or WireNegInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeInt16 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeInt16() (int16, error)

DepolorizeInt16 attempts to decode a 16-bit signed integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt or WireNegInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeInt32 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeInt32() (int32, error)

DepolorizeInt32 attempts to decode a 32-bit signed integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt or WireNegInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeInt64 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeInt64() (int64, error)

DepolorizeInt64 attempts to decode a 64-bit signed integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt or WireNegInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeNull

func (depolorizer *Depolorizer) DepolorizeNull() error

DepolorizeNull attempts to decode a null value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireNull.

func (*Depolorizer) DepolorizePacked

func (depolorizer *Depolorizer) DepolorizePacked() (*Depolorizer, error)

DepolorizePacked attempts to decode another Depolorizer from the Depolorizer, consuming one wire element. A new Depolorizer is returned with the unpacked wire if the element is WirePack or WireDoc. Returns an error if there are no elements left or if the element is not WirePack or WireDoc.

func (*Depolorizer) DepolorizeRaw

func (depolorizer *Depolorizer) DepolorizeRaw() (Raw, error)

DepolorizeRaw attempts to decode a Raw from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireRaw.

func (*Depolorizer) DepolorizeString

func (depolorizer *Depolorizer) DepolorizeString() (string, error)

DepolorizeString attempts to decode a string value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WireWord. Returns an empty string if the element is a WireNull.

func (*Depolorizer) DepolorizeUint

func (depolorizer *Depolorizer) DepolorizeUint() (uint, error)

DepolorizeUint attempts to decode an unsigned integer value from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeUint8 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeUint8() (uint8, error)

DepolorizeUint8 attempts to decode an 8-bit unsigned integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeUint16 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeUint16() (uint16, error)

DepolorizeUint16 attempts to decode a 16-bit unsigned integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeUint32 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeUint32() (uint32, error)

DepolorizeUint32 attempts to decode a 32-bit unsigned integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) DepolorizeUint64 added in v0.4.6

func (depolorizer *Depolorizer) DepolorizeUint64() (uint64, error)

DepolorizeUint64 attempts to decode a 64-bit unsigned integer from the Depolorizer, consuming one wire element. Returns an error if there are no elements left or if the element is not WirePosInt. Returns 0 if the element is a WireNull.

func (*Depolorizer) Done

func (depolorizer *Depolorizer) Done() bool

Done returns whether all elements in the Depolorizer have been read.

func (*Depolorizer) IsNull added in v0.4.6

func (depolorizer *Depolorizer) IsNull() bool

IsNull returns whether the next element in the Depolorizer is a WireNull without consuming it from the buffer. False if there are no elements left in the Depolorizer.

Use DepolorizeNull to consume the WireNull element if further elements are to be read.

func (*Depolorizer) Unpacked added in v0.4.6

func (depolorizer *Depolorizer) Unpacked() (*Depolorizer, error)

Unpacked attempts to unpack a Depolorizer that contains a WirePack or WireDoc element. A new Depolorizer is returned with the unpacked wire and the original Depolorizer consumes one wire element. Returns an error if there are no elements left or if the element is not a WirePack or WireDoc.

note: this method is a wrapper around DepolorizePacked for brevity and aesthetics.

type Document

type Document map[string]Raw

Document is a representation for a string indexed collection of encoded object data. It represents an intermediary access format with objects settable/gettable with string keys.

Example

ExampleDocument is an example for using the Document object's method to partially encode fields as properties into it and then serialize it into document encoded POLO bytes

// Create a new Document
document := make(Document)

// Encode the 'Name' field
if err := document.Set("Name", "orange"); err != nil {
	log.Fatalln(err)
}

// Encode the 'cost' field
if err := document.Set("cost", 300); err != nil {
	log.Fatalln(err)
}

// Encode the 'alias' field
if err := document.Set("alias", []string{"tangerine", "mandarin"}); err != nil {
	log.Fatalln(err)
}

// Print the Document object and it serialized bytes
fmt.Println(document)
fmt.Println(document.Bytes())
Output:
map[Name:[6 111 114 97 110 103 101] alias:[14 63 6 150 1 116 97 110 103 101 114 105 110 101 109 97 110 100 97 114 105 110] cost:[3 1 44]]
[13 175 1 6 69 182 1 133 2 230 4 165 5 78 97 109 101 6 111 114 97 110 103 101 97 108 105 97 115 14 63 6 150 1 116 97 110 103 101 114 105 110 101 109 97 110 100 97 114 105 110 99 111 115 116 3 1 44]

func PolorizeDocument added in v0.4.0

func PolorizeDocument(object any, options ...EncodingOptions) (Document, error)

PolorizeDocument encodes an object into its POLO bytes such that it can be decoded as a Document. The object must either be a map with string keys, a struct or a pointer to either.

  • Map objects will use the same key in the object for Document keys
  • Struct objects will use the field name as declared unless overridden with a polo struct tag specifying the field name to use. Private struct fields and fields marked to be skipped will be ignored while converting into a Document.

Returns an error if the object is not a supported type, is a nil pointer or if any of the element/field types cannot be serialized with Polorize()

Example

ExamplePolorizeDocument is an example for using PolorizeDocument to encode a struct into a Document and then further serializing it into document encoded POLO bytes

// Create a Fruit object
orange := &Fruit{"orange", 300, []string{"tangerine", "mandarin"}}

// Encode the object into a Document
document, err := PolorizeDocument(orange)
if err != nil {
	log.Fatalln(err)
}

// Print the Document object
fmt.Println(document)

// Serialize the Document object
// This can also be done with document.Bytes()
wire, err := Polorize(document)
if err != nil {
	log.Fatalln(err)
}

// Print the serialized Document
fmt.Println(wire)
Output:
map[Name:[6 111 114 97 110 103 101] alias:[14 63 6 150 1 116 97 110 103 101 114 105 110 101 109 97 110 100 97 114 105 110] cost:[3 1 44]]
[13 175 1 6 69 182 1 133 2 230 4 165 5 78 97 109 101 6 111 114 97 110 103 101 97 108 105 97 115 14 63 6 150 1 116 97 110 103 101 114 105 110 101 109 97 110 100 97 114 105 110 99 111 115 116 3 1 44]

func (Document) Bytes

func (doc Document) Bytes() []byte

Bytes returns the POLO wire representation of a Document

func (Document) Get

func (doc Document) Get(key string, object any) error

Get retrieves some object for some given key from a Document. The data for the given key is decoded from its POLO form into the given object which must be a pointer. Returns an error if there is no data for the key or if the data could not be decoded into the given object.

func (Document) GetRaw

func (doc Document) GetRaw(key string) Raw

GetRaw retrieves some raw byte data for a given key from a Document. Return nil if there is no data for the key.

func (Document) Set

func (doc Document) Set(key string, object any, options ...EncodingOptions) error

Set inserts some object for some given key into a Document. The given object is encoded into its POLO form and inserted, overwriting any existing data. Returns an error if the given object cannot be serialized with Polorize().

func (Document) SetRaw

func (doc Document) SetRaw(key string, val Raw)

SetRaw inserts some raw byte data for a given key into a Document. Any existing data at the given key is overwritten.

func (Document) Size

func (doc Document) Size() int

Size returns the number of elements in a Document

type EncodingOptions added in v0.4.0

type EncodingOptions func(*wireConfig)

EncodingOptions represents options that can be provided to encoding/decoding functions or buffers to modify the wire form

func DocStringMaps added in v0.4.0

func DocStringMaps() EncodingOptions

DocStringMaps is an EncodingOption that sets the encoding/decoding to interpret string keyed maps as document-encoded values instead of a pack

func DocStructs added in v0.4.0

func DocStructs() EncodingOptions

DocStructs is an EncodingOption that sets the encoding/decoding to interpret structs as document-encoded values instead of a pack

func PackedBytes added in v0.4.0

func PackedBytes() EncodingOptions

PackedBytes is an EncodingOption that sets the encoding/decoding to allow interpretation of bytes from pack-encoded uint8 values

type IncompatibleValueError

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

IncompatibleValueError is an error for when an incompatible value is used for encoding

func UnsupportedTypeError

func UnsupportedTypeError(t reflect.Type) IncompatibleValueError

func (IncompatibleValueError) Error

func (err IncompatibleValueError) Error() string

Error implements the error interface for IncompatibleValueError

type IncompatibleWireError

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

IncompatibleWireError is an error for when an object cannot be decoded from some wire data

func IncompatibleWireType

func IncompatibleWireType(actual WireType, expected ...WireType) IncompatibleWireError

IncompatibleWireType returns an IncompatibleWireError formatted to express the mismatch between an unexpected wire type and the list of expected ones.

func (IncompatibleWireError) Error

func (err IncompatibleWireError) Error() string

Error implements the error interface for IncompatibleWireError

type Key added in v0.4.4

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

Key is an indexed reflect value. Preserves the original index of key. Can be used in conjunction with the KeySort function to acquire sorted order of reflected value.

func NewKey added in v0.4.4

func NewKey(idx int, val any) Key

NewKey creates a new value for the given index and value. The given value is reflected to use in Key.

func (Key) Index added in v0.4.5

func (key Key) Index() int

Index returns the index of the key

type MalformedTagError

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

MalformedTagError is an error for when a consumed varint for a tag is malformed

func (MalformedTagError) Error

func (err MalformedTagError) Error() string

Error implements the error interface for MalformedTagError

type Polorizable

type Polorizable interface {
	Polorize() (*Polorizer, error)
}

Polorizable is an interface for an object that serialize into a Polorizer

type Polorizer

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

Polorizer is an encoding buffer that can sequentially polorize objects into it. It can be collapsed into its bytes with Bytes() or Packed().

Example

ExamplePolorizer is an example for using the Polorizer to encode the fields of a Fruit object using a Polorizer which allows sequential encoding of data into a write-only buffer

// Create a Fruit object
orange := &Fruit{"orange", 300, []string{"tangerine", "mandarin"}}

// Create a new Polorizer
polorizer := NewPolorizer()

// Encode the Name field as a string
polorizer.PolorizeString(orange.Name)
// Encode the Cost field as an integer
polorizer.PolorizeInt(int64(orange.Cost))

// Create a new Polorizer to serialize the Alias field (slice)
aliases := NewPolorizer()
// Encode each element in the Alias slice as a string
for _, alias := range orange.Alias {
	aliases.PolorizeString(alias)
}
// Encode the Polorizer containing the alias field contents as packed data
polorizer.PolorizePacked(aliases)

// Print the serialized bytes in the Polorizer buffer
fmt.Println(polorizer.Bytes())
Output:
[14 79 6 99 142 1 111 114 97 110 103 101 1 44 63 6 150 1 116 97 110 103 101 114 105 110 101 109 97 110 100 97 114 105 110]

func NewPolorizer

func NewPolorizer(options ...EncodingOptions) *Polorizer

NewPolorizer creates a new Polorizer. Accepts EncodingOptions to modify the encoding behaviour of the Polorizer

func (Polorizer) Bytes

func (polorizer Polorizer) Bytes() []byte

Bytes returns the contents of the Polorizer as bytes.

  • If no objects were polorized, it returns a WireNull wire
  • If only one object was polorized, it returns the contents directly
  • If more than one object was polorized, it returns the contents in a packed wire.

func (Polorizer) Packed

func (polorizer Polorizer) Packed() []byte

Packed returns the contents of the Polorizer as bytes after packing it and tagging with WirePack.

func (*Polorizer) Polorize

func (polorizer *Polorizer) Polorize(value any) error

Polorize encodes a value into the Polorizer. Encodes the object based on its type using the Go reflection.

func (*Polorizer) PolorizeAny

func (polorizer *Polorizer) PolorizeAny(value Any) error

PolorizeAny encodes an Any into the Polorizer. Encodes the Any as its native type. A nil Any is encodes as WireNull. Returns an error if the Any wire has an invalid wire tag. USE WITH CAUTION: Encoding unsupported wire formats, will lead to serialization failures

func (*Polorizer) PolorizeBigInt

func (polorizer *Polorizer) PolorizeBigInt(value *big.Int)

PolorizeBigInt encodes a big.Int into the Polorizer. Encodes the big.Int as its binary form with the wire type being WirePosInt or WireNegInt based on polarity, with zero considered as positive. A nil big.Int is encoded as WireNull.

func (*Polorizer) PolorizeBool

func (polorizer *Polorizer) PolorizeBool(value bool)

PolorizeBool encodes a bool value into the Polorizer. Encodes the boolean as either WireTrue or WireFalse, depending on its value.

func (*Polorizer) PolorizeBytes

func (polorizer *Polorizer) PolorizeBytes(value []byte)

PolorizeBytes encodes a bytes value into the Polorizer. Encodes the bytes as is with the wire type being WireWord.

If PackedBytes() is used when creating the Polorizer, the bytes is encoded as a WirePack element instead of WireWord.

func (*Polorizer) PolorizeDocument

func (polorizer *Polorizer) PolorizeDocument(document Document)

PolorizeDocument encodes a Document into the Polorizer. Encodes the Document keys and raw values as POLO doc-encoded data with the wire type being WireDoc. If the Document is nil, it is encoded as a WireNull.

func (*Polorizer) PolorizeFloat32

func (polorizer *Polorizer) PolorizeFloat32(value float32)

PolorizeFloat32 encodes a single point precision float into the Polorizer. Encodes the float as its IEEE754 binary form (big-endian) with the wire type being WireFloat.

func (*Polorizer) PolorizeFloat64

func (polorizer *Polorizer) PolorizeFloat64(value float64)

PolorizeFloat64 encodes a double point precision float into the Polorizer. Encodes the float as its IEEE754 binary form (big-endian) with the wire type being WireFloat.

func (*Polorizer) PolorizeInt

func (polorizer *Polorizer) PolorizeInt(value int64)

PolorizeInt encodes a signed integer value into the Polorizer. Encodes the integer as the binary form of its absolute value with the wire type being WirePosInt or WireBigInt based on polarity, with zero considered as positive.

func (*Polorizer) PolorizeNull

func (polorizer *Polorizer) PolorizeNull()

PolorizeNull encodes a null value into the Polorizer. Encodes a WireNull into the head, consuming a position on the wire.

func (*Polorizer) PolorizePacked

func (polorizer *Polorizer) PolorizePacked(pack *Polorizer)

PolorizePacked encodes the contents of another Polorizer as pack-encoded data. The contents are packed into a WireLoad message and tagged with the WirePack wire type. If the given Polorizer is nil, a WireNull is encoded instead.

func (*Polorizer) PolorizeRaw

func (polorizer *Polorizer) PolorizeRaw(value Raw)

PolorizeRaw encodes a Raw into the Polorizer. Encodes the Raw with the wire type being WireRaw. No check is performed on the Raw wire, and is assumed to be a valid POLO Wire. A nil Raw = Raw{0}. USE WITH CAUTION: Encoding unsupported wire formats, will lead to serialization failures

func (*Polorizer) PolorizeString

func (polorizer *Polorizer) PolorizeString(value string)

PolorizeString encodes a string value into the Polorizer. Encodes the string as its UTF-8 encoded bytes with the wire type being WireWord.

func (*Polorizer) PolorizeUint

func (polorizer *Polorizer) PolorizeUint(value uint64)

PolorizeUint encodes a signed integer value into the Polorizer. Encodes the integer as it's the binary form (big-endian) with the wire type being WirePosInt.

type Raw

type Raw []byte

Raw is a container for raw POLO encoded data. The data of Raw must have type WireRaw with its body being valid POLO data

type WireType

type WireType byte

WireType is an enum for different wire types. A wire type indicates the kind of encoding for some data.

const (
	// WireNull represents a null wire. Used for consuming field orders without data.
	WireNull WireType = 0

	// WireFalse represents a Boolean False
	WireFalse WireType = 1
	// WireTrue represents a Boolean True
	WireTrue WireType = 2

	// WirePosInt represents a Binary encoded +ve Integer in BigEndian Order.
	WirePosInt WireType = 3
	// WireNegInt represents a Binary encoded -ve Integer in BigEndian Order.
	// The number is encoded as its absolute value and must be multiplied with -1 to get its actual value.
	WireNegInt WireType = 4

	// WireRaw represents some POLO encoded bytes
	WireRaw WireType = 5
	// WireWord represents UTF-8 encoded string/bytes
	WireWord WireType = 6
	// WireFloat represents some floating point data encoded in the IEEE754 standard. (floats)
	WireFloat WireType = 7

	// WireDoc represents some doc encoded data (string keyed maps, tagged structs and Document objects)
	WireDoc WireType = 13
	// WirePack represents some pack encoded data (slices, arrays, maps, structs)
	WirePack WireType = 14
	// WireLoad represents a load tag for compound wire type
	WireLoad WireType = 15
)

func (WireType) IsCompound

func (wiretype WireType) IsCompound() bool

IsCompound returns whether a given wiretype is a compound type. (contains a load inside it)

func (WireType) IsNull

func (wiretype WireType) IsNull() bool

IsNull returns whether a given wiretype is null. A wiretype is null if it is WireNull, has a value greater than 15 or is between 8 and 12 (reserved)

func (WireType) IsValid

func (wiretype WireType) IsValid() bool

IsValid returns whether a wiretype is valid. A wiretype is valid if its value is less than 15 and not one of the reserved types

func (WireType) String

func (wiretype WireType) String() string

String returns a string representation of the wiretype.

If the wiretype is not recognized, "unknown" is returned. Implements the Stringer interface for wiretype.

Jump to

Keyboard shortcuts

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