Documentation
¶
Overview ¶
Example (CustomEncoding) ¶
Example_customEncoding is an example for using custom serialization and deserialization on the CustomFruit type by implementing the Polorizable and Depolorizable interfaces for it.
// Create a CustomFruit object
orange := &CustomFruit{"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)
// Create a new instance of CustomFruit
object := new(CustomFruit)
// Deserialize the wire into the CustomFruit object (must be a pointer)
if err := Depolorize(object, wire); err != nil {
log.Fatalln(err)
}
// Print the deserialized object
fmt.Println(object)
Output: Custom Serialize for Fruit Invoked [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] Custom Deserialize for Fruit Invoked &{orange 300 [tangerine mandarin]}
Example (DepolorizeDocumentToDocument) ¶
Example_depolorizeDocumentToDocument is an example of using the Depolorize function to decode a document-encoded wire into a Document object
wire := []byte{
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,
}
// Create a new Document
doc := make(Document)
// Deserialize the document bytes into a Document
if err := Depolorize(&doc, wire); err != nil {
log.Fatalln(err)
}
// Print the decoded Document
fmt.Println(doc)
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]]
Example (DepolorizeDocumentToStruct) ¶
Example_depolorizeDocumentToStruct is an example of using the Depolorize function to decode a document encoded wire into a Fruit object
wire := []byte{
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,
}
// Create a new instance of Fruit
object := new(Fruit)
// Deserialize the document bytes into the Fruit object
if err := Depolorize(object, wire, DocStructs()); err != nil {
log.Fatalln(err)
}
// Print the deserialized object
fmt.Println(object)
Output: &{orange 300 [tangerine mandarin]}
Example (WireDecoding) ¶
Example_wireDecoding is an example for using the Any type to capture the raw POLO encoded bytes for a specific field of the Fruit object.
// RawFruit is a struct that can capture the raw POLO bytes of each field
type RawFruit struct {
Name Any
Cost int
Alias []string
}
// 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)
// Create a new instance of RawFruit
object := new(RawFruit)
// Deserialize the wire into the RawFruit object (must be a pointer)
if err := Depolorize(object, wire); err != nil {
log.Fatalln(err)
}
// Print the deserialized object
fmt.Println(object)
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] &{[6 111 114 97 110 103 101] 300 [tangerine mandarin]}
Index ¶
- Constants
- Variables
- func Depolorize(object any, data []byte, options ...EncodingOptions) error
- func IsWireType[T Raw | Any | []byte](wire T, kind WireType) bool
- func KeySort(keys []Key, ch chan Key)
- func Polorize(object any, options ...EncodingOptions) ([]byte, error)
- func ValueCmp(a, b reflect.Value) int
- func ValueLt(a, b reflect.Value) bool
- func ValueSort(keys []reflect.Value) func(int, int) bool
- 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) DepolorizeBytes32() ([32]byte, error)
- func (depolorizer *Depolorizer) DepolorizeDocument() (Document, error)
- func (depolorizer *Depolorizer) DepolorizeFloat32() (float32, error)
- func (depolorizer *Depolorizer) DepolorizeFloat64() (float64, error)
- func (depolorizer *Depolorizer) DepolorizeInt() (int, error)
- func (depolorizer *Depolorizer) DepolorizeInt8() (int8, error)
- func (depolorizer *Depolorizer) DepolorizeInt16() (int16, error)
- func (depolorizer *Depolorizer) DepolorizeInt32() (int32, error)
- func (depolorizer *Depolorizer) DepolorizeInt64() (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() (uint, error)
- func (depolorizer *Depolorizer) DepolorizeUint8() (uint8, error)
- func (depolorizer *Depolorizer) DepolorizeUint16() (uint16, error)
- func (depolorizer *Depolorizer) DepolorizeUint32() (uint32, error)
- func (depolorizer *Depolorizer) DepolorizeUint64() (uint64, error)
- func (depolorizer *Depolorizer) Done() bool
- func (depolorizer *Depolorizer) IsNull() bool
- func (depolorizer *Depolorizer) Unpacked() (*Depolorizer, error)
- type Document
- type EncodingOptions
- type IncompatibleValueError
- type IncompatibleWireError
- type Key
- 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 ¶
const DefaultMaxDepth = 10000
DefaultMaxDepth is the default maximum nesting depth of compound wires for decoding. It bounds the recursion of the decoder so that a deeply nested wire cannot overflow the stack.
Variables ¶
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") // ErrMaxDepthExceeded is an error for when a wire is nested deeper than the decoder allows (see MaxDepth) ErrMaxDepthExceeded = errors.New("maximum nesting depth exceeded") )
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 ¶
IsWireType returns whether the POLO data is of a certain wire type
func KeySort ¶ added in v0.4.4
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
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)
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 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 ¶
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) 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 ¶
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().
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 MaxDepth ¶ added in v0.4.10
func MaxDepth(depth int) EncodingOptions
MaxDepth is an EncodingOption that sets the maximum nesting depth of compound wires (packs and documents) that a decoder will descend into. Deeper wires are rejected with ErrMaxDepthExceeded. Defaults to DefaultMaxDepth. A depth less than 1 removes the limit.
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.
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 NewPolorizer ¶
func NewPolorizer(options ...EncodingOptions) *Polorizer
NewPolorizer creates a new Polorizer. Accepts EncodingOptions to modify the encoding behaviour of the Polorizer
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.
If PackedBytes() is used when creating the Polorizer, the bytes is encoded as a WirePack element instead of 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)
