简体   繁体   English

如何将 Python proto-plus 消息 class 转换为 protobuf proto 文件?

[英]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. I see that a lot of Google Python SDK code uses the proto-plus library to write protobuf message schemas as a Python class. I was wondering if there is a way to convert this class to a proto file.我想知道是否有办法将此 class 转换为原始文件。

ie. 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)

and generate a proto file并生成一个 proto 文件

Proto Plus does not include this functionality. Proto Plus 不包含此功能。

It should be possible to do this since there's a 1:1 mapping between Proto Plus, Protobuf generated sources and proto files.应该可以这样做,因为 Proto Plus、Protobuf 生成的源和 proto 文件之间存在 1:1 映射。

But, I suspect it's an anti-pattern.但是,我怀疑这是一种反模式。 It's not unlike hacking a binary and wanting to regenerate sources.这与破解二进制文件并想要重新生成源没有什么不同。

The source-of-truth for protobuf generated code is its proto files. protobuf 生成代码的真实来源它的 proto 文件。

Google provides Proto Plus to make protoc-generated Python sources more idiomatic. Google 提供了 Proto Plus 以使 protoc 生成的 Python 源更加惯用。

Presumably Google's build pipeline now includes a generator for Proto Plus sources directly from proto files (this may be part of the SDK or is some adjunct to protoc).据推测,Google 的构建管道现在包括一个直接来自 proto 文件的 Proto Plus 源的生成器(这可能是 SDK 的一部分,或者是 protoc 的一些附属物)。

While the Proto Plus docs describe declaring messages , I think this is itself an anti-pattern and should not be encouraged as it results in protobuf code without definitive (cross-language) sources(-of-truth).虽然 Proto Plus 文档描述了声明消息,但我认为这本身就是一种反模式,不应鼓励,因为它会导致 protobuf 代码没有明确的(跨语言)来源(-of-truth)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM