訊息模組函式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
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
解析相對於 Message 定義和模組的名稱時,演算法會搜尋其路徑中找到的任何 Message 或子模組,並忽略非 Message 的值。
開頭為 '.'
的名稱視為完整合格名稱。尋找演算法會從最頂層的套件開始搜尋。舉例來說,假設有兩種訊息類型:
abc.xyz.SomeMessage
xyz.SomeMessage
搜尋相對於 abc
的 .xyz.SomeMessage
會解析為 xyz.SomeMessage
,而非 abc.xyz.SomeMessage
。對於這類名稱,實際上會將 relative_to 參數忽略,且 relative_to 參數會一律設為 None。
如要進一步瞭解套件名稱解析,請參閱通訊協定緩衝區套件指定碼。
引數
- name
- 要查找的定義名稱。可以是完整名稱或相對名稱。
- relative_to
- 搜尋與訊息定義或模組相關的定義。如果為 None,則會觸發完整名稱的搜尋。
傳回與該名稱相關聯的 Enum 或 Message 類別定義。
如果在所有搜尋路徑中都找不到任何定義,則會發出 DefinitionNotFoundError 錯誤。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-08 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-08 (世界標準時間)。"],[[["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."]]],[]]