|
| 1 | +Migration Guide |
| 2 | +=============== |
| 3 | + |
| 4 | +## Google's protocolbuffers |
| 5 | + |
| 6 | +betterproto has a mostly 1 to 1 drop in replacement for Google's protocolbuffers (after |
| 7 | +regenerating your protobufs of course) although there are some minor differences. |
| 8 | + |
| 9 | +!!! note |
| 10 | + |
| 11 | + betterproto implements the same basic methods including: |
| 12 | + |
| 13 | + - `betterproto.Message.FromString` |
| 14 | + - `betterproto.Message.SerializeToString` |
| 15 | + |
| 16 | + for compatibility purposes, however it is important to note that these are |
| 17 | + effectively aliases for `betterproto.Message.parse` and |
| 18 | + `betterproto.Message.__bytes__` respectively. |
| 19 | + |
| 20 | + |
| 21 | +## One-of Support |
| 22 | + |
| 23 | +Protobuf supports grouping fields in a oneof clause. Only one of the fields in the group |
| 24 | +may be set at a given time. For example, given the proto: |
| 25 | + |
| 26 | +```proto |
| 27 | +syntax = "proto3"; |
| 28 | +
|
| 29 | +message Test { |
| 30 | + oneof foo { |
| 31 | + bool on = 1; |
| 32 | + int32 count = 2; |
| 33 | + string name = 3; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +You can use `betterproto.which_one_of(message, group_name)` to determine which of the |
| 39 | +fields was set. It returns a tuple of the field name and value, or a blank string and |
| 40 | +`None` if unset. Again this is a little different than the official Google code |
| 41 | +generator: |
| 42 | + |
| 43 | +```python |
| 44 | +# Old way (official Google protobuf package) |
| 45 | +>>> message.WhichOneof("group") |
| 46 | +"foo" |
| 47 | + |
| 48 | +# New way (this project) |
| 49 | +>>> betterproto.which_one_of(message, "group") |
| 50 | +("foo", "foo's value") |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +## Well-Known Google Types |
| 55 | + |
| 56 | +Google provides several well-known message types like a timestamp, duration, and several |
| 57 | +wrappers used to provide optional zero value support. Each of these has a special JSON |
| 58 | +representation and is handled a little differently from normal messages. The Python |
| 59 | +mapping for these is as follows: |
| 60 | + |
| 61 | +| Google Message | Python Type | Default | |
| 62 | +|-------------------------------|------------------------------------------------|--------------------------| |
| 63 | +| `google.protobuf.duration` | `datetime.timedelta` | `0` | |
| 64 | +| `google.protobuf.timestamp` | Timezone-aware `datetime.datetime` | `1970-01-01T00:00:00Z` | |
| 65 | +| `google.protobuf.*Value` | `Optional[...]` / `None` | `None` | |
| 66 | +| `google.protobuf.*` | `betterproto.lib.std.google.protobuf.*` | `None` | |
| 67 | +| `google.protobuf.*` | `betterproto.lib.pydantic.google.protobuf.*` | `None` | |
| 68 | + |
| 69 | +For the wrapper types, the Python type corresponds to the wrapped type, e.g. |
| 70 | +``google.protobuf.BoolValue`` becomes ``Optional[bool]`` while |
| 71 | +``google.protobuf.Int32Value`` becomes ``Optional[int]``. All of the optional values |
| 72 | +default to None, so don't forget to check for that possible state. |
| 73 | + |
| 74 | +Given: |
| 75 | + |
| 76 | +```proto |
| 77 | +syntax = "proto3"; |
| 78 | +
|
| 79 | +import "google/protobuf/duration.proto"; |
| 80 | +import "google/protobuf/timestamp.proto"; |
| 81 | +import "google/protobuf/wrappers.proto"; |
| 82 | +
|
| 83 | +message Test { |
| 84 | + google.protobuf.BoolValue maybe = 1; |
| 85 | + google.protobuf.Timestamp ts = 2; |
| 86 | + google.protobuf.Duration duration = 3; |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +You can use it as such: |
| 91 | + |
| 92 | +```python |
| 93 | +>>> t = Test().from_dict({"maybe": True, "ts": "2019-01-01T12:00:00Z", "duration": "1.200s"}) |
| 94 | +>>> t |
| 95 | +Test(maybe=True, ts=datetime.datetime(2019, 1, 1, 12, 0, tzinfo=datetime.timezone.utc), duration=datetime.timedelta(seconds=1, microseconds=200000)) |
| 96 | + |
| 97 | +>>> t.ts - t.duration |
| 98 | +datetime.datetime(2019, 1, 1, 11, 59, 58, 800000, tzinfo=datetime.timezone.utc) |
| 99 | + |
| 100 | +>>> t.ts.isoformat() |
| 101 | +'2019-01-01T12:00:00+00:00' |
| 102 | + |
| 103 | +>>> t.maybe = None |
| 104 | +>>> t.to_dict() |
| 105 | +{'ts': '2019-01-01T12:00:00Z', 'duration': '1.200s'} |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +## [1.2.5] to [2.0.0b1] |
| 110 | + |
| 111 | +### Updated package structures |
| 112 | + |
| 113 | +Generated code now strictly follows the *package structure* of the `.proto` files. |
| 114 | +Consequently `.proto` files without a package will be combined in a single |
| 115 | +`__init__.py` file. To avoid overwriting existing `__init__.py` files, its best |
| 116 | +to compile into a dedicated subdirectory. |
| 117 | + |
| 118 | +Upgrading: |
| 119 | + |
| 120 | +- Remove your previously compiled `.py` files. |
| 121 | +- Create a new *empty* directory, e.g. `generated` or `lib/generated/proto` etc. |
| 122 | +- Regenerate your python files into this directory |
| 123 | +- Update import statements, e.g. `import ExampleMessage from generated` |
0 commit comments