Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write my own code generator of protobuf

Google protobuf is a nice IDL for RPC. But I want to know how to write my own code generator for protobuf.

like image 312
guilin 桂林 Avatar asked Mar 10 '15 07:03

guilin 桂林


People also ask

How do I create a proto code?

To generate code from the proto file, use a gRPC plug-in with protoc. This process generates client code, server code, and protocol buffer code for populating, serializing, and retrieving message types. For more information, see Introduction to gRPC in the gRPC documentation.

Is protobuf a programming language?

"Protobuf (Protocol buffers) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

How are Protobufs encoded?

Protobuf encodingA Protobuf message is structured as a collection of key/value pairs, with the numeric tag as the key and the corresponding field as the value.


1 Answers

The protoc compiler can output a protobuf-formatted description of the .proto file. That way most of the parsing has been done for you already, and you only need to generate the output you want.

The .proto schema for the .proto file description is here: https://github.com/google/protobuf/blob/master/src/google/protobuf/descriptor.proto

As an additional step, you can make your generator runnable via an "-mygenerator-out=." option on protoc itself: https://developers.google.com/protocol-buffers/docs/reference/other

Here is one (albeit a bit convoluted) example on how a code generator can be written in Python: https://github.com/nanopb/nanopb/blob/master/generator/nanopb_generator.py

like image 185
jpa Avatar answered Oct 06 '22 07:10

jpa