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

Change logic for generating import statement #65

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
7 changes: 4 additions & 3 deletions protobuf_to_pydantic/plugin/field_desc_proto_to_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ def _add_other_module_pkg(self, other_fd: FileDescriptorProto, type_str: str) ->
for _index in range(min(len(fd_path_list), len(message_path_list))):
if message_path_list[_index] == fd_path_list[_index]:
index = _index
else:
break
Comment on lines +109 to +110
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the parent paths are different, the loop should exit.

# common/a/name.proto includes common/b/include.proto
# The basic name: include_p2p
module_name: str = message_path_list[-1].replace(".proto", "") + self.config.file_name_suffix
# Add non-shared parts: b.include_p2p
module_name = ".".join(message_path_list[index + 1 : -1] + (module_name,))

logger.info((self._fd.name, other_fd.name, index))
if index != -1:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When importing generated files from the destination project, paths should always be relative, even if they start from the root.

# Add relative parts: ..b.include_p2p
module_name = "." * (len(message_path_list) - (index + 1)) + module_name
# Add relative parts: ..b.include_p2p
module_name = "." * (len(message_path_list) - (index + 1)) + module_name
self._add_import_code(module_name, type_str)

def _comment_handler(self, leading_comments: str, trailing_comments: str) -> Tuple[dict, str, str]:
Expand Down