Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce protobuf encoder with main types
Introducing protobuf encoder that can convert four main protobuf types. Varint, Len, I32, I64 protobuf types and enum type are supported now. Encoder can create a new schema and encode data according to it. As a result a binary string is returned that can be transported by wire or decoded back by another protobuf decoder. The future versions will add support for repeated fields, .proto files parser and decode method for encoded data. @TarantoolBot document Title: Protobuf module Product: Tarantool Since: 3.1 Root document: - Protobuf decoder API Introducing protobuf encoder that can convert four main protobuf types into wire format. Varint, Len, I32, I64 protobuf types and enum data type are supported now. To encode data you need to create a schema according to which data will be encoded. The two main components of the schema are messages and enums. To create them .message and .enum functions are used Each message consists of name of the message and fields. Each field has a name, a type and an id which are set according to example: ```lua protobuf.message('KeyValue', { key = {'bytes', 1}, create_revision = {'int64', 2}, mod_revision = {'int64', 3}, version = {'int64', 4}, value = {'bytes', 5}, lease = {'int64', 6), }) ``` Each enum type consists of name of type and values. Values must have a zero value to be set as default as in example: ```lua protobuf.enum('EventType', { ['PUT'] = 0, ['DELETE'] = 1, }) ``` To create a schema .protocol function is used. This function supports forward declared types and nested messages so the tuple can be set according to example: ```lua schema = protobuf.protocol({ protobuf.message(<...>), protobuf.message(<...>), protobuf.enum(<...>), protobuf.enum(<...>), }) ``` Output schema can then be used for encoding entered data by the method named encode. This method converts input data according to chosen message definition from protobuf schema into protobuf wireformat. All fields in message definition are optional so if some input data is missing it simply will not be encoded. Input data can be submitted using luatypes or using cdata (for example entering int64) according to the example. ```lua result = schema:encode(‘KeyValue’, { key = 'protocol', version = 2, lease = 5, } ) ``` Output result will be a binary string encoded according to the protobuf standard and can be transmitted to another user.
- Loading branch information