![](/img/trans.png)
[英]Convert Python generated protobuf to java without .proto file
[英]How to convert a Python proto-plus Message class into protobuf proto file?
I see that a lot of Google Python SDK code uses the proto-plus library to write protobuf message schemas as a Python class. 我想知道是否有辦法將此 class 轉換為原始文件。
IE。
import proto
class Composer(proto.Message):
given_name = proto.Field(proto.STRING, number=1)
family_name = proto.Field(proto.STRING, number=2)
class Song(proto.Message):
composer = proto.Field(Composer, number=1)
title = proto.Field(proto.STRING, number=2)
lyrics = proto.Field(proto.STRING, number=3)
year = proto.Field(proto.INT32, number=4)
並生成一個 proto 文件
Proto Plus 不包含此功能。
應該可以這樣做,因為 Proto Plus、Protobuf 生成的源和 proto 文件之間存在 1:1 映射。
但是,我懷疑這是一種反模式。 這與破解二進制文件並想要重新生成源沒有什么不同。
protobuf 生成代碼的真實來源是它的 proto 文件。
Google 提供了 Proto Plus 以使 protoc 生成的 Python 源更加慣用。
據推測,Google 的構建管道現在包括一個直接來自 proto 文件的 Proto Plus 源的生成器(這可能是 SDK 的一部分,或者是 protoc 的一些附屬物)。
雖然 Proto Plus 文檔描述了聲明消息,但我認為這本身就是一種反模式,不應鼓勵,因為它會導致 protobuf 代碼沒有明確的(跨語言)來源(-of-truth)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.