-
Notifications
You must be signed in to change notification settings - Fork 70
feat: add support for reading google.api.api_version #1999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 36 commits
4e3138c
009fc71
b574ed1
dd96aa4
de4ba22
28e6991
fca5b55
8cd648a
35755b7
ea5666f
79f1767
7eb5c92
c0b7810
d52e78e
5129f61
ab18eea
6946304
6dfa53c
2e666ea
1e6e96e
6416902
68b4010
178c964
a5d6109
109edd4
12ff101
ca95a6b
0e669ab
7957ded
542e4e1
0f68169
921f20b
16402e1
555cff7
f150e67
220f873
a62b313
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{# | ||
# Copyright (C) 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#} | ||
|
||
{% macro auto_populate_uuid4_fields(api, method) %} | ||
{# | ||
Automatically populate UUID4 fields according to | ||
https://google.aip.dev/client-libraries/4235 when the | ||
field satisfies either of: | ||
- The field supports explicit presence and has not been set by the user. | ||
- The field doesn't support explicit presence, and its value is the empty | ||
string (i.e. the default value). | ||
When using this macro, ensure the calling template generates a line `import uuid` | ||
#} | ||
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %} | ||
{% if method_settings is not none %} | ||
{% for auto_populated_field in method_settings.auto_populated_fields %} | ||
{% if method.input.fields[auto_populated_field].proto3_optional %} | ||
if '{{ auto_populated_field }}' not in request: | ||
{% else %} | ||
if not request.{{ auto_populated_field }}: | ||
{% endif %} | ||
request.{{ auto_populated_field }} = str(uuid.uuid4()) | ||
{% endfor %} | ||
{% endif %}{# if method_settings is not none #} | ||
{% endwith %}{# method_settings #} | ||
{% endmacro %} | ||
|
||
{% macro add_google_api_core_version_header_import(service_version) %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a note at the top that this is intended to be a symlink to the other file, and for now is a duplicate (link to the issue you filed)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a62b313 |
||
{# | ||
The `version_header` module was added to `google-api-core` | ||
in version 2.19.0. | ||
https://github.com/googleapis/python-api-core/releases/tag/v2.19.0 | ||
The `try/except` below can be removed once the minimum version of | ||
`google-api-core` is 2.19.0 or newer. | ||
#} | ||
{% if service_version %} | ||
try: | ||
from google.api_core import version_header | ||
HAS_GOOGLE_API_CORE_VERSION_HEADER = True # pragma: NO COVER | ||
except ImportError: # pragma: NO COVER | ||
HAS_GOOGLE_API_CORE_VERSION_HEADER = False | ||
{% endif %}{# service_version #} | ||
{% endmacro %} | ||
{% macro add_api_version_header_to_metadata(service_version) %} | ||
{# | ||
Add API Version to metadata as per https://github.com/aip-dev/google.aip.dev/pull/1331. | ||
When using this macro, ensure the calling template also calls macro | ||
`add_google_api_core_version_header_import` to add the necessary import statements. | ||
#} | ||
{% if service_version %} | ||
if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER | ||
metadata = tuple(metadata) + ( | ||
version_header.to_api_version_header("{{ service_version }}"), | ||
) | ||
{% endif %}{# service_version #} | ||
{% endmacro %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{# | ||
# Copyright (C) 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#} | ||
|
||
{% macro auto_populate_uuid4_fields(api, method) %} | ||
{# | ||
Automatically populate UUID4 fields according to | ||
https://google.aip.dev/client-libraries/4235 when the | ||
field satisfies either of: | ||
- The field supports explicit presence and has not been set by the user. | ||
- The field doesn't support explicit presence, and its value is the empty | ||
string (i.e. the default value). | ||
When using this macro, ensure the calling template generates a line `import uuid` | ||
#} | ||
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %} | ||
{% if method_settings is not none %} | ||
{% for auto_populated_field in method_settings.auto_populated_fields %} | ||
{% if method.input.fields[auto_populated_field].proto3_optional %} | ||
if '{{ auto_populated_field }}' not in request: | ||
{% else %} | ||
if not request.{{ auto_populated_field }}: | ||
{% endif %} | ||
request.{{ auto_populated_field }} = str(uuid.uuid4()) | ||
{% endfor %} | ||
{% endif %}{# if method_settings is not none #} | ||
{% endwith %}{# method_settings #} | ||
{% endmacro %} | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
{% macro add_google_api_core_version_header_import(service_version) %} | ||
{# | ||
The `version_header` module was added to `google-api-core` | ||
in version 2.19.0. | ||
https://github.com/googleapis/python-api-core/releases/tag/v2.19.0 | ||
The `try/except` below can be removed once the minimum version of | ||
`google-api-core` is 2.19.0 or newer. | ||
#} | ||
{% if service_version %} | ||
try: | ||
from google.api_core import version_header | ||
HAS_GOOGLE_API_CORE_VERSION_HEADER = True # pragma: NO COVER | ||
except ImportError: # pragma: NO COVER | ||
HAS_GOOGLE_API_CORE_VERSION_HEADER = False | ||
{% endif %}{# service_version #} | ||
{% endmacro %} | ||
|
||
{% macro add_api_version_header_to_metadata(service_version) %} | ||
{# | ||
Add API Version to metadata as per https://github.com/aip-dev/google.aip.dev/pull/1331. | ||
When using this macro, ensure the calling template also calls macro | ||
`add_google_api_core_version_header_import` to add the necessary import statements. | ||
#} | ||
{% if service_version %} | ||
if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER | ||
metadata = tuple(metadata) + ( | ||
version_header.to_api_version_header("{{ service_version }}"), | ||
) | ||
{% endif %}{# service_version #} | ||
{% endmacro %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a regular file, not a symlink:
See the meaning of Git modes. (Credit: SO answer)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch and thanks for the link! Fixed in 7957ded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted this change in 542e4e1 and filed #2028 to investigate if symlinks can be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the symlinks don't work (weird, they should), can you specify a template outside the current directory (something like
{% include ../templates/foo/bar %}
If that also doesn't work, I would suggest keeping the duplicate files but adding a prominent header to the ads one saying that this is a copy of the one in the standard templates, intended to be a symlink. My rationale: we have duplicate code either way, and the macro file9s) makes them more organized....and it paves the way for the symlink once we can figure it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. You reverted back to the duplicate file, like I said in that last paragraph. SG. Could you add a note that this is intended to be a symlink to the other file, and for now is a duplicate?