gRPC

  • Post author:
  • Post category:其他


Protocol buffer data is structured as messages, where each message is a small logical record of information containing a series of name-value pairs called fields.

Service method:

Unary RPC

Server streaming RPC

Client streaming RPC

Bidirectional streaming RPC

Fields can be normal fields, oneof fields, or map fields. A field has a type and field number.

What’s Generated From Your .proto?

the Python compiler generates a module with a static descriptor of each message type in your .proto, which is then used with a metaclass to create the necessary Python data access class at runtime.

Python strings are represented as unicode on decode but can be str if an ASCII string is given (this is subject to change).

Enum definitions must start with enum value zero.

Maps:

where the key_type can be any integral or string type (so, any scalar type except for floating point types and bytes). Note that enum is not a valid key_type. The value_type can be any type except another map.

Map fields cannot be repeated.

Wire format ordering and map iteration ordering of map values is undefined, so you cannot rely on your map items being in a particular order.

When generating text format for a .proto, maps are sorted by key. Numeric keys are sorted numerically.

In Python, the package directive is ignored, since Python modules are organized according to their location in the file system.

Each field must be annotated with one of the following modifiers:

required: a value for the field must be provided, otherwise the message will be considered “uninitialized”. Serializing an uninitialized message will raise an exception. Parsing an uninitialized message will fail. Other than this, a required field behaves exactly like an optional field.

optional: the field may or may not be set. If an optional field value isn’t set, a default value is used. For simple types, you can specify your own default value, as we’ve done for the phone number type in the example. Otherwise, a system default is used: zero for numeric types, the empty string for strings, false for bools. For embedded messages, the default value is always the “default instance” or “prototype” of the message, which has none of its fields set. Calling the accessor to get the value of an optional (or required) field which has not been explicitly set always returns that field’s default value.

repeated: the field may be repeated any number of times (including zero). The order of the repeated values will be preserved in the protocol buffer. Think of repeated fields as dynamically sized arrays.

Extending the protocol buffer:

  • you must not change the tag numbers of any existing fields.
  • you must not add or delete any required fields.
  • you may delete optional or repeated fields.
  • you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).

If you follow these rules, old code will happily read new messages and simply ignore any new fields. To the old code, optional fields that were deleted will simply have their default value, and deleted repeated fields will be empty. New code will also transparently read old messages. However, keep in mind that new optional fields will not be present in old messages, so you will need to either check explicitly whether they’re set with has_, or provide a reasonable default value in your .proto file with [default = value] after the tag number. If the default value is not specified for an optional element, a type-specific default value is used instead: for strings, the default value is the empty string. For booleans, the default value is false. For numeric types, the default value is zero. Note also that if you added a new repeated field, your new code will not be able to tell whether it was left empty (by new code) or never set at all (by old code) since there is no has_ flag for it.



版权声明:本文为phoenix198425原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。