4 releases
0.3.0-alpha.2 | Aug 22, 2020 |
---|---|
0.3.0-alpha.1 | Aug 21, 2020 |
0.3.0-alpha.0 | Aug 13, 2020 |
0.1.0 | Apr 4, 2020 |
#5 in #trans-gen
28KB
731 lines
trans-gen
trans-gen
is a code generator for trans
— simple binary serialization protocol, for translating data between different programming languages.
Trans
Trans is a simple binary serialization protocol.
Data types
Trans supports following data types:
Bool
— a boolean value (true
/false
). Serialized as one byte:0
forfalse
and1
for true, other byte values are invalid representation.Int32
— 32-bit integer number. Serialized in little endian byte order.Int64
— 64-bit integer number. Serialized in little endian byte order.Float32
— 32-bit floating point number. Serialized in little endian byte order.Float64
— 64-bit floating point number. Serialized in little endian byte order.String
— UTF-8 encoded string. Serialized as length in bytes (as anInt32
), followed by the bytesOption<T>
— an optional value of typeT
. If absent, serialized asfalse
. Otherwise, serialized astrue
, followed by the value.Vec<T>
— a vector (array) with elements of typeT
. Serialized as number of elements (as anInt32
), followed by the elements.Map<K, V>
— a map (dictionary) with keys of typeK
and values of typeV
. Serialized as number of key-value pairs (as anInt32
), followed by key-value pairs (key first, value second).Struct
— a structure containing multiple fields. Each field may be of its own type. Serialized as all the fields one after another without any extra data, so field order is important.Enum
— an enumeration, a type whose value is restricted to a set of variants. Serialized as index of the variant (as anInt32
), starting with0
.OneOf
— an algebraic (sum) data type, a type that can be one of the given set of other types. Each variant type is actually, like aStruct
, just a set of fields. Serialized as index of the type variant (as anInt32
), starting with0
, followed by the fields.
Client vs Server
Trans makes a distinction between a "client" and "server". Server is the code that contains model definitions and needs to handle incorrect data when receiving data. Client is the code that needs to read data and is relying on it to be valid, thus can possibly implement some optimizations (like preallocating collection types).
Versioning
One feature that is present only on "server" side is versioning. Basically, server should support clients of all possible versions, while client knows only of its specific version.
Versioning only makes sense for custom data types: Struct
, Enum
and OneOf
.
For Struct
s, different versions may contain different set of fields. All the fields are always present on the server, but client only knows about those present in its version. In case we are receiving data from a version that does not contain a specific field, a default value must be provided for constructing a struct on the server.
For Enum
s, later versions may add extra variants. Removing variants is not yet supported.
OneOf
s act like a combination of Enum
and Struct
, thus they can add extra variants and/or change the set of fields in each variant.
Code generator
Code generator takes schemas provided by the server and produces client code for a specified version.
Supported languages (follow link for language implementation details):
- C++
- C#
- D
- F#
- Go
- Haskell
- Java
- JavaScript
- Kotlin
- Pascal
- PHP
- Python
- Ruby
- Rust
- Scala
- Swift
- TypeScript
- Markdown (generates documentation)
Example of generated code & benchmarks
The testing
example contains tests. You can see generated code for the it in the testing
branch of this repository.
You can also see benchmarks for all the languages, platforms, tests and models that are run on GitHub Actions there. Alternatively, see local benchmarks (may be outdated).
Models:
example
— simple example modelcodecraft
— models from AI Cup 2020 "CodeCraft"
Tests:
- FileReadWrite — reads data from a file and then writes it to another.
- TcpReadWrite — reads a stream of data from TCP connection to the server and writes it back.
Dependencies
~2.5MB
~48K SLoC