Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 3288090

Browse files
Add oneof documentation to generated code (#18)
1 parent 710df51 commit 3288090

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/betterproto2_compiler/plugin/models.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
FieldDescriptorProtoType as FieldType,
5757
FileDescriptorProto,
5858
MethodDescriptorProto,
59+
OneofDescriptorProto,
5960
)
6061
from betterproto2_compiler.lib.google.protobuf.compiler import CodeGeneratorRequest
6162

@@ -287,6 +288,7 @@ class MessageCompiler(ProtoContentBase):
287288
proto_obj: DescriptorProto = PLACEHOLDER
288289
path: list[int] = PLACEHOLDER
289290
fields: list[Union["FieldCompiler", "MessageCompiler"]] = field(default_factory=list)
291+
oneofs: list["OneofCompiler"] = field(default_factory=list)
290292
builtins_types: set[str] = field(default_factory=set)
291293

292294
def __post_init__(self) -> None:
@@ -566,6 +568,26 @@ def repeated(self) -> bool:
566568
return False # maps cannot be repeated
567569

568570

571+
@dataclass
572+
class OneofCompiler(ProtoContentBase):
573+
source_file: FileDescriptorProto
574+
typing_compiler: TypingCompiler
575+
path: list[int] = PLACEHOLDER
576+
577+
parent: MessageCompiler = PLACEHOLDER
578+
proto_obj: OneofDescriptorProto = PLACEHOLDER
579+
580+
def __post_init__(self) -> None:
581+
# Add oneof to message
582+
if isinstance(self.parent, MessageCompiler): # TODO why?
583+
self.parent.oneofs.append(self)
584+
super().__post_init__()
585+
586+
@property
587+
def name(self) -> str:
588+
return self.proto_obj.name
589+
590+
569591
@dataclass
570592
class EnumDefinitionCompiler(MessageCompiler):
571593
"""Representation of a proto Enum definition."""

src/betterproto2_compiler/plugin/parser.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FieldCompiler,
2323
MapEntryCompiler,
2424
MessageCompiler,
25+
OneofCompiler,
2526
OneOfFieldCompiler,
2627
OutputTemplate,
2728
PluginRequestCompiler,
@@ -217,7 +218,13 @@ def read_protobuf_type(
217218
typing_compiler=output_package.typing_compiler,
218219
)
219220
elif is_oneof(field):
220-
_make_one_of_field_compiler(output_package, source_file, message_data, field, path + [2, index])
221+
OneOfFieldCompiler(
222+
source_file=source_file,
223+
parent=message_data,
224+
proto_obj=field,
225+
path=path + [2, index],
226+
typing_compiler=output_package.typing_compiler,
227+
)
221228
else:
222229
FieldCompiler(
223230
source_file=source_file,
@@ -226,6 +233,16 @@ def read_protobuf_type(
226233
path=path + [2, index],
227234
typing_compiler=output_package.typing_compiler,
228235
)
236+
237+
for index, oneof in enumerate(item.oneof_decl):
238+
OneofCompiler(
239+
source_file=source_file,
240+
typing_compiler=output_package.typing_compiler,
241+
path=path + [8, index],
242+
parent=message_data,
243+
proto_obj=oneof,
244+
)
245+
229246
elif isinstance(item, EnumDescriptorProto):
230247
# Enum
231248
EnumDefinitionCompiler(

src/betterproto2_compiler/templates/template.py.j2

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ class {{ enum.py_name }}(betterproto2.Enum):
3232
@dataclass(eq=False, repr=False)
3333
{% endif %}
3434
class {{ message.py_name }}(betterproto2.Message):
35-
{% if message.comment %}
35+
{% if message.comment or message.oneofs %}
3636
"""
3737
{{ message.comment | indent(4) }}
38+
39+
{% if message.oneofs %}
40+
Oneofs:
41+
{% for oneof in message.oneofs %}
42+
- {{ oneof.name }}: {{ oneof.comment | indent(12) }}
43+
{% endfor %}
44+
{% endif %}
3845
"""
3946
{% endif %}
4047

tests/inputs/documentation/documentation.proto

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ message Test { // Documentation of message 3
1313
// Documentation of field 2
1414
// other line 2
1515
uint32 x = 1; // Documentation of field 3
16+
17+
// Documentation of oneof 1
18+
// other line 1
19+
20+
// Documentation of oneof 2
21+
// other line 2
22+
oneof oneof_example { // Documentation of oneof 3
23+
// Documentation of oneof field 1
24+
// other line 1
25+
26+
// Documentation of oneof field 2
27+
// other line 2
28+
int32 a = 2; // Documentation of oneof field 3
29+
}
1630
}
1731

1832
// Documentation of enum 1

0 commit comments

Comments
 (0)