Skip to content

Commit 4699c98

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

5 files changed

Lines changed: 44 additions & 10 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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{% extends "_base.py.j2" %}
22

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+
311
{% block content %}
412
{% import "%namespace/%name/%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
513

614
import os
7-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
15+
{% if has_auto_populated_fields.value %}
816
import re
917
{% endif %}
1018
from unittest import mock
@@ -70,7 +78,7 @@ from google.iam.v1 import policy_pb2 # type: ignore
7078
{% endfilter %}
7179
{{ shared_macros.add_google_api_core_version_header_import(service.version) }}
7280

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

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{% extends "_base.py.j2" %}
22

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+
311
{% block content %}
412
{% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %}
513
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
@@ -8,7 +16,7 @@ import logging as std_logging
816
from collections import OrderedDict
917
import re
1018
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 %}
19+
{% if has_auto_populated_fields.value %}
1220
import uuid
1321
{% endif %}
1422
{% 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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
{% extends "_base.py.j2" %}
2+
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 method_settings = api.all_method_settings.get(method.meta.address.proto) -%}
6+
{%- set has_auto_populated_fields = method_settings.auto_populated_fields if method_settings else [] -%}
47

58
{% block content %}
69
{% import "tests/unit/gapic/%name_%version/%sub/test_macros.j2" as test_macros %}
710
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
811

912
import os
1013
import asyncio
11-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
14+
{% if auto_populated_fields %}
1215
import re
1316
{% endif %}
1417
from unittest import mock
@@ -107,7 +110,7 @@ CRED_INFO_JSON = {
107110
"principal": "service-account@example.com",
108111
}
109112
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
110-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
113+
{% if auto_populated_fields %}
111114
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
112115
{% endif %}
113116

@@ -434,7 +437,7 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
434437
client._add_cred_info_for_auth_errors(error)
435438
assert error.details == []
436439

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

0 commit comments

Comments
 (0)