原型

回報問題 查看來源 Nightly

通訊協定訊息處理的模組。

成員

encode_text

string proto.encode_text(x)

傳回結構引數的編碼,格式為文字格式通訊協定訊息。 資料結構必須以遞迴方式由字串、整數、浮點數或布林值組成,或是這些類型的結構體、序列和字典。

結構體會轉換為訊息。系統會按照名稱順序發出欄位。值為 None 的每個結構欄位都會遭到忽略。

序列 (例如清單或元組) 會轉換為重複欄位。其中的元素不得為序列或字典。

系統會將字典轉換為訊息的重複欄位,欄位中名稱為「key」和「value」。項目會按疊代 (插入) 順序發出。字典的鍵必須為字串或 int,且其值不可以是序列或字典。例如:

proto.encode_text(struct(field=123))
# field: 123

proto.encode_text(struct(field=True))
# field: true

proto.encode_text(struct(field=[1, 2, 3]))
# field: 1
# field: 2
# field: 3

proto.encode_text(struct(field='text', ignored_field=None))
# field: "text"

proto.encode_text(struct(field=struct(inner_field='text', ignored_field=None)))
# field {
#   inner_field: "text"
# }

proto.encode_text(struct(field=[struct(inner_field=1), struct(inner_field=2)]))
# field {
#   inner_field: 1
# }
# field {
#   inner_field: 2
# }

proto.encode_text(struct(field=struct(inner_field=struct(inner_inner_field='text'))))
# field {
#    inner_field {
#     inner_inner_field: "text"
#   }
# }

proto.encode_text(struct(foo={4: 3, 2: 1}))
# foo: {
#   key: 4
#   value: 3
# }
# foo: {
#   key: 2
#   value: 1
# }

參數

參數 說明
x 結構;必要