Google Cloud Platform

The MessageField Class

The MessageField class is used to define messages for efficient transmission across network or process space. Messages are defined using field classes.

MessageField is provided by the protorpc.messages module.

  1. MessageField()
  2. Class Property


The constructor of the MessageField class is defined as follows:

class MessageField(message_type, number, [required=False | repeated=False])

Defines fields for sub-message values.


The message type for the field. Must be a subclass of Message.
Number of the field. Must be unique per message class.
Whether or not this field is required. Mutually exclusive with the repeated argument; do not specify repeated=True if you use required=True.
Whether or not this field is repeated. Mutually exclusive with the required argument; do not specify required=True if you use repeated=True.

Raises a FieldDefinitionError if the message_type is invalid.

Class Property

The MessageField class provides the following properties:

The Python type used for values of this field. For example, in the case of DateTimeField, type is datetime.datetime. For user defined MessageFields, type is the specified Message type.
The underlying message type used for serialization. Specifically this is the type that you can store on an instance of a Message class. For example, for DateTimeField, the type will be message_types.DateTimeMessage. For normal message fields it will be the protorpc message subclass. For example:
class Sub(messages.Message):
  x = messages.IntegerField(1)

class M(messages.Message):
  sub = messages.MessageField(Sub, 1)
  dt = message_types.DateTimeField(2)

print 'M.sub.type         =', M.sub.type
print 'M.sub.message_type =', M.sub.message_type
print 'M.dt.type          =', M.dt.type
print 'M.dt.message_type  =', M.dt.message_type

=== output ===
M.sub.type                 = <class '__main__.Sub>
M.sub.message_type = <class '__main__.Sub'>
M.dt.type                   = <type 'datetime.datetime'>
M.dt.message_type   = <class 'protorpc.message_types.DateTimeMessage'>