Stay organized with collections Save and categorize content based on your preferences.
The Message class is used to define messages for efficient transmission across network or process space. Messages are defined using field classes.
Message
is provided by the protorpc.messages
module.
Messages are more restricted than normal classes in that they may only contain field attributes and other Message and Enum definitions. These restrictions are in place because the structure of the Message class itself is intended to be transmitted across network or process space and used directly by clients or even other servers. As such, methods and non-field attributes cannot be transmitted with structural information, which causes discrepancies between different languages and implementations.
Initialization and ValidationA Message object is considered to be initialized if it has all required fields and any nested messages are also initialized.
Calling 'check_initialized' will raise a ValidationError if it is not initialized; 'is_initialized' returns a boolean value indicating if it is valid.
Google Protocol RPC validates Message objects automatically when they are created and populated. Your application can validate whether a given value is compatible with a field that it is assigned to using the Field instance's validate() method. When used on a message, this method checks that all values of a message and its sub-messages are valid. Assigning an invalid value to a field raises a ValidationError.
The following example creates and initializes Message objects in a fictitious stock trading application.
from protorpc import messages # Trade type. class TradeType(messages.Enum): BUY = 1 SELL = 2 SHORT = 3 CALL = 4 class Lot(messages.Message): price = messages.IntegerField(1, required=True) quantity = messages.IntegerField(2, required=True) class Order(messages.Message): symbol = messages.StringField(1, required=True) total_quantity = messages.IntegerField(2, required=True) trade_type = messages.EnumField(TradeType, 3, required=True) lots = messages.MessageField(Lot, 4, repeated=True) limit = messages.IntegerField(5) order = Order(symbol='GOOG', total_quantity=10, trade_type=TradeType.BUY) lot1 = Lot(price=304, quantity=7) lot2 = Lot(price=305, quantity=3) order.lots = [lot1, lot2] # Now object is initialized! order.check_initialized()Constructor
The constructor of the Message class is defined as follows:
Initialize the internal messages state.
An application initializes a message via the constructor by passing in keyword arguments corresponding to field classes. For example:
class Date(Message) day = IntegerField(1) month = IntegerField(2) year = IntegerField(3)
After defining the class field, you can concisely invoke field values. The following two invocations are equivalent:
date = Date(day=6, month=6, year=1911)
Is equivalent to:
date = Date() date.day = 6 date.month = 6 date.year = 1911
The Message class provides the following class methods:
Message instances have the following methods:
Returns the assigned value of an attribute, or None if the attribute has no assigned value.
True
if the message is valid, else False
.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["The `Message` class defines structured messages for efficient data transmission across networks or processes, using field attributes, other `Message` definitions, and `Enum` types."],["`Message` objects must have all required fields populated and nested messages initialized to be considered valid, which can be checked with `check_initialized` or `is_initialized`."],["`Message` objects are validated upon creation and population, and assigning an invalid value will raise a `ValidationError`, while the `validate()` method can check value compatibility with a field."],["The `Message` constructor accepts keyword arguments corresponding to field classes, and values can be assigned directly during object creation or afterward, with the constructor supporting both methods."],["The Message class offers class methods like `all_fields`, `field_by_name`, `field_by_number` to access field information, and instance methods such as `get_assigned_value`, and `reset` for handling field values."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4