Skip to content

Commit 9cd7664

Browse files
authored
feat: add flag for gapic metadata (#795)
As per the design doc, gapic metadata should not be generated by default, and should be gated by the 'metadata' flag. When invoking the generator via protoc, toggle the metadata flag like so: --python_gapic_opt="metadata" Subsequent Bazel integration is WIP.
1 parent fa2fdc0 commit 9cd7664

File tree

4 files changed

+54
-26
lines changed

4 files changed

+54
-26
lines changed

gapic/generator/generator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ def get_response(
105105
template_name, api_schema=api_schema, opts=opts)
106106
)
107107

108-
output_files.update(
109-
self._generate_samples_and_manifest(
110-
api_schema, self._env.get_template(sample_templates[0]),
111-
)
112-
)
108+
sample_output = self._generate_samples_and_manifest(
109+
api_schema,
110+
self._env.get_template(sample_templates[0]),
111+
) if sample_templates else {}
112+
113+
output_files.update(sample_output)
113114

114115
# Return the CodeGeneratorResponse output.
115116
res = CodeGeneratorResponse(
@@ -233,6 +234,10 @@ def _render_template(
233234
answer: Dict[str, CodeGeneratorResponse.File] = OrderedDict()
234235
skip_subpackages = False
235236

237+
# Very, very special case. This flag exists to gate this one file.
238+
if not opts.metadata and template_name.endswith("gapic_metadata.json.j2"):
239+
return answer
240+
236241
# Sanity check: Rendering per service and per proto would be a
237242
# combinatorial explosion and is almost certainly not what anyone
238243
# ever wants. Error colorfully on it.

gapic/utils/options.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,22 @@ class Options:
4141
lazy_import: bool = False
4242
old_naming: bool = False
4343
add_iam_methods: bool = False
44+
metadata: bool = False
4445
# TODO(yon-mg): should there be an enum for transport type?
4546
transport: List[str] = dataclasses.field(default_factory=lambda: [])
4647

4748
# Class constants
4849
PYTHON_GAPIC_PREFIX: str = 'python-gapic-'
4950
OPT_FLAGS: FrozenSet[str] = frozenset((
51+
'add-iam-methods', # microgenerator implementation for `reroute_to_grpc_interface`
52+
'lazy-import', # requires >= 3.7
53+
'metadata', # generate GAPIC metadata JSON file
5054
'old-naming', # TODO(dovs): Come up with a better comment
5155
'retry-config', # takes a path
5256
'samples', # output dir
53-
'lazy-import', # requires >= 3.7
54-
'add-iam-methods', # microgenerator implementation for `reroute_to_grpc_interface`
5557
# transport type(s) delineated by '+' (i.e. grpc, rest, custom.[something], etc?)
5658
'transport',
57-
'warehouse-package-name' # change the package name on PyPI
59+
'warehouse-package-name', # change the package name on PyPI
5860
))
5961

6062
@classmethod
@@ -143,6 +145,7 @@ def tweak_path(p):
143145
lazy_import=bool(opts.pop('lazy-import', False)),
144146
old_naming=bool(opts.pop('old-naming', False)),
145147
add_iam_methods=bool(opts.pop('add-iam-methods', False)),
148+
metadata=bool(opts.pop('metadata', False)),
146149
# transport should include desired transports delimited by '+', e.g. transport='grpc+rest'
147150
transport=opts.pop('transport', ['grpc'])[0].split('+')
148151
)

tests/unit/generator/test_generator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ def test_get_response_fails_invalid_file_paths():
116116
assert "%proto" in ex_str and "%service" in ex_str
117117

118118

119+
def test_get_response_ignore_gapic_metadata():
120+
g = make_generator()
121+
with mock.patch.object(jinja2.FileSystemLoader, "list_templates") as lt:
122+
lt.return_value = ["gapic/gapic_metadata.json.j2"]
123+
with mock.patch.object(jinja2.Environment, "get_template") as gt:
124+
gt.return_value = jinja2.Template(
125+
"This is not something we want to see")
126+
res = g.get_response(
127+
api_schema=make_api(),
128+
opts=Options.build(""),
129+
)
130+
131+
# We don't expect any files because opts.metadata is not set.
132+
assert res.file == CodeGeneratorResponse().file
133+
134+
119135
def test_get_response_ignores_unwanted_transports_and_clients():
120136
g = make_generator()
121137
with mock.patch.object(jinja2.FileSystemLoader, "list_templates") as lt:

tests/unit/generator/test_options.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616
import pytest
17+
import re
1718
from unittest import mock
1819
import warnings
1920

@@ -139,21 +140,24 @@ def test_options_service_config(fs):
139140
assert opts.retry == expected_cfg
140141

141142

142-
def test_options_lazy_import():
143-
opts = Options.build('lazy-import')
144-
assert opts.lazy_import
145-
146-
147-
def test_options_old_naming():
148-
opts = Options.build('old-naming')
149-
assert opts.old_naming
150-
151-
152-
def test_options_add_iam_methods():
153-
opts = Options.build('add-iam-methods')
154-
assert opts.add_iam_methods
155-
156-
157-
def test_options_warehouse_package_name():
158-
opts = Options.build('warehouse-package-name')
159-
assert opts.warehouse_package_name
143+
def test_options_bool_flags():
144+
# All these options are default False.
145+
# If new options violate this assumption,
146+
# this test may need to be tweaked.
147+
# New options should follow the dash-case/snake_case convention.
148+
opt_str_to_attr_name = {
149+
name: re.sub(r"-", "_", name)
150+
for name in
151+
["lazy-import",
152+
"old-naming",
153+
"add-iam-methods",
154+
"metadata",
155+
"warehouse-package-name",
156+
]}
157+
158+
for opt, attr in opt_str_to_attr_name.items():
159+
options = Options.build("")
160+
assert not getattr(options, attr)
161+
162+
options = Options.build(opt)
163+
assert getattr(options, attr)

0 commit comments

Comments
 (0)