-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolo.go
52 lines (42 loc) · 1.53 KB
/
polo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package polo
// Any is some raw POLO encoded data.
// The data of Any can have any WireType
type Any []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 Raw []byte
// Polorizable is an interface for an object that serialize into a Polorizer
type Polorizable interface {
Polorize() (*Polorizer, 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.
func Polorize(object any, options ...EncodingOptions) ([]byte, error) {
// Create a new polorizer
polorizer := NewPolorizer(options...)
// Polorize the object
if err := polorizer.Polorize(object); err != nil {
return nil, err
}
// Return the bytes of the writebuffer
return polorizer.wb.bytes(), nil
}
// Depolorizable is an interface for an object that deserialize its contents from a Depolorizer
type Depolorizable interface {
Depolorize(*Depolorizer) 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.
func Depolorize(object any, data []byte, options ...EncodingOptions) error {
// Create a new depolorizer from the data
depolorizer, err := NewDepolorizer(data, options...)
if err != nil {
return err
}
// Depolorize the object from the depolorizer
if err = depolorizer.Depolorize(object); err != nil {
return err
}
return nil
}