| 1 |
Type Marshaling |
| 2 |
=============== |
| 3 |
|
| 4 |
Proto Plus provides a service that converts between protocol buffer objects |
| 5 |
and native Python types (or the wrapper types provided by this library). |
| 6 |
|
| 7 |
This allows native Python objects to be used in place of protocol buffer |
| 8 |
messages where appropriate. In all cases, we return the native type, and are |
| 9 |
liberal on what we accept. |
| 10 |
|
| 11 |
Well-known types |
| 12 |
---------------- |
| 13 |
|
| 14 |
The following types are currently handled by Proto Plus: |
| 15 |
|
| 16 |
=================================== ======================= ======== |
| 17 |
Protocol buffer type Python type Nullable |
| 18 |
=================================== ======================= ======== |
| 19 |
``google.protobuf.BoolValue`` ``bool`` Yes |
| 20 |
``google.protobuf.BytesValue`` ``bytes`` Yes |
| 21 |
``google.protobuf.DoubleValue`` ``float`` Yes |
| 22 |
``google.protobuf.Duration`` ``datetime.timedelta`` – |
| 23 |
``google.protobuf.FloatValue`` ``float`` Yes |
| 24 |
``google.protobuf.Int32Value`` ``int`` Yes |
| 25 |
``google.protobuf.Int64Value`` ``int`` Yes |
| 26 |
``google.protobuf.ListValue`` ``MutableSequence`` Yes |
| 27 |
``google.protobuf.StringValue`` ``str`` Yes |
| 28 |
``google.protobuf.Struct`` ``MutableMapping`` Yes |
| 29 |
``google.protobuf.Timestamp`` ``datetime.datetime`` Yes |
| 30 |
``google.protobuf.UInt32Value`` ``int`` Yes |
| 31 |
``google.protobuf.UInt64Value`` ``int`` Yes |
| 32 |
``google.protobuf.Value`` JSON-encodable values Yes |
| 33 |
=================================== ======================= ======== |
| 34 |
|
| 35 |
.. note:: |
| 36 |
|
| 37 |
Protocol buffers include well-known types for ``Timestamp`` and |
| 38 |
``Duration``, both of which have nanosecond precision. However, the |
| 39 |
Python ``datetime`` and ``timedelta`` objects have only microsecond |
| 40 |
precision. This library converts timestamps to an implementation of |
| 41 |
``datetime.datetime``, DatetimeWithNanoseconds, that includes nanosecond |
| 42 |
precision. |
| 43 |
|
| 44 |
If you *write* a timestamp field using a Python ``datetime`` value, |
| 45 |
any existing nanosecond precision will be overwritten. |
| 46 |
|
| 47 |
.. note:: |
| 48 |
|
| 49 |
Setting a ``bytes`` field from a string value will first base64 decode the string. |
| 50 |
This is necessary to preserve the original protobuf semantics when converting between |
| 51 |
Python dicts and proto messages. |
| 52 |
Converting a message containing a bytes field to a dict will |
| 53 |
base64 encode the bytes field and yield a value of type str. |
| 54 |
|
| 55 |
.. code-block:: python |
| 56 |
|
| 57 |
import proto |
| 58 |
from google.protobuf.json_format import ParseDict |
| 59 |
|
| 60 |
class MyMessage(proto.Message): |
| 61 |
data = proto.Field(proto.BYTES, number=1) |
| 62 |
|
| 63 |
msg = MyMessage(data=b"this is a message") |
| 64 |
msg_dict = MyMessage.to_dict(msg) |
| 65 |
|
| 66 |
# Note: the value is the base64 encoded string of the bytes field. |
| 67 |
# It has a type of str, NOT bytes. |
| 68 |
assert type(msg_dict['data']) == str |
| 69 |
|
| 70 |
msg_pb = ParseDict(msg_dict, MyMessage.pb()) |
| 71 |
msg_two = MyMessage(msg_dict) |
| 72 |
|
| 73 |
assert msg == msg_pb == msg_two |
| 74 |
|
| 75 |
|
| 76 |
Wrapper types |
| 77 |
------------- |
| 78 |
|
| 79 |
Additionally, every :class:`~.Message` subclass is a wrapper class. The |
| 80 |
creation of the class also creates the underlying protocol buffer class, and |
| 81 |
this is registered to the marshal. |
| 82 |
|
| 83 |
The underlying protocol buffer message class is accessible with the |
| 84 |
:meth:`~.Message.pb` class method. |