Skip to content

Commit adba9fd

Browse files
authored
chore(generator): use {% set %} to extract repeated logic in conditionals (#17890)
Updating the use of global variable for auto populated fields, we are making sure that the Jinja variables are defined at the top of the file so they can easily reference throughout the template.
1 parent e613d1c commit adba9fd

5 files changed

Lines changed: 22 additions & 18 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{% extends '_base.py.j2' %}
2-
32
{% block content %}
3+
{% set has_auto_populated_fields = api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
4+
45
{% import "%namespace/%name/%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
56

67
from collections import OrderedDict
78
import os
89
import re
910
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 %}
11+
{% if has_auto_populated_fields %}
1112
import uuid
1213
{% endif %}
1314
{% if service.any_deprecated %}

packages/gapic-generator/gapic/ads-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,10 +1,12 @@
11
{% extends "_base.py.j2" %}
2-
32
{% block content %}
3+
{% set has_auto_populated_fields = api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
4+
5+
46
{% import "%namespace/%name/%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
57

68
import os
7-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
9+
{% if has_auto_populated_fields %}
810
import re
911
{% endif %}
1012
from unittest import mock
@@ -70,7 +72,7 @@ from google.iam.v1 import policy_pb2 # type: ignore
7072
{% endfilter %}
7173
{{ shared_macros.add_google_api_core_version_header_import(service.version) }}
7274

73-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %}
75+
{% if has_auto_populated_fields %}
7476
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
7577
{% endif %}
7678

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{% extends "_base.py.j2" %}
2-
32
{% block content %}
3+
{% set has_auto_populated_fields = api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
44
{% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %}
55
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
66

77
import logging as std_logging
88
from collections import OrderedDict
99
import re
1010
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 %}
11+
{% if has_auto_populated_fields %}
1212
import uuid
1313
{% endif %}
1414
{% if service.any_deprecated %}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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 = api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
66
{% import "%namespace/%name_%version/%sub/services/%service/_client_macros.j2" as macros %}
77
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
88

@@ -16,7 +16,7 @@ import logging as std_logging
1616
import os
1717
import re
1818
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 %}
19+
{% if has_auto_populated_fields %}
2020
import uuid
2121
{% endif %}
2222
import warnings
@@ -453,7 +453,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
453453
# NOTE (b/349488459): universe validation is disabled until further notice.
454454
return True
455455

456-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
456+
{% if has_auto_populated_fields %}
457+
457458
@staticmethod
458459
def _setup_request_id(request, field_name: str, is_proto3_optional: bool):
459460
"""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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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 = api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
66
{% import "tests/unit/gapic/%name_%version/%sub/test_macros.j2" as test_macros %}
77
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
88

99
import os
1010
import asyncio
11-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
11+
{% if has_auto_populated_fields %}
1212
import re
1313
{% endif %}
1414
from unittest import mock
@@ -107,7 +107,7 @@ CRED_INFO_JSON = {
107107
"principal": "service-account@example.com",
108108
}
109109
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
110-
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
110+
{% if has_auto_populated_fields %}
111111
_UUID4_RE = re.compile(r"{{ uuid4_re }}")
112112
{% endif %}
113113

@@ -434,7 +434,7 @@ def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
434434
client._add_cred_info_for_auth_errors(error)
435435
assert error.details == []
436436

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

0 commit comments

Comments
 (0)