Skip to content

Commit ce17577

Browse files
committed
Centralize parsing of options
INCLUDE_GOOGLE option is now parsed in parse_options.
1 parent 9e3ba53 commit ce17577

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/betterproto/plugin/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def comment(self) -> str:
183183
@dataclass
184184
class Options:
185185
grpc_kind: str = "grpclib"
186+
include_google: bool = False
186187

187188

188189
@dataclass

src/betterproto/plugin/parser.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
if TYPE_CHECKING:
4343
from google.protobuf.descriptor import Descriptor
4444

45+
4546
def traverse(
4647
proto_file: FieldDescriptorProto,
4748
) -> "itertools.chain[Tuple[Union[str, EnumDescriptorProto], List[int]]]":
@@ -68,30 +69,30 @@ def _traverse(
6869
_traverse([5], proto_file.enum_type), _traverse([4], proto_file.message_type)
6970
)
7071

72+
7173
def parse_options(plugin_options: List[str]) -> Options:
7274
options = Options()
7375
for option in plugin_options:
7476
if option.startswith("grpc="):
7577
options.grpc_kind = option.split("=", 1)[1]
78+
if option == "INCLUDE_GOOGLE":
79+
options.include_google = True
7680
return options
7781

82+
7883
def generate_code(
7984
request: plugin.CodeGeneratorRequest, response: plugin.CodeGeneratorResponse
8085
) -> None:
81-
plugin_options = request.parameter.split(",") if request.parameter else []
82-
83-
options = parse_options(plugin_options)
86+
plugin_options = parse_options(
87+
request.parameter.split(",") if request.parameter else []
88+
)
8489

8590
request_data = PluginRequestCompiler(
86-
plugin_request_obj=request,
87-
options=options
91+
plugin_request_obj=request, options=plugin_options
8892
)
8993
# Gather output packages
9094
for proto_file in request.proto_file:
91-
if (
92-
proto_file.package == "google.protobuf"
93-
and "INCLUDE_GOOGLE" not in plugin_options
94-
):
95+
if proto_file.package == "google.protobuf" and plugin_options.include_google:
9596
# If not INCLUDE_GOOGLE,
9697
# skip re-compiling Google's well-known types
9798
continue

0 commit comments

Comments
 (0)