Skip to content

Commit 2360399

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

5 files changed

Lines changed: 52 additions & 17 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
{% extends '_base.py.j2' %}
2-
32
{% block content %}
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 %}
10+
411
{% import "%namespace/%name/%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
512

613
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
{% extends "_base.py.j2" %}
2-
32
{% block content %}
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 %}
10+
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
{% extends "_base.py.j2" %}
2-
32
{% block content %}
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 %}
10+
411
{% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %}
512
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
613

714
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
{% extends '_base.py.j2' %}
2+
{% block content %}
13
{# 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. #}
24
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
3-
{% extends '_base.py.j2' %}
5+
{% set has_auto_populated_fields = namespace(value=false) %}
6+
{% for method in service.methods.values() %}
7+
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
8+
{% if method_settings and method_settings.auto_populated_fields %}
9+
{% set has_auto_populated_fields.value = true %}
10+
{% endif %}
11+
{% endfor %}
412

5-
{% block content %}
613
{% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %}
714
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
815

@@ -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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
{# 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. #}
2-
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
31
{% extends "_base.py.j2" %}
4-
52
{% block content %}
3+
{# 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. #}
4+
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
5+
{% set has_auto_populated_fields = namespace(value=false) %}
6+
{% for method in service.methods.values() %}
7+
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
8+
{% if method_settings and method_settings.auto_populated_fields %}
9+
{% set has_auto_populated_fields.value = true %}
10+
{% endif %}
11+
{% endfor %}
612
{% import "tests/unit/gapic/%name_%version/%sub/test_macros.j2" as test_macros %}
713
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
814

915
import os
1016
import asyncio
11-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
17+
{% if has_auto_populated_fields.value %}
1218
import re
1319
{% endif %}
1420
from unittest import mock
@@ -107,7 +113,7 @@ CRED_INFO_JSON = {
107113
"principal": "service-account@example.com",
108114
}
109115
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
110-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
116+
{% if has_auto_populated_fields.value %}
111117
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
112118
{% endif %}
113119

@@ -434,7 +440,7 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
434440
client._add_cred_info_for_auth_errors(error)
435441
assert error.details == []
436442

437-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
443+
{% if has_auto_populated_fields.value %}
438444
def test__setup_request_id():
439445
class MockRequest:
440446
def __init__(self, **kwargs):

0 commit comments

Comments
 (0)