-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add --resource-name-alias flag to resolve namespace collisions
#16769
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
21db7b3
71f7109
f148bd9
9e82c9a
fe91848
783cd7c
fdfbced
aafe845
2d415d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ class Options: | |
| rest_numeric_enums: bool = False | ||
| proto_plus_deps: Tuple[str, ...] = dataclasses.field(default=("",)) | ||
| gapic_version: str = "0.0.0" | ||
| resource_name_aliases: Dict[str, str] = dataclasses.field(default_factory=dict) | ||
|
|
||
| # Class constants | ||
| PYTHON_GAPIC_PREFIX: str = "python-gapic-" | ||
|
|
@@ -72,7 +73,11 @@ class Options: | |
| # proto plus dependencies delineated by '+' | ||
| # For example, 'google.cloud.api.v1+google.cloud.anotherapi.v2' | ||
| "proto-plus-deps", | ||
| "gapic-version", # A version string following https://peps.python.org/pep-0440 | ||
| "gapic-version", # A version string following https://peps.python.org/pep-0440, | ||
| # Resolves method name collisions by mapping a fully qualified | ||
| # resource path to a custom TitleCase alias. | ||
| # Format: resource.path/Name:AliasName | ||
| "resource-name-alias", | ||
| ) | ||
| ) | ||
|
|
||
|
|
@@ -188,6 +193,37 @@ def tweak_path(p): | |
| if len(proto_plus_deps): | ||
| proto_plus_deps = tuple(proto_plus_deps[0].split("+")) | ||
|
|
||
| # Parse the resource name aliases dictionary (Format: "path/to/Resource:AliasName") | ||
| resource_name_aliases = {} | ||
| raw_aliases = opts.pop("resource-name-alias", []) | ||
|
|
||
| # Normalize: protoc can return a string (1 flag) or list (multiple flags) | ||
| if not isinstance(raw_aliases, list): | ||
| raw_aliases = [raw_aliases] | ||
|
|
||
| # Parse explicitly and safely | ||
| for mapping in raw_aliases: | ||
| if not mapping or not mapping.strip(): | ||
| continue | ||
|
|
||
| try: | ||
| # split(":", 1) ensures we only split on the FIRST colon | ||
| res_path, alias_name = mapping.split(":", 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of References
|
||
|
|
||
| clean_path = res_path.strip() | ||
| clean_alias = alias_name.strip() | ||
|
|
||
| if not clean_path or not clean_alias: | ||
| raise ValueError() | ||
|
|
||
| resource_name_aliases[clean_path] = clean_alias | ||
|
|
||
| except ValueError: | ||
| warnings.warn( | ||
| f"Ignored malformed resource-name-alias: '{mapping}'. " | ||
| "Expected format is 'resource.path/Name:AliasName'." | ||
| ) | ||
|
|
||
| answer = Options( | ||
| name=opts.pop("name", [""]).pop(), | ||
| namespace=tuple(opts.pop("namespace", [])), | ||
|
|
@@ -210,6 +246,7 @@ def tweak_path(p): | |
| rest_numeric_enums=rest_numeric_enums, | ||
| proto_plus_deps=proto_plus_deps, | ||
| gapic_version=opts.pop("gapic-version", ["0.0.0"]).pop(), | ||
| resource_name_aliases=resource_name_aliases, | ||
| ) | ||
|
|
||
| # Note: if we ever need to recursively check directories for sample | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.