MCPcopy Index your code
hub / github.com/danielgtaylor/python-betterproto / traverse

Function traverse

src/betterproto/plugin/parser.py:48–73  ·  view source on GitHub ↗
(
    proto_file: FileDescriptorProto,
)

Source from the content-addressed store, hash-verified

46
47
48def traverse(
49 proto_file: FileDescriptorProto,
50) -> Generator[
51 Tuple[Union[EnumDescriptorProto, DescriptorProto], List[int]], None, None
52]:
53 # Todo: Keep information about nested hierarchy
54 def _traverse(
55 path: List[int],
56 items: Union[List[EnumDescriptorProto], List[DescriptorProto]],
57 prefix: str = "",
58 ) -> Generator[
59 Tuple[Union[EnumDescriptorProto, DescriptorProto], List[int]], None, None
60 ]:
61 for i, item in enumerate(items):
62 # Adjust the name since we flatten the hierarchy.
63 # Todo: don't change the name, but include full name in returned tuple
64 item.name = next_prefix = f"{prefix}_{item.name}"
65 yield item, [*path, i]
66
67 if isinstance(item, DescriptorProto):
68 # Get nested types.
69 yield from _traverse([*path, i, 4], item.enum_type, next_prefix)
70 yield from _traverse([*path, i, 3], item.nested_type, next_prefix)
71
72 yield from _traverse([5], proto_file.enum_type)
73 yield from _traverse([4], proto_file.message_type)
74
75
76def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse:

Callers 1

generate_codeFunction · 0.85

Calls 1

_traverseFunction · 0.85

Tested by

no test coverage detected