Skip to content

Commit

Permalink
feat: useOptionals=all
Browse files Browse the repository at this point in the history
  • Loading branch information
ii64 committed Apr 9, 2024
1 parent 59948e6 commit 48ea4ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,8 @@ def to_dict(
elif field_is_repeated:
# Convert each item.
cls = self._betterproto.cls_by_field[field_name]
if not value:
value = []
if cls == datetime:
value = [_Timestamp.timestamp_to_json(i) for i in value]
elif cls == timedelta:
Expand Down
11 changes: 9 additions & 2 deletions src/betterproto/plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@
reference to `A` to `B`'s `fields` attribute.
"""


import builtins
import re
import textwrap
from dataclasses import (
dataclass,
field,
)


try:
from typing import Literal
except ImportError:
from typing_extensions import Literal

from typing import (
Dict,
Iterable,
Expand Down Expand Up @@ -250,6 +256,7 @@ class OutputTemplate:
services: List["ServiceCompiler"] = field(default_factory=list)
imports_type_checking_only: Set[str] = field(default_factory=set)
pydantic_dataclasses: bool = False
use_optionals: Optional[Literal["all"]] = None
output: bool = True

@property
Expand Down Expand Up @@ -483,7 +490,7 @@ def repeated(self) -> bool:

@property
def optional(self) -> bool:
return self.proto_obj.proto3_optional
return self.proto_obj.proto3_optional or self.output_file.use_optionals == "all"

@property
def mutable(self) -> bool:
Expand Down
3 changes: 3 additions & 0 deletions src/betterproto/plugin/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse:
output_package_name
].pydantic_dataclasses = True

if "useOptionals=all" in plugin_options:
request_data.output_packages[output_package_name].use_optionals = "all"

# Read Messages and Enums
# We need to read Messages before Services in so that we can
# get the references to input/output messages for each service
Expand Down

0 comments on commit 48ea4ea

Please sign in to comment.