Skip to content

Commit f7a31e6

Browse files
committed
chore(generator): use global var for autopopulated fields
1 parent dd30194 commit f7a31e6

5 files changed

Lines changed: 40 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
{% set has_auto_populated_fields = namespace(value=false) %}
2+
{% for method in service.methods.values() %}
3+
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
4+
{% if method_settings and method_settings.auto_populated_fields %}
5+
{% set has_auto_populated_fields.value = true %}
6+
{% endif %}
7+
{% endfor %}
18
{% extends '_base.py.j2' %}
29

310
{% block content %}
@@ -7,7 +14,7 @@ from collections import OrderedDict
714
import os
815
import re
916
from typing import Callable, Dict, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union, cast
10-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
17+
{% if has_auto_populated_fields.value %}
1118
import uuid
1219
{% endif %}
1320
{% if service.any_deprecated %}

packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
{% set has_auto_populated_fields = namespace(value=false) %}
2+
{% for method in service.methods.values() %}
3+
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
4+
{% if method_settings and method_settings.auto_populated_fields %}
5+
{% set has_auto_populated_fields.value = true %}
6+
{% endif %}
7+
{% endfor %}
18
{% extends "_base.py.j2" %}
29

310
{% block content %}
411
{% import "%namespace/%name/%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
512

613
import os
7-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
14+
{% if has_auto_populated_fields.value %}
815
import re
916
{% endif %}
1017
from unittest import mock
@@ -70,7 +77,7 @@ from google.iam.v1 import policy_pb2 # type: ignore
7077
{% endfilter %}
7178
{{ shared_macros.add_google_api_core_version_header_import(service.version) }}
7279

73-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %}
80+
{% if has_auto_populated_fields.value %}
7481
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
7582
{% endif %}
7683

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
{% set has_auto_populated_fields = namespace(value=false) %}
2+
{% for method in service.methods.values() %}
3+
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
4+
{% if method_settings and method_settings.auto_populated_fields %}
5+
{% set has_auto_populated_fields.value = true %}
6+
{% endif %}
7+
{% endfor %}
18
{% extends "_base.py.j2" %}
29

310
{% block content %}
@@ -8,7 +15,7 @@ import logging as std_logging
815
from collections import OrderedDict
916
import re
1017
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}AsyncIterable, Awaitable, {% endif %}{% if service.any_client_streaming %}AsyncIterator, {% endif %}Sequence, Tuple, Type, Union
11-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
18+
{% if has_auto_populated_fields.value %}
1219
import uuid
1320
{% endif %}
1421
{% if service.any_deprecated %}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
22
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
3+
{% set has_auto_populated_fields = namespace(value=false) %}
4+
{% for method in service.methods.values() %}
5+
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
6+
{% if method_settings and method_settings.auto_populated_fields %}
7+
{% set has_auto_populated_fields.value = true %}
8+
{% endif %}
9+
{% endfor %}
310
{% extends '_base.py.j2' %}
411

512
{% block content %}
@@ -16,7 +23,7 @@ import logging as std_logging
1623
import os
1724
import re
1825
from typing import Dict, Callable, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union, cast
19-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
26+
{% if has_auto_populated_fields.value %}
2027
import uuid
2128
{% endif %}
2229
import warnings
@@ -453,7 +460,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
453460
# NOTE (b/349488459): universe validation is disabled until further notice.
454461
return True
455462

456-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
463+
{% if has_auto_populated_fields.value %}
464+
457465
@staticmethod
458466
def _setup_request_id(request, field_name: str, is_proto3_optional: bool):
459467
"""Populate a UUID4 field in the request if it is not already set.

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
22
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
3+
{%- set method_settings = api.all_method_settings.get(method.meta.address.proto) -%}
4+
{%- set has_auto_populated_fields = method_settings.auto_populated_fields if method_settings else [] -%}
35
{% extends "_base.py.j2" %}
46

57
{% block content %}
@@ -8,7 +10,7 @@
810

911
import os
1012
import asyncio
11-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
13+
{% if auto_populated_fields %}
1214
import re
1315
{% endif %}
1416
from unittest import mock
@@ -107,7 +109,7 @@ CRED_INFO_JSON = {
107109
"principal": "service-account@example.com",
108110
}
109111
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
110-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
112+
{% if auto_populated_fields %}
111113
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
112114
{% endif %}
113115

@@ -434,7 +436,7 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
434436
client._add_cred_info_for_auth_errors(error)
435437
assert error.details == []
436438

437-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
439+
{% if auto_populated_fields %}
438440
def test__setup_request_id():
439441
class MockRequest:
440442
def __init__(self, **kwargs):

0 commit comments

Comments
 (0)