Skip to content

Commit 16f3b08

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

4 files changed

Lines changed: 36 additions & 7 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/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: 10 additions & 3 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 %}
@@ -8,7 +15,7 @@
815

916
import os
1017
import asyncio
11-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
18+
{% if has_auto_populated_fields.value %}
1219
import re
1320
{% endif %}
1421
from unittest import mock
@@ -107,7 +114,7 @@ CRED_INFO_JSON = {
107114
"principal": "service-account@example.com",
108115
}
109116
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
110-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
117+
{% if has_auto_populated_fields.value %}
111118
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
112119
{% endif %}
113120

@@ -434,7 +441,7 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
434441
client._add_cred_info_for_auth_errors(error)
435442
assert error.details == []
436443

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

0 commit comments

Comments
 (0)