Skip to content

Commit 8ddb399

Browse files
committed
read config changes solution
1 parent 80d0dd9 commit 8ddb399

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
from gapic.utils import cached_proto_context
7171
from gapic.utils import uri_sample
7272
from gapic.utils import make_private
73+
from gapic.utils import Options
7374

7475

7576
@dataclasses.dataclass(frozen=True)
@@ -688,7 +689,16 @@ def resource_path(self) -> Optional[str]:
688689
@property
689690
def resource_type(self) -> Optional[str]:
690691
resource = self.options.Extensions[resource_pb2.resource]
691-
return resource.type[resource.type.find("/") + 1 :] if resource else None
692+
if not resource:
693+
return None
694+
695+
# Extract the standard short name (e.g., "Tool" from "ces.googleapis.com/Tool")
696+
default_type = resource.type[resource.type.find("/") + 1 :]
697+
698+
# Check if the CLI provided an alias for this specific resource path
699+
aliases = getattr(Options, "resource_name_aliases_global", {})
700+
701+
return aliases.get(resource.type, default_type)
692702

693703
@property
694704
def resource_type_full_path(self) -> Optional[str]:

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ class {{ service.async_client_name }}:
7676
_DEFAULT_ENDPOINT_TEMPLATE = {{ service.client_name }}._DEFAULT_ENDPOINT_TEMPLATE
7777
_DEFAULT_UNIVERSE = {{ service.client_name }}._DEFAULT_UNIVERSE
7878

79-
{% for message in service.resource_messages|sort(attribute="resource_type") %}
79+
{% for message in service.resource_messages %}
8080
{{ message.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.{{ message.resource_type|snake_case }}_path)
8181
parse_{{ message.resource_type|snake_case}}_path = staticmethod({{ service.client_name }}.parse_{{ message.resource_type|snake_case }}_path)
8282
{% endfor %}
83-
{% for resource_msg in service.common_resources.values()|sort(attribute="type_name") %}
83+
{% for resource_msg in service.common_resources.values() %}
8484
common_{{ resource_msg.message_type.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.common_{{ resource_msg.message_type.resource_type|snake_case }}_path)
8585
parse_common_{{ resource_msg.message_type.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.parse_common_{{ resource_msg.message_type.resource_type|snake_case }}_path)
8686
{% endfor %}

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
262262
return self._transport
263263

264264

265-
{% for message in service.resource_messages|sort(attribute="resource_type") %}
265+
{% for message in service.resource_messages %}
266266
@staticmethod
267267
def {{ message.resource_type|snake_case }}_path({% for arg in message.resource_path_args %}{{ arg }}: str,{% endfor %}) -> str:
268268
"""Returns a fully-qualified {{ message.resource_type|snake_case }} string."""

packages/gapic-generator/gapic/utils/options.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Options:
5151
rest_numeric_enums: bool = False
5252
proto_plus_deps: Tuple[str, ...] = dataclasses.field(default=("",))
5353
gapic_version: str = "0.0.0"
54+
resource_name_aliases: Dict[str, str] = dataclasses.field(default_factory=dict)
5455

5556
# Class constants
5657
PYTHON_GAPIC_PREFIX: str = "python-gapic-"
@@ -73,6 +74,7 @@ class Options:
7374
# For example, 'google.cloud.api.v1+google.cloud.anotherapi.v2'
7475
"proto-plus-deps",
7576
"gapic-version", # A version string following https://peps.python.org/pep-0440
77+
"resource-name-aliases",
7678
)
7779
)
7880

@@ -187,6 +189,18 @@ def tweak_path(p):
187189
proto_plus_deps = tuple(opts.pop("proto-plus-deps", ""))
188190
if len(proto_plus_deps):
189191
proto_plus_deps = tuple(proto_plus_deps[0].split("+"))
192+
193+
resource_name_aliases = {}
194+
resource_alias_strings = opts.pop("resource-name-aliases", None)
195+
if resource_alias_strings:
196+
try:
197+
# Strip single quotes that survive the Bazel/Shell transition
198+
raw_string = resource_alias_strings[-1].strip("'")
199+
resource_name_aliases = json.loads(raw_string)
200+
except json.JSONDecodeError as e:
201+
warnings.warn(f"Failed to parse resource-name-aliases JSON: {e} from string: {resource_alias_strings[-1]}")
202+
203+
Options.resource_name_aliases_global = resource_name_aliases
190204

191205
answer = Options(
192206
name=opts.pop("name", [""]).pop(),
@@ -210,6 +224,7 @@ def tweak_path(p):
210224
rest_numeric_enums=rest_numeric_enums,
211225
proto_plus_deps=proto_plus_deps,
212226
gapic_version=opts.pop("gapic-version", ["0.0.0"]).pop(),
227+
resource_name_aliases=resource_name_aliases,
213228
)
214229

215230
# Note: if we ever need to recursively check directories for sample

0 commit comments

Comments
 (0)