Skip to content

Commit 6c3b3cd

Browse files
authored
Minor cleanups for generated sample manifest (#181)
Manifest does not render empty region tags Sample manifest ends in a newline
1 parent 14da46e commit 6c3b3cd

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

packages/gapic-generator/gapic/samplegen/samplegen.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ def generate_manifest(
729729
Tuple[str, yaml.Doc]: The filename of the manifest and the manifest data as a dictionary.
730730
731731
"""
732+
732733
doc = yaml.Doc(
733734
[
734735
yaml.KeyVal("type", "manifest/samples"),
@@ -748,14 +749,18 @@ def generate_manifest(
748749
yaml.Collection(
749750
name="samples",
750751
elements=[
751-
[
752+
[ # type: ignore
753+
# Mypy doesn't correctly intuit the type of the
754+
# "region_tag" conditional expression.
752755
yaml.Alias("python"),
753756
yaml.KeyVal("sample", sample["id"]),
754757
yaml.KeyVal("path",
755758
"'{base_path}/%s'" % os.path.relpath(fpath,
756759
base_path)),
757-
yaml.KeyVal("region_tag",
758-
sample.get("region_tag", "")),
760+
(yaml.KeyVal("region_tag", sample["region_tag"])
761+
if "region_tag" in sample else
762+
yaml.Null),
763+
759764
]
760765
for fpath, sample in fpaths_and_samples
761766
],

packages/gapic-generator/gapic/samplegen_utils/yaml.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ def render(self, spaces: int = 0) -> str:
3535
return ""
3636

3737

38+
@dataclasses.dataclass(frozen=True)
39+
class Null(Element):
40+
def render(self, spaces: int = 0) -> str:
41+
return ""
42+
43+
3844
@dataclasses.dataclass(frozen=True)
3945
class KeyVal(Element):
4046
"""A single key/value entry."""
@@ -65,9 +71,11 @@ def render(self, spaces: int = 0) -> str:
6571
return f"{self.name}:\n" + "\n".join(
6672
indent(
6773
"-"
68-
+ "\n".join(e.render(spaces=spaces + self.INDENT_SPACES) for e in l)[
69-
1:
70-
],
74+
+ "\n".join(
75+
r
76+
for r in (e.render(spaces + self.INDENT_SPACES) for e in l)
77+
if r
78+
)[1:],
7179
" " * (spaces),
7280
)
7381
for l in self.elements
@@ -106,4 +114,4 @@ class Doc(Element):
106114
elements: List[Element]
107115

108116
def render(self):
109-
return "---\n{}".format("\n".join(e.render() for e in self.elements))
117+
return "---\n{}\n".format("\n".join(e.render() for e in self.elements))

packages/gapic-generator/tests/unit/generator/test_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def test_samplegen_config_to_output_files(mock_gmtime, mock_generate_sample, fs)
356356
sample: clam_sample
357357
path: '{base_path}/clam_sample.py'
358358
region_tag: clam_sample
359-
""".rstrip()),
359+
"""),
360360
)
361361
]
362362
)
@@ -446,7 +446,7 @@ def test_samplegen_id_disambiguation(mock_gmtime, mock_generate_sample, fs):
446446
- <<: *python
447447
sample: 157884ee
448448
path: '{base_path}/157884ee.py'
449-
region_tag: """)
449+
""")
450450
),
451451
]
452452
)

packages/gapic-generator/tests/unit/samplegen/test_manifest.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def test_generate_manifest():
6363
"sample", "squid_sample"),
6464
gapic_yaml.KeyVal(
6565
"path", "'{base_path}/squid_fpath.py'"),
66-
gapic_yaml.KeyVal(
67-
"region_tag", ""),
66+
gapic_yaml.Null,
6867
],
6968
[
7069
gapic_yaml.Alias("python"),
@@ -80,24 +79,25 @@ def test_generate_manifest():
8079

8180
assert info == doc
8281

83-
expected_rendering = dedent("""
84-
---
85-
type: manifest/samples
86-
schema_version: 3
87-
python: &python
88-
environment: python
89-
bin: python3
90-
base_path: samples/
91-
invocation: '{bin} {path} @args'
92-
samples:
93-
- <<: *python
94-
sample: squid_sample
95-
path: '{base_path}/squid_fpath.py'
96-
region_tag:
97-
- <<: *python
98-
sample: clam_sample
99-
path: '{base_path}/clam_fpath.py'
100-
region_tag: giant_clam_sample""".lstrip("\n"))
82+
expected_rendering = dedent(
83+
"""\
84+
---
85+
type: manifest/samples
86+
schema_version: 3
87+
python: &python
88+
environment: python
89+
bin: python3
90+
base_path: samples/
91+
invocation: '{bin} {path} @args'
92+
samples:
93+
- <<: *python
94+
sample: squid_sample
95+
path: '{base_path}/squid_fpath.py'
96+
- <<: *python
97+
sample: clam_sample
98+
path: '{base_path}/clam_fpath.py'
99+
region_tag: giant_clam_sample
100+
""")
101101

102102
rendered_yaml = doc.render()
103103
assert rendered_yaml == expected_rendering
@@ -119,7 +119,6 @@ def test_generate_manifest():
119119
"invocation": "{bin} {path} @args",
120120
"sample": "squid_sample",
121121
"path": "{base_path}/squid_fpath.py",
122-
"region_tag": None,
123122
},
124123
{
125124
"environment": "python",

0 commit comments

Comments
 (0)