Skip to content

protobuf

The protobuf package reads and writes the Protocol Buffers proto3 binary format. There is no .proto compiler step: a schema is a hashmap, and the output is compatible with any other protobuf implementation.

schema = {
1: {name: "id", type: "int32"},
2: {name: "score", type: "double"},
3: {name: "tags", type: "string", repeated: true},
4: {name: "owner", type: "message", schema: {
1: {name: "name", type: "string"}
}}
}
message = {id: 7, score: 9.81, tags: ["dev", "kiwi"], owner: {name: "Ada"}}
encoded = protobuf::encode(schema, message)
println encoded.size() # 29 bytes
decoded = protobuf::decode(schema, encoded)
println decoded["owner"]["name"] # Ada
println decoded["tags"] # ["dev", "kiwi"]

encode_file(schema, data, path) and decode_file(schema, path) read and write files directly.

A schema maps each field number to a descriptor:

KeyMeaning
nameThe key used in the data hashmap.
typeThe wire type; see below.
repeatedtrue for a list field.
schemaThe nested schema, for type: "message".

protobuf::field(name, type, repeated, schema) builds a descriptor if you prefer a function call.

TypeEncoding
int32, int64, uint32, uint64, bool, enumVarint
sint32, sint64Zigzag varint, compact for negative numbers
float, fixed32, sfixed3232-bit
double, fixed64, sfixed6464-bit
string, bytesLength-delimited
messageLength-delimited nested message; needs schema

Field numbers must match the ones used by whatever produces or consumes the data.

protobuf::encode(schema: hashmap, data: hashmap): bytes

Encodes a Kiwi hashmap into Protocol Buffer binary format.

ParameterTypeDefaultDescription
schemahashmapField descriptor hashmap keyed by field number.
datahashmapData to encode, keyed by field name.

Returns bytes — The encoded binary data.

protobuf::decode(schema: hashmap, data: bytes): hashmap

Decodes Protocol Buffer binary data into a Kiwi hashmap.

ParameterTypeDefaultDescription
schemahashmapField descriptor hashmap keyed by field number.
databytesThe binary data to decode.

Returns hashmap — Decoded data keyed by field name.

protobuf::encode_file(schema: hashmap, data: hashmap, file_path: string)

Encodes a protobuf message and writes it to a file.

ParameterTypeDefaultDescription
schemahashmapField descriptor hashmap keyed by field number.
datahashmapData to encode.
file_pathstringOutput file path.
protobuf::decode_file(schema: hashmap, file_path: string): hashmap

Reads a file and decodes it as a protobuf message.

ParameterTypeDefaultDescription
schemahashmapField descriptor hashmap keyed by field number.
file_pathstringPath to the binary file.

Returns hashmap — Decoded data keyed by field name.

Throws string — If the file does not exist.

protobuf::field(name: string, type: string, repeated: boolean = false, schema: hashmap = {}): hashmap

Builds a field descriptor hashmap.

ParameterTypeDefaultDescription
namestringField name.
typestringField type.
repeatedbooleanfalseWhether the field is repeated (default false).
schemahashmap{}Nested schema for message fields (default {}).

Returns hashmap — A field descriptor.