原型

报告问题 查看源代码 每夜版 · 8.4 · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

用于处理协议消息的模块。

成员

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 结构; 必需