Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: collisions don't contain reserved names by default #684

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 2 additions & 5 deletions gapic/schema/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class Address:
)
collisions: FrozenSet[str] = dataclasses.field(default_factory=frozenset)

def __post_init__(self):
super().__setattr__("collisions", self.collisions | RESERVED_NAMES)

def __eq__(self, other) -> bool:
return all([getattr(self, i) == getattr(other, i) for i
in ('name', 'module', 'module_path', 'package', 'parent')])
Expand Down Expand Up @@ -114,7 +111,7 @@ def module_alias(self) -> str:
while still providing names that are fundamentally readable
to users (albeit looking auto-generated).
"""
if self.module in self.collisions:
if self.module in self.collisions | RESERVED_NAMES:
return '_'.join(
(
''.join(
Expand Down Expand Up @@ -283,7 +280,7 @@ def with_context(self, *, collisions: FrozenSet[str]) -> 'Address':
``Address`` object aliases module names to avoid naming collisions in
the file being written.
"""
return dataclasses.replace(self, collisions=frozenset(collisions))
return dataclasses.replace(self, collisions=collisions)


@dataclasses.dataclass(frozen=True)
Expand Down
14 changes: 10 additions & 4 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,19 @@ def recursive_field_types(self) -> Sequence[
return tuple(types)

@utils.cached_property
def recursive_fields(self) -> FrozenSet[Field]:
return frozenset(chain(
def recursive_resource_fields(self) -> FrozenSet[Field]:
all_fields = chain(
self.fields.values(),
(field
for t in self.recursive_field_types if isinstance(t, MessageType)
for field in t.fields.values()),
))
)
return frozenset(
f
for f in all_fields
if (f.options.Extensions[resource_pb2.resource_reference].type or
f.options.Extensions[resource_pb2.resource_reference].child_type)
)

@property
def map(self) -> bool:
Expand Down Expand Up @@ -1060,7 +1066,7 @@ def gen_resources(message):
yield type_

def gen_indirect_resources_used(message):
for field in message.recursive_fields:
for field in message.recursive_resource_fields:
resource = field.options.Extensions[
resource_pb2.resource_reference]
resource_type = resource.type or resource.child_type
Expand Down