8000 Make plugin use betterproto generated classes internally · fixpoint/python-betterproto@18f22fa · GitHub
[go: up one dir, main page]

Skip to content

Commit 18f22fa

Browse files
committed
Make plugin use betterproto generated classes internally
This means the betterproto plugin no longer needs to depend durectly on protobuf. This requires a small runtime hack to monkey patch some google types to get around the fact that the compiler uses proto2, but betterproto expects proto3. Also: - regenerate google.protobuf package - fix a regex bug in the logic for determining whether to use a google wrapper type. - fix a bug causing comments to get mixed up when multiple proto files generate code into a single python module
1 parent 8f7af27 commit 18f22fa

File tree

12 files changed

+1838
-579
lines changed

12 files changed

+1838
-579
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ Assuming your `google.protobuf` source files (included with all releases of `pro
413413

414414
```sh
415415
protoc \
416-
--plugin=protoc-gen-custom=betterproto/plugin.py \
416+
--plugin=protoc-gen-custom=src/betterproto/plugin/main.py \
417417
--custom_opt=INCLUDE_GOOGLE \
418-
--custom_out=betterproto/lib \
418+
--custom_out=src/betterproto/lib \
419419
-I /usr/local/include/ \
420420
/usr/local/include/google/protobuf/*.proto
421421
```

src/betterproto/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ def which_one_of(message: Message, group_name: str) -> Tuple[str, Optional[Any]]
11491149
BytesValue,
11501150
DoubleValue,
11511151
Duration,
1152+
EnumValue,
11521153
FloatValue,
11531154
Int32Value,
11541155
Int64Value,
@@ -1215,14 +1216,17 @@ def from_dict(self: T, value: Any) -> T:
12151216

12161217
def _get_wrapper(proto_type: str) -> Type:
12171218
"""Get the wrapper message class for a wrapped type."""
1219+
1220+
# TODO: include ListValue and NullValue?
12181221
return {
12191222
TYPE_BOOL: BoolValue,
1223+
TYPE_BYTES: BytesValue,
1224+
TYPE_DOUBLE: DoubleValue,
1225+
TYPE_FLOAT: FloatValue,
1226+
TYPE_ENUM: EnumValue,
12201227
TYPE_INT32: Int32Value,
1221-
TYPE_UINT32: UInt32Value,
12221228
TYPE_INT64: Int64Value,
1223-
TYPE_UINT64: UInt64Value,
1224-
TYPE_FLOAT: FloatValue,
1225-
TYPE_DOUBLE: DoubleValue,
12261229
TYPE_STRING: StringValue,
1227-
TYPE_BYTES: BytesValue,
1230+
TYPE_UINT32: UInt32Value,
1231+
TYPE_UINT64: UInt64Value,
12281232
}[proto_type]

src/betterproto/lib/google/protobuf/__init__.py

Lines changed: 604 additions & 443 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/lib/google/protobuf/compiler/__init__.py

Lines changed: 125 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/plugin/main.py

100644100755
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@
33
import os
44
import sys
55

6-
from google.protobuf.compiler import plugin_pb2 as plugin
6+
from betterproto.lib.google.protobuf.compiler import (
7+
CodeGeneratorRequest,
8+
CodeGeneratorResponse,
9+
)
710

811
from betterproto.plugin.parser import generate_code
12+
from betterproto.plugin.models import monkey_patch_oneof_index
913

1014

1115
def main() -> None:
1216
"""The plugin's main entry point."""
1317
# Read request message from stdin
1418
data = sys.stdin.buffer.read()
1519

20+
# Apply Work around for proto2/3 difference in protoc messages
21+
monkey_patch_oneof_index()
22+
1623
# Parse request
17-
request = plugin.CodeGeneratorRequest()
18-
request.ParseFromString(data)
24+
request = CodeGeneratorRequest()
25+
request.parse(data)
1926

2027
dump_file = os.getenv("BETTERPROTO_DUMP")
2128
if dump_file:
2229
dump_request(dump_file, request)
2330

2431
# Create response
25-
response = plugin.CodeGeneratorResponse()
32+
response = CodeGeneratorResponse()
2633

2734
# Generate code
2835
generate_code(request, response)
@@ -34,7 +41,7 @@ d 637E ef main() -> None:
3441
sys.stdout.buffer.write(output)
3542

3643

37-
def dump_request(dump_file: str, request: plugin.CodeGeneratorRequest) -> None:
44+
def dump_request(dump_file: str, request: CodeGeneratorRequest) -> None:
3845
"""
3946
For developers: Supports running plugin.py standalone so its possible to debug it.
4047
Run protoc (or generate.py) with BETTERPROTO_DUMP="yourfile.bin" to write the request to a file.

0 commit comments

Comments
 (0)
0