【发布时间】:2013-05-17 10:43:08
【问题描述】:
假设我有 2 个 Protobuf-Messages,A 和 B。它们的整体结构相似,但不完全相同。因此,我们将共享的内容移到了一个单独的消息中,我们称为 Common。这很好用。
但是,我现在面临以下问题:存在一种特殊情况,我必须处理序列化消息,但我不知道它是 A 类消息还是 B 类消息。我有一个可行的解决方案C++(如下所示),但我没能找到在 Python 中做同样事情的方法。
示例:
// file: Common.proto
// contains some kind of shared struct that is used by all messages:
message Common {
...
}
// file: A.proto
import "Common.proto";
message A {
required int32 FormatVersion = 1;
optional bool SomeFlag [default = true] = 2;
optional Common CommonSettings = 3;
... A-specific Fields ...
}
// file: B.proto
import "Common.proto";
message B {
required int32 FormatVersion = 1;
optional bool SomeFlag [default = true] = 2;
optional Common CommonSettings = 3;
... B-specific Fields ...
}
C++ 中的工作解决方案
在 C++ 中,我使用反射 API 来访问 CommonSettings 字段,如下所示:
namespace gp = google::protobuf;
...
Common* getCommonBlock(gp::Message* paMessage)
{
gp::Message* paMessage = new gp::Message();
gp::FieldDescriptor* paFieldDescriptor = paMessage->GetDescriptor()->FindFieldByNumber(3);
gp::Reflection* paReflection = paMessage->GetReflection();
return dynamic_cast<Common&>(paReflection->GetMessage(*paMessage,paFieldDescriptor));
}
方法 'getCommonBlock' 使用 FindFieldByNumber() 来获取我要获取的字段的描述符。然后它使用反射来获取实际数据。 getCommonBlock 可以处理类型 A、B 或任何未来类型的消息,只要 Common 字段位于索引 3 处。
我的问题是:有没有办法用 Python 做类似的事情?我一直在看Protobuf documentation,但想不出办法。
【问题讨论】:
标签: python reflection protocol-buffers