Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding a pb2_module_prefix on protoc plugin #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion grpclib/plugin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
(True, True): const.Cardinality.STREAM_STREAM,
}

_PB2_MODULE_PREFIX_PARAMETER_KEY = 'pb2_module_prefix'

class Method(NamedTuple):
name: str
Expand Down Expand Up @@ -174,8 +175,9 @@ def _base_module_name(proto_file_path: str) -> str:
return basename.replace("-", "_").replace("/", ".")


_pb2_module_prefix: str = ''
def _proto2pb2_module_name(proto_file_path: str) -> str:
return _base_module_name(proto_file_path) + "_pb2"
return _pb2_module_prefix + _base_module_name(proto_file_path) + "_pb2"


def _proto2grpc_module_name(proto_file_path: str) -> str:
Expand Down Expand Up @@ -208,10 +210,19 @@ def _type_names(
parents.pop()


def _populate_config(request_parameter: str):
global _pb2_module_prefix
config = dict(item.split("=") for item in request_parameter.split(','))
if config.get(_PB2_MODULE_PREFIX_PARAMETER_KEY):
_pb2_module_prefix = config[_PB2_MODULE_PREFIX_PARAMETER_KEY]


def main() -> None:
with os.fdopen(sys.stdin.fileno(), 'rb') as inp:
request = CodeGeneratorRequest.FromString(inp.read())

_populate_config(request.parameter)

types_map: Dict[str, str] = {}
for pf in request.proto_file:
for mt in pf.message_type:
Expand Down