Skip to content

Commit 5cdf67e

Browse files
committed
fix: restore messages not attached to rpc in selective_gapic_generation
1 parent 08c24c8 commit 5cdf67e

2 files changed

Lines changed: 43 additions & 452 deletions

File tree

packages/gapic-generator/gapic/schema/api.py

Lines changed: 17 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -258,112 +258,21 @@ def disambiguate(self, string: str) -> str:
258258
return self.disambiguate(f"_{string}")
259259
return string
260260

261-
def add_to_address_allowlist(
261+
def with_selective_generation(
262262
self,
263263
*,
264-
address_allowlist: Set["metadata.Address"],
265-
method_allowlist: Set[str],
266-
resource_messages: Dict[str, "wrappers.MessageType"],
267-
) -> None:
268-
"""Adds to the set of Addresses of wrapper objects to be included in selective GAPIC generation.
269-
270-
This method is used to create an allowlist of addresses to be used to filter out unneeded
271-
services, methods, messages, and enums at a later step.
272-
273-
Args:
274-
address_allowlist (Set[metadata.Address]): A set of allowlisted metadata.Address
275-
objects to add to. Only the addresses of the allowlisted methods, the services
276-
containing these methods, and messages/enums those methods use will be part of the
277-
final address_allowlist. The set may be modified during this call.
278-
method_allowlist (Set[str]): An allowlist of fully-qualified method names.
279-
resource_messages (Dict[str, wrappers.MessageType]): A dictionary mapping the unified
280-
resource type name of a resource message to the corresponding MessageType object
281-
representing that resource message. Only resources with a message representation
282-
should be included in the dictionary.
283-
Returns:
284-
None
285-
"""
286-
# The method.operation_service for an extended LRO is not fully qualified, so we
287-
# truncate the service names accordingly so they can be found in
288-
# method.add_to_address_allowlist
289-
services_in_proto = {
290-
service.name: service for service in self.services.values()
291-
}
292-
for service in self.services.values():
293-
service.add_to_address_allowlist(
294-
address_allowlist=address_allowlist,
295-
method_allowlist=method_allowlist,
296-
resource_messages=resource_messages,
297-
services_in_proto=services_in_proto,
298-
)
299-
300-
def prune_messages_for_selective_generation(
301-
self, *, address_allowlist: Set["metadata.Address"]
302-
) -> Optional["Proto"]:
303-
"""Returns a truncated version of this Proto.
304-
305-
Only the services, messages, and enums contained in the allowlist
306-
of visited addresses are included in the returned object. If there
307-
are no services, messages, or enums left, and no file level resources,
308-
return None.
309-
310-
Args:
311-
address_allowlist (Set[metadata.Address]): A set of allowlisted metadata.Address
312-
objects to filter against. Objects with addresses not the allowlist will be
313-
removed from the returned Proto.
314-
Returns:
315-
Optional[Proto]: A truncated version of this proto. If there are no services, messages,
316-
or enums left after the truncation process and there are no file level resources,
317-
returns None.
318-
"""
319-
# Once the address allowlist has been created, it suffices to only
320-
# prune items at 2 different levels to truncate the Proto object:
321-
#
322-
# 1. At the Proto level, we remove unnecessary services, messages,
323-
# and enums.
324-
# 2. For allowlisted services, at the Service level, we remove
325-
# non-allowlisted methods.
326-
services = {
327-
k: v.prune_messages_for_selective_generation(
328-
address_allowlist=address_allowlist
329-
)
330-
for k, v in self.services.items()
331-
if v.meta.address in address_allowlist
332-
}
333-
334-
all_messages = {
335-
k: v for k, v in self.all_messages.items() if v.ident in address_allowlist
336-
}
337-
338-
all_enums = {
339-
k: v for k, v in self.all_enums.items() if v.ident in address_allowlist
340-
}
341-
342-
if not services and not all_messages and not all_enums:
343-
return None
344-
345-
return dataclasses.replace(
346-
self, services=services, all_messages=all_messages, all_enums=all_enums
347-
)
348-
349-
def with_internal_methods(self, *, public_methods: Set[str]) -> "Proto":
350-
"""Returns a version of this Proto with some Methods marked as internal.
264+
generate_omitted_as_internal: bool,
265+
public_methods: Set[str],
266+
) -> "Proto":
351267

352-
The methods not in the public_methods set will be marked as internal and
353-
services containing these methods will also be marked as internal by extension.
354-
(See :meth:`Service.is_internal` for more details).
268+
services = {}
269+
for k, v in self.services.items():
270+
new_v = v.with_selective_generation(
271+
generate_omitted_as_internal=generate_omitted_as_internal,
272+
public_methods=public_methods)
273+
if new_v:
274+
services[k] = new_v
355275

356-
Args:
357-
public_methods (Set[str]): An allowlist of fully-qualified method names.
358-
Methods not in this allowlist will be marked as internal.
359-
Returns:
360-
Proto: A version of this Proto with Method objects corresponding to methods
361-
not in `public_methods` marked as internal.
362-
"""
363-
services = {
364-
k: v.with_internal_methods(public_methods=public_methods)
365-
for k, v in self.services.items()
366-
}
367276
return dataclasses.replace(self, services=services)
368277

369278

@@ -529,37 +438,13 @@ def disambiguate_keyword_sanitize_fname(
529438
k: v for k, v in api.all_protos.items() if k not in api.protos
530439
}
531440

532-
if selective_gapic_settings.generate_omitted_as_internal:
533-
for name, proto in api.protos.items():
534-
new_all_protos[name] = proto.with_internal_methods(
535-
public_methods=selective_gapic_methods
536-
)
537-
else:
538-
all_resource_messages = collections.ChainMap(
539-
*(proto.resource_messages for proto in protos.values())
441+
for name, proto in api.protos.items():
442+
proto_to_generate = proto.with_selective_generation(
443+
generate_omitted_as_internal=selective_gapic_settings.generate_omitted_as_internal,
444+
public_methods=selective_gapic_methods,
540445
)
541-
542-
# Prepare a list of addresses to include in selective generation,
543-
# then prune each Proto object. We look at metadata.Addresses, not objects, because
544-
# objects that refer to the same thing in the proto are different Python objects
545-
# in memory.
546-
address_allowlist: Set["metadata.Address"] = set([])
547-
for proto in api.protos.values():
548-
proto.add_to_address_allowlist(
549-
address_allowlist=address_allowlist,
550-
method_allowlist=selective_gapic_methods,
551-
resource_messages=all_resource_messages,
552-
)
553-
554-
# We only prune services/messages/enums from protos that are not dependencies.
555-
for name, proto in api.protos.items():
556-
proto_to_generate = (
557-
proto.prune_messages_for_selective_generation(
558-
address_allowlist=address_allowlist
559-
)
560-
)
561-
if proto_to_generate:
562-
new_all_protos[name] = proto_to_generate
446+
if proto_to_generate:
447+
new_all_protos[name] = proto_to_generate
563448

564449
api = cls(
565450
naming=naming,

0 commit comments

Comments
 (0)