MIT License
Warning: This package is a work in progress. After some preliminary testing, I think I might change a lot of how this package is structured.
Cod is a schemaless serialization library for generating serialization code for your structs. This is for generating code that encodes and decodes data as quickly as possible. By default we use variable length encoding for all unsigned and signed integers larger than 16 bits.
You just need to run this to get the binary: go install github.com/unitoftime/cod/cmd/cod
You will also need to add ~/go/bin/ (or windows equivalent is) to your path so you can reference binaries from there
You can then add //go:generate cod
to one of your go files in your package. This will run the cod binary every time you execute go generate
. Finally you can tag structures that you want to generate code for as follows:
//cod:struct
//cod:union <UnionDefName>
//cod:def
cod_encode.go
and will reside in the package you generated fromcod:"fixed"
(or something)Add the //cod:struct
to indicate that its a struct, or create a serializable union with //cod:union <CSV list of union types>
:
//cod:struct
type Person struct {
Name string
Age uint8
FavoriteThings map[string]Thing
SomeListOfNumbers []int64
}
//cod:union ThingUnionDef
type Thing cod.Union
//cod:def
type ThingUnionDef struct {
Ball
Hat
}
//cod:struct
type Ball struct {
Color string
}
//cod:struct
type Hat struct {
Material string
}
All types will have two methods generated for them:
EncodeCod([]byte) []byte
DecodeCod([]byte) (int, error)
Unions will also get the following methods
Get() any // Guaranteed to return one of the unionable types or nil
Set(any) // Must pass in only a unionable type or nil
New<TYPE>(v any) <TYPE> // A Constructor: Where <TYPE> is the name of the union
// ---------------
// For cod:structs
// ---------------
func (t Person) EncodeCod(bs []byte) []byte {
// ... Generated Code ...
}
func (t *Person) DecodeCod(bs []byte) (int, error) {
// ... Generated Code ...
}
// ... Other Structs ...
// ---------------
// For cod:unions
// ---------------
func (t <TYPE>) EncodeCod(bs []byte) []byte {
// ... Generated Code ...
}
func (t *<TYPE>) DecodeCod(bs []byte) (int, error) {
// ... Generated Code ...
}
func (t <TYPE>) Get() any {
// ... Generated Code ...
}
func (t *<TYPE>) Set(v any) {
// ... Generated Code ...
}
func New<TYPE>(v any) <TYPE> {
// ... Generated Code ...
}