Documentation
¶
Index ¶
- Variables
- func Depolorize(object any, data []byte) error
- func IsWireType[T Raw | Any | []byte](wire T, kind WireType) bool
- func Polorize(object any) ([]byte, error)
- type Any
- type Depolorizable
- type Depolorizer
- func (depolorizer *Depolorizer) Depolorize(object any) error
- func (depolorizer *Depolorizer) DepolorizeAny() (Any, error)
- func (depolorizer *Depolorizer) DepolorizeBigInt() (*big.Int, error)
- func (depolorizer *Depolorizer) DepolorizeBool() (bool, error)
- func (depolorizer *Depolorizer) DepolorizeBytes() ([]byte, error)
- func (depolorizer *Depolorizer) DepolorizeDocument() (Document, error)
- func (depolorizer *Depolorizer) DepolorizeFloat32() (float32, error)
- func (depolorizer *Depolorizer) DepolorizeFloat64() (float64, error)
- func (depolorizer *Depolorizer) DepolorizeInt() (int64, error)
- func (depolorizer *Depolorizer) DepolorizeNull() error
- func (depolorizer *Depolorizer) DepolorizePacked() (*Depolorizer, error)
- func (depolorizer *Depolorizer) DepolorizeRaw() (Raw, error)
- func (depolorizer *Depolorizer) DepolorizeString() (string, error)
- func (depolorizer *Depolorizer) DepolorizeUint() (uint64, error)
- func (depolorizer *Depolorizer) Done() bool
- type Document
- type IncompatibleValueError
- type IncompatibleWireError
- type MalformedTagError
- type Polorizable
- type Polorizer
- func (polorizer Polorizer) Bytes() []byte
- func (polorizer Polorizer) Packed() []byte
- func (polorizer *Polorizer) Polorize(value any) error
- func (polorizer *Polorizer) PolorizeAny(value Any) error
- func (polorizer *Polorizer) PolorizeBigInt(value *big.Int)
- func (polorizer *Polorizer) PolorizeBool(value bool)
- func (polorizer *Polorizer) PolorizeBytes(value []byte)
- func (polorizer *Polorizer) PolorizeDocument(document Document)
- func (polorizer *Polorizer) PolorizeFloat32(value float32)
- func (polorizer *Polorizer) PolorizeFloat64(value float64)
- func (polorizer *Polorizer) PolorizeInt(value int64)
- func (polorizer *Polorizer) PolorizeNull()
- func (polorizer *Polorizer) PolorizePacked(pack *Polorizer)
- func (polorizer *Polorizer) PolorizeRaw(value Raw)
- func (polorizer *Polorizer) PolorizeString(value string)
- func (polorizer *Polorizer) PolorizeUint(value uint64)
- type Raw
- type WireType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNullPack is an error for when a WireNull is attempted to be converted to a Depolorizer ErrNullPack = errors.New("null pack element") // 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 ¶
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 ¶
IsWireType returns whether the POLO data is of a certain wire type
func Polorize ¶
Polorize serializes an object into its POLO byte form. 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]
Types ¶
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 Pack Depolorizer from the data
depolorizer, err := NewPackDepolorizer(wire)
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.DepolorizeInt()
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) (*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 NewPackDepolorizer ¶
func NewPackDepolorizer(data []byte) (*Depolorizer, error)
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.
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() (int64, error)
DepolorizeInt attempts to decode a signed 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 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. Returns an error if there are no elements left or if the element is not WirePack or WireDoc. Returns a ErrNullPack if the element is a WireNull.
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() (uint64, 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) Done ¶
func (depolorizer *Depolorizer) Done() bool
Done returns whether all elements in the Depolorizer have been read.
type Document ¶
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 DocumentEncode ¶
DocumentEncode 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 ¶
ExampleDocumentEncode is an example for using DocumentEncode 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 := DocumentEncode(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) Get ¶
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 ¶
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 ¶
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().
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 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 ¶
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 (Polorizer) Bytes ¶
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 ¶
Packed returns the contents of the Polorizer as bytes after packing it and tagging with WirePack.
func (*Polorizer) Polorize ¶
Polorize encodes a value into the Polorizer. Encodes the object based on its type using the Go reflection.
func (*Polorizer) PolorizeAny ¶
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 ¶
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 ¶
PolorizeBool encodes a bool value into the Polorizer. Encodes the boolean as either WireTrue or WireFalse, depending on its value.
func (*Polorizer) PolorizeBytes ¶
PolorizeBytes encodes a bytes value into the Polorizer. Encodes the bytes as is with the wire type being WireWord.
func (*Polorizer) PolorizeDocument ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsCompound returns whether a given wiretype is a compound type. (contains a load inside it)
func (WireType) IsNull ¶
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)
