Support compiling google protobuf files

This commit is contained in:
boukeversteegh 2020-05-29 15:19:00 +02:00
parent eec24e4ee8
commit f087c6c9bd
2 changed files with 21 additions and 1 deletions

View File

@ -256,6 +256,7 @@ Google provides several well-known message types like a timestamp, duration, and
| `google.protobuf.duration` | [`datetime.timedelta`][td] | `0` |
| `google.protobuf.timestamp` | Timezone-aware [`datetime.datetime`][dt] | `1970-01-01T00:00:00Z` |
| `google.protobuf.*Value` | `Optional[...]` | `None` |
| `google.protobuf.*` | `betterproto.lib.google.protobuf.*` | `None` |
[td]: https://docs.python.org/3/library/datetime.html#timedelta-objects
[dt]: https://docs.python.org/3/library/datetime.html#datetime.datetime
@ -354,6 +355,21 @@ $ pipenv run generate
$ pipenv run test
```
### (Re)compiling Google Well-known Types
Betterproto includes compiled versions for Google's well-known types at [betterproto/lib/google](betterproto/lib/google).
Be sure to regenerate these files when modifying the plugin output format, and validate by running the tests.
Normally, the plugin does not compile any references to `google.protobuf`, since they are pre-compiled. To force compilation of `google.protobuf`, set this environment variable: `PROTOBUF_OPTS=INCLUDE_GOOGLE`.
Assuming your `google.protobuf` source files (included with all releases of `protoc`) are located in `/usr/local/include`, you can regenerate them as follows:
```sh
export PROTOBUF_OPTS=INCLUDE_GOOGLE
protoc --plugin=protoc-gen-custom=betterproto/plugin.py --custom_out=betterproto/lib -I/usr/local/include/ /usr/local/include/google/protobuf/*.proto
```
### TODO
- [x] Fixed length fields

View File

@ -182,6 +182,9 @@ def get_comment(proto_file, path: List[int], indent: int = 4) -> str:
def generate_code(request, response):
plugin_options = os.environ.get("BETTERPROTO_OPTS")
plugin_options = plugin_options.split(" ") if plugin_options else []
env = jinja2.Environment(
trim_blocks=True,
lstrip_blocks=True,
@ -192,7 +195,8 @@ def generate_code(request, response):
output_map = {}
for proto_file in request.proto_file:
out = proto_file.package
if out == "google.protobuf":
if out == "google.protobuf" and "INCLUDE_GOOGLE" not in plugin_options:
continue
if not out: