Google protobuf 是一个很好的 RPC IDL。但我想知道如何为 protobuf 编写自己的代码生成器。
最佳答案
protoc
编译器可以输出 .proto 文件的 protobuf 格式的描述。这样大部分解析工作已经为您完成,您只需要生成您想要的输出。
.proto 文件描述的 .proto 架构在这里:
https://github.com/google/protobuf/blob/master/src/google/protobuf/descriptor.proto
作为附加步骤,您可以通过“-mygenerator-out=”使您的生成器可运行。 protoc 本身的选项:
https://developers.google.com/protocol-buffers/docs/reference/other
这是一个关于如何用 Python 编写代码生成器的示例(虽然有点复杂):
https://github.com/nanopb/nanopb/blob/master/generator/nanopb_generator.py
关于protocol-buffers - 如何编写自己的 protobuf 代码生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958135/