消息模块函数
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
protorpc.messages
软件包提供了以下定位实用函数:
- find_definition(name, relative_to=None)
-
根据模块空间中的名称查找定义。
查找算法根据相对于消息定义的名称或完全限定名称查找定义。如果没有找到相对于 relative_to
参数的定义,它将搜索 relative_to
的容器。如果 relative_to
是嵌套式消息,则会搜索其 message_definition()。如果该消息没有 message_definition(),则搜索其模块。如果 relative_to
是模块,则查找算法会搜索与之相对的包含模块和搜索。如果该模块是一个顶级模块,则查找算法会使用完全限定名称搜索消息。如果仍然没有找到消息,则搜索失败,并且该方法引发 DefinitionNotFoundError。
例如,在查找任何 foo.bar.ADefinition
定义(相对于实际消息定义 abc.xyz.SomeMessage
)时,请使用:
find_definition('foo.bar.ADefinition', SomeMessage)
该方法的参数所遵循的模式与完全限定名称搜索类似。
abc.xyz.SomeMessage. foo.bar.ADefinition
abc.xyz. foo.bar.ADefinition
abc. foo.bar.ADefinition
foo.bar.ADefinition
在解析相对于消息定义和模块的名称时,该算法会搜索路径中找到的所有消息或子模块,并忽略非消息值。
以 '.'
开头的名称将被视为完全限定名称。查找算法将从最顶层的包开始搜索。例如,假设有以下两种消息类型:
abc.xyz.SomeMessage
xyz.SomeMessage
搜索 .xyz.SomeMessage
(相对于 abc
)将解析为 xyz.SomeMessage
,而不是 abc.xyz.SomeMessage
。对于这种类型的名称,会有效忽略 relative_to 参数,并始终将其设置为 None。
如需详细了解程序包名称解析,请参阅 Protocol Buffer Package 说明符。
参数
- name
- 要查找的定义的名称。可以是完全限定名称,也可以是相对名称。
- relative_to
- 搜索相对于消息定义或模块的定义。如果为 None,将导致完全限定名称搜索。
返回与名称关联的 Enum 或 Message 类定义。
如果在任何搜索路径中都找不到定义,则引发 DefinitionNotFoundError。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-16。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-06-16。"],[[["The `find_definition` function in `protorpc.messages` locates definitions by name, either relative to a message or module or by a fully qualified name."],["The search algorithm for `find_definition` prioritizes searching relative to the `relative_to` parameter, progressing up the module hierarchy if necessary and then falling back to searching by fully qualified name."],["A name starting with `'.'` in `find_definition` is treated as fully qualified, starting the search from the topmost package and ignoring the `relative_to` parameter."],["The `find_definition` method requires two arguments: `name`, which can be fully qualified or relative, and `relative_to`, that specifies a definition to start the search from."],["`find_definition` will return an `Enum` or `Message` class definition, and in case a definition cannot be found, it will raise a `DefinitionNotFoundError` exception."]]],[]]