如何在 java 中为 protobuf DynamicMessage 设置扩展? [英] How do I set extension to protobuf DynamicMessage in java?
问题描述
我正在从运行时加载的 .proto
文件在 java 中构建 DynamicMessage
.
I'm constructing DynamicMessage
in java from .proto
file I loaded during runtime.
我的问题是为此类消息设置扩展名.
My problem is setting extension to such message.
我有:
Descriptors.Descriptor
包含类型和为其创建的DynamicMessage
Descriptors.Descriptor
扩展类型和为其创建的DynamicMessage
Descriptors.Descriptor
of the containing type andDynamicMessage
created for itDescriptors.Descriptor
of the extension type andDynamicMessage
created for it
现在我不知道如何设置包含消息的扩展名.
Now I don't know how to set the extension to the containing message.
包含描述符,如果我要求字段列表,则仅列出扩展名中没有字段的字段.这是有道理的.
The containing descriptor, if I ask for field list, lists only the fields without the fields in the extension. Which makes sense.
扩展描述符只有来自扩展类型的字段(它没有来自包含类型的字段).
The extension descriptor has only the fields from the extending type (it doesn't have the fields from containing type).
请建议我如何将这些组合在一起.
Please advice how I can combine these together.
要了解上下文,您可能需要查看我之前关于此主题的不关心扩展的问题:运行时生成的协议缓冲区对象
To get to the context you might want to have a look on my previous question on this topic which didn't care about extensions: Protocol buffer objects generated at runtime
事实上,我正在寻找生成消息的 .setExtension
的类比.我注意到只有 GeneratedMessage
扩展了 ExtendableMessage
但我相信一定有办法:)
In fact I'm looking for analogy of .setExtension
of the generated message. I noticed that only GeneratedMessage
extends ExtendableMessage
but I believe there must be a way :)
推荐答案
扩展由 FieldDescriptor
描述,就像常规字段一样,所以在使用动态接口时,实际上使用完全相同的方法访问任何一个.
Extensions are described by FieldDescriptor
s, just like regular fields, so when using the dynamic interfaces, you actually use exactly the same methods to access either one.
请记住,扩展实际上是独立于扩展类型或扩展类型声明的.例如,这是有效的:
Keep in mind that extensions are actually declared independently from either the extended type or the extension type. For example, this is valid:
message Foo { extensions 1000 to max; }
message Bar { ... }
extend Foo {
optional Bar ext1 = 1234;
optional Bar ext2 = 2345;
}
请注意,我们为 Bar
类型的 Foo
声明了 两个 扩展.因此,仅仅知道您正在寻找 Bar
类型的扩展是不够的——您必须指定哪个扩展.
Notice that we declared two extensions to Foo
of type Bar
. So, simply knowing that you're looking for an extension of type Bar
is not good enough -- you have to specify which one.
在任何情况下,诸如 FileDescriptor
和 Descriptor
的各种描述符类型都有 findExtensionByName()
方法,可用于查找扩展名描述符.请注意,此方法查找在您调用它的描述符的范围 内声明的扩展——它不会找到到该消息类型的扩展.也就是说,如果您有:
In any case, the various descriptor types like FileDescriptor
and Descriptor
have findExtensionByName()
methods which can be used to look up an extension descriptor. Note that this method looks for extensions declared within the scope of the descriptor on which you call it -- it does not find extensions to that message type. That is, if you have:
message Foo { extensions 1000 to max; }
message Bar {
extend Foo {
optional int32 ext1 = 1234;
}
}
extend Foo {
optional int32 ext2 = 2345;
}
为了找到扩展名 ext1
,你必须在 Descriptor
上为 调用
,不是 findExtensionByName("ext1")
BarFoo
的描述符.要查找ext2
,您必须在文件的FileDescriptor
上调用findExtensionByName("ext2")
.
In order to find the extension ext1
, you would have to call findExtensionByName("ext1")
on the Descriptor
for Bar
, not the descriptor for Foo
. To find ext2
, you would have to call findExtensionByName("ext2")
on the file's FileDescriptor
.
您可能还想查看 ExtensionRegistry
类,可用于通过完全限定名称查找扩展(例如 "mypackage.Bar.ext1"
),但是这要求您首先注册您感兴趣的描述符,因此它可能不会有太大帮助.
You may also want to take a look at the ExtensionRegistry
class, which can be used to look up extensions by their fully-qualified name (e.g. "mypackage.Bar.ext1"
), but this requires that you first register the descriptors you're interested in, so it might not be terribly helpful.
这篇关于如何在 java 中为 protobuf DynamicMessage 设置扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!