用於處理通訊協定訊息的模組。
成員
encode_text
string proto.encode_text(x)
結構體會轉換為訊息。欄位會依名稱順序傳出。系統會忽略值為 None 的每個結構體欄位。
序列 (例如清單或元組) 會轉換為重複欄位。其元素不得為序列或字典。
字典會轉換為重複的訊息欄位,其中的欄位名稱為「key」和「value」。項目會依照疊代 (插入) 順序傳出。字典的鍵必須是字串或整數,且其值不得為序列或字典。範例:
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
|
結構體;或 NativeInfo;
必要 |