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

Commit 7e6c75c

Browse files
committed
Add server generation option
1 parent 64be741 commit 7e6c75c

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/betterproto2_compiler/plugin/parser.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
CodeGeneratorResponseFeature,
1515
CodeGeneratorResponseFile,
1616
)
17-
from betterproto2_compiler.settings import ClientGeneration, Settings
17+
from betterproto2_compiler.settings import ClientGeneration, ServerGeneration, Settings
1818

1919
from .compiler import outputfile_compiler
2020
from .models import (
@@ -62,22 +62,27 @@ def _traverse(
6262
def get_settings(plugin_options: list[str]) -> Settings:
6363
# Synchronous clients are suitable for most users
6464
client_generation = ClientGeneration.SYNC
65+
server_generation = ServerGeneration.NONE
6566

6667
for opt in plugin_options:
6768
if opt.startswith("client_generation="):
6869
name = opt.split("=")[1]
69-
70-
# print(ClientGeneration.__members__, file=sys.stderr)
71-
# print([member.value for member in ClientGeneration])
72-
7370
try:
7471
client_generation = ClientGeneration(name)
7572
except ValueError:
7673
raise ValueError(f"Invalid client_generation option: {name}")
7774

75+
if opt.startswith("server_generation="):
76+
name = opt.split("=")[1]
77+
try:
78+
server_generation = ServerGeneration(name)
79+
except ValueError:
80+
raise ValueError(f"Invalid server_generation option: {name}")
81+
7882
return Settings(
7983
pydantic_dataclasses="pydantic_dataclasses" in plugin_options,
8084
client_generation=client_generation,
85+
server_generation=server_generation,
8186
)
8287

8388

src/betterproto2_compiler/settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ class ClientGeneration(StrEnum):
2020
SYNC_ASYNC = "sync_async"
2121
"""Both synchronous and asynchronous clients are generated.
2222
23-
The asynchronous client is generated with the Async suffix."""
23+
Asynchronous clients are generated with the Async suffix."""
2424

2525
ASYNC_SYNC = "async_sync"
2626
"""Both synchronous and asynchronous clients are generated.
2727
28-
The synchronous client is generated with the Sync suffix."""
28+
Synchronous clients are generated with the Sync suffix."""
2929

3030
SYNC_ASYNC_NO_DEFAULT = "sync_async_no_default"
3131
"""Both synchronous and asynchronous clients are generated.
3232
33-
The synchronous client is generated with the Sync suffix, and the asynchronous client is generated with the Async
33+
Synchronous clients are generated with the Sync suffix, and asynchronous clients are generated with the Async
3434
suffix."""
3535

3636
@property
@@ -60,8 +60,14 @@ def is_async_prefixed(self) -> bool:
6060
return self in {ClientGeneration.SYNC_ASYNC, ClientGeneration.SYNC_ASYNC_NO_DEFAULT}
6161

6262

63+
class ServerGeneration(StrEnum):
64+
NONE = "none"
65+
ASYNC = "async"
66+
67+
6368
@dataclass
6469
class Settings:
6570
pydantic_dataclasses: bool
6671

6772
client_generation: ClientGeneration
73+
server_generation: ServerGeneration

src/betterproto2_compiler/templates/template.py.j2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ default_message_pool.register_message("{{ output_file.package }}", "{{ message.p
102102
{{ i }}
103103
{% endfor %}
104104

105+
{% if output_file.settings.server_generation == "async" %}
105106
{% for _, service in output_file.services|dictsort(by="key") %}
106107
class {{ service.py_name }}Base(ServiceBase):
107108
{% if service.comment %}
@@ -170,3 +171,4 @@ class {{ service.py_name }}Base(ServiceBase):
170171
}
171172

172173
{% endfor %}
174+
{% endif %}

0 commit comments

Comments
 (0)