Protobuf support in Onyx.
This package only provides functional for marshaling and unmarshaling data from protobufs.
To generate Onyx bindings from .proto definitions, use protoc-gen-onyx.
Given api.proto containing
package my_api;
message Query {
string name = 1;
optional int32 max_results = 2;
}You need to generate Onyx bindings using protoc-gen-onyx.
Use this command,
$ protoc --onyx_out=protos api.protoTo generate protos/api.proto.onyx.
package protobuf.my_api
use protobuf
@protobuf.Message
Query :: struct {
@protobuf.Field.{1, 9}
name: str
@protobuf.Field.{2, 5}
max_results: ? i32
}You can the protobuf package to marshal/unmarshal the data.
// Load the packages in the project
#load "./lib/packages.onyx"
// Make sure you load the `proto.onyx` file from the out directory.
#load "./protos/proto.onyx"
use protobuf
use protobuf.my_api { Query }
main :: () {
query := Query.{
name = "foo"
max_results = 10
}
// Marshal data
query_data := protobuf.marshal(query)! // This doesn't do any error handling
// Unmarshal data
decoded_query := protobuf.unmarshal(query_data, Query)!
logf(.Info, "Query: {p}", decoded_query)
}oneofhandlingLENfield concatenation