Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1cbae6a
fix: add oneof fields to genereated protoplus init
crwilcox Jun 25, 2020
f104ba0
Add a one of field to the Field dataclass
crwilcox Jun 25, 2020
1f8ee9c
style
crwilcox Jun 26, 2020
7438862
style
crwilcox Jun 26, 2020
593f647
str type for oneof
crwilcox Jun 26, 2020
114a277
add field oneof to ads templates
crwilcox Jun 26, 2020
589e914
mypy: specify oneof as optional
crwilcox Jun 26, 2020
c912534
add oneofs to schema
crwilcox Jun 30, 2020
afa3578
exit oops
crwilcox Jun 30, 2020
9a5e6c5
mypy and lint fixes
crwilcox Jun 30, 2020
086cd70
Merge branch 'master' into oneof-proto-templates
crwilcox Jun 30, 2020
cf31e2a
add tests for oneofs
crwilcox Jun 30, 2020
3b90426
lint
crwilcox Jun 30, 2020
70cfe15
typing and test
crwilcox Jun 30, 2020
ca19eee
Merge branch 'master' into oneof-proto-templates
crwilcox Jun 30, 2020
58d3f97
style
crwilcox Jun 30, 2020
b44317f
whitespace
crwilcox Jun 30, 2020
df33ed9
Merge remote-tracking branch 'upstream' into oneof-proto-templates
crwilcox Jun 30, 2020
c4d1b4e
Merge branch 'master' into oneof-proto-templates
crwilcox Jul 6, 2020
374bfbe
Add an nth utility
software-dov Jul 6, 2020
a329bd0
Tweak api.py to use nth
software-dov Jul 6, 2020
cbf73a3
Templates change for oneofs (NOT ADS TEMPLATES)
software-dov Jul 6, 2020
2fc85fa
Merge branch 'oneof-proto-templates' of https://github.com/googleapis…
software-dov Jul 6, 2020
5f861d3
Test nth
software-dov Jul 7, 2020
64ec32b
Style check
software-dov Jul 7, 2020
07709f8
Tweak
software-dov Jul 7, 2020
aa7ceb6
Thing
software-dov Jul 7, 2020
ec65b73
Thing in thing exception
software-dov Jul 7, 2020
05b05d5
Whitespace
software-dov Jul 7, 2020
e590edb
Fighting the type system
software-dov Jul 7, 2020
3def32f
Merge branch 'master' into oneof-proto-templates
software-dov Jul 7, 2020
8dbaa75
Merge branch 'master' into oneof-proto-templates
software-dov Jul 7, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class {{ message.name }}({{ p }}.Message):
{% else -%}
{{ field.name }} = {{ p }}.{% if field.repeated %}Repeated{% endif %}Field(
{{- p }}.{{ field.proto_type }}, number={{ field.number }}
{% if field.oneof %}, oneof='{{ field.oneof }}'{% endif %}
{%- if field.enum or field.message %},
{{ field.proto_type.lower() }}={{ field.type.ident.rel(message.ident) }},
{% endif %})
Expand Down
47 changes: 44 additions & 3 deletions gapic/schema/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,14 +556,43 @@ def _load_children(self,
answer[wrapped.name] = wrapped
return answer

def _get_oneofs(self,
oneof_pbs: Sequence[descriptor_pb2.OneofDescriptorProto],
address: metadata.Address, path: Tuple[int, ...],
) -> Dict[str, wrappers.Oneof]:
"""Return a dictionary of wrapped oneofs for the given message.

Args:
oneof_fields (Sequence[~.descriptor_pb2.OneofDescriptorProto]): A
sequence of protobuf field objects.
address (~.metadata.Address): An address object denoting the
location of these oneofs.
path (Tuple[int]): The source location path thus far, as
understood by ``SourceCodeInfo.Location``.

Returns:
Mapping[str, ~.wrappers.Oneof]: A ordered mapping of
:class:`~.wrappers.Oneof` objects.
"""
# Iterate over the oneofs and collect them into a dictionary.
answer: Dict[str, wrappers.Oneof] = collections.OrderedDict()
for oneof_pb, i in zip(oneof_pbs, range(0, sys.maxsize)):
Comment thread
crwilcox marked this conversation as resolved.
Outdated
answer[oneof_pb.name] = wrappers.Oneof(
oneof_pb=oneof_pb,
)

# Done; return the answer.
return answer

def _get_fields(self,
field_pbs: Sequence[descriptor_pb2.FieldDescriptorProto],
address: metadata.Address, path: Tuple[int, ...],
oneofs = None
) -> Dict[str, wrappers.Field]:
"""Return a dictionary of wrapped fields for the given message.

Args:
fields (Sequence[~.descriptor_pb2.FieldDescriptorProto]): A
field_pbs (Sequence[~.descriptor_pb2.FieldDescriptorProto]): A
sequence of protobuf field objects.
address (~.metadata.Address): An address object denoting the
location of these fields.
Expand All @@ -586,6 +615,10 @@ def _get_fields(self,
# `_load_message` method.
answer: Dict[str, wrappers.Field] = collections.OrderedDict()
for field_pb, i in zip(field_pbs, range(0, sys.maxsize)):
oneof_name = None
if getattr(field_pb, 'oneof_index', -1) >= 0 and oneofs:
Comment thread
crwilcox marked this conversation as resolved.
Outdated
oneof_name = list(oneofs.keys())[field_pb.oneof_index]

answer[field_pb.name] = wrappers.Field(
field_pb=field_pb,
enum=self.api_enums.get(field_pb.type_name.lstrip('.')),
Expand All @@ -594,6 +627,7 @@ def _get_fields(self,
address=address.child(field_pb.name, path + (i,)),
documentation=self.docs.get(path + (i,), self.EMPTY),
),
oneof=oneof_name,
)

# Done; return the answer.
Expand Down Expand Up @@ -779,19 +813,25 @@ def _load_message(self,
loader=self._load_message,
path=path + (3,),
)
# self._load_children(message.oneof_decl, loader=self._load_field,
# address=nested_addr, info=info.get(8, {}))

oneofs = self._get_oneofs(
message_pb.oneof_decl,
address=address,
path=path + (8,),
)

# Create a dictionary of all the fields for this message.
fields = self._get_fields(
message_pb.field,
address=address,
path=path + (2,),
oneofs=oneofs,
)
fields.update(self._get_fields(
message_pb.extension,
address=address,
path=path + (6,),
oneofs=oneofs,
))

# Create a message correspoding to this descriptor.
Expand All @@ -804,6 +844,7 @@ def _load_message(self,
address=address,
documentation=self.docs.get(path, self.EMPTY),
),
oneofs=oneofs,
Comment thread
crwilcox marked this conversation as resolved.
)
return self.proto_messages[address.proto]

Expand Down
11 changes: 11 additions & 0 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Field:
meta: metadata.Metadata = dataclasses.field(
default_factory=metadata.Metadata,
)
oneof: Optional[str] = None

def __getattr__(self, name):
return getattr(self.field_pb, name)
Expand Down Expand Up @@ -206,6 +207,15 @@ def with_context(self, *, collisions: FrozenSet[str]) -> 'Field':
)


@dataclasses.dataclass(frozen=True)
class Oneof:
"""Description of a field."""
oneof_pb: descriptor_pb2.OneofDescriptorProto

def __getattr__(self, name):
return getattr(self.oneof_pb, name)


@dataclasses.dataclass(frozen=True)
class MessageType:
"""Description of a message (defined with the ``message`` keyword)."""
Expand All @@ -220,6 +230,7 @@ class MessageType:
meta: metadata.Metadata = dataclasses.field(
default_factory=metadata.Metadata,
)
oneofs: Optional[List[str]] = None

def __getattr__(self, name):
return getattr(self.message_pb, name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class {{ message.name }}({{ p }}.Message):
{% else -%}
{{ field.name }} = {{ p }}.{% if field.repeated %}Repeated{% endif %}Field(
{{- p }}.{{ field.proto_type }}, number={{ field.number }}
{% if field.oneof %}, oneof='{{ field.oneof }}'{% endif %}
{%- if field.enum or field.message %},
{{ field.proto_type.lower() }}={{ field.type.ident.rel(message.ident) }},
{% endif %})
Expand Down