Extensions > API reference > ProtoModule

ProtoModule

A module for protocol message processing.

Members

encode_text

string ProtoModule.encode_text(x)

Returns the struct argument's encoding as a text-format protocol message. The data structure must be recursively composed of strings, ints, floats, or bools, or structs, sequences, and dicts of these types.

A struct is converted to a message. Fields are emitted in name order.

A sequence (such as a list or tuple) is converted to a repeated field. Its elements must not be sequences or dicts.

A dict is converted to a repeated field of messages with fields named 'key' and 'value'. Entries are emitted in iteration (insertion) order. The dict's keys must be strings, ints, or bools, and its values must not be sequences or dicts. Examples:

struct(field=123).to_proto()
# field: 123

struct(field=True).to_proto()
# field: true

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

struct(field='text').to_proto()
# field: "text"

struct(field=struct(inner_field='text')).to_proto()
# field {
#   inner_field: "text"
# }

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

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

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

Parameters

Parameter Description
x ; required