Skip to content

Commit 3ef1aad

Browse files
authored
chore: add sample function parameters, without type annotations (#1540)
* chore: add sample function parameters, without type annotations
1 parent 2085e8d commit 3ef1aad

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

packages/gapic-generator/gapic/configurable_snippetgen/configured_snippet.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,21 @@ def filename(self) -> str:
7979
sync_or_async = "sync" if self.is_sync else "async"
8080
return f"{module_name}_{self.api_version}_generated_{service_name}_{snake_case_rpc_name}_{config_id}_{sync_or_async}.py"
8181

82+
def _add_sample_function_parameters(self) -> None:
83+
# TODO: https://github.com/googleapis/gapic-generator-python/issues/1537, add typing annotation in sample function parameters.
84+
params = []
85+
for config_parameter in self.config.signature.parameters:
86+
params.append(libcst_utils.convert_parameter(config_parameter))
87+
parameters = libcst.Parameters(params=params)
88+
self._sample_function_def = self._sample_function_def.with_changes(
89+
params=parameters
90+
)
91+
8292
def _build_sample_function(self) -> None:
8393
# TODO: https://github.com/googleapis/gapic-generator-python/issues/1536, add return type.
84-
# TODO: https://github.com/googleapis/gapic-generator-python/issues/1537, add sample function parameters.
8594
# TODO: https://github.com/googleapis/gapic-generator-python/issues/1538, add docstring.
8695
# TODO: https://github.com/googleapis/gapic-generator-python/issues/1539, add sample function body.
87-
pass
96+
self._add_sample_function_parameters()
8897

8998
def _add_sample_function(self) -> None:
9099
self._module = self._module.with_changes(

packages/gapic-generator/gapic/configurable_snippetgen/libcst_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,26 @@ def base_function_def(function_name: str, is_sync: bool) -> libcst.FunctionDef:
3434
)
3535

3636
return function_def
37+
38+
39+
def convert_expression(
40+
config_expression: snippet_config_language_pb2.Expression,
41+
) -> libcst.BaseExpression:
42+
value_name = config_expression.WhichOneof("value")
43+
if value_name == "string_value":
44+
string_value = config_expression.string_value
45+
return libcst.SimpleString(value=f'"{string_value}"')
46+
else:
47+
raise ValueError(
48+
f"Conversion from Expression value {value_name} unsupported.")
49+
50+
51+
def convert_parameter(
52+
config_parameter: snippet_config_language_pb2.Statement.Declaration,
53+
) -> libcst.Param:
54+
# TODO: https://github.com/googleapis/gapic-generator-python/issues/1537, add typing annotation in sample function parameters.
55+
param = libcst.Param(
56+
name=libcst.Name(value=config_parameter.name),
57+
default=convert_expression(config_parameter.value),
58+
)
59+
return param

packages/gapic-generator/tests/unit/configurable_snippetgen/test_configured_snippet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_code(snippet):
104104
# https://github.com/googleapis/gapic-generator-python/issues/1522
105105
# Placeholder code. We will gradually add to the ConfiguredSnippet class
106106
# until the generated code is the same as that of the golden file.
107-
expected_code = """def sample_create_custom_class_Basic():
107+
expected_code = """def sample_create_custom_class_Basic(parent = "projects/[PROJECT]/locations/us", custom_class_id = "passengerships"):
108108
\"\"
109109
"""
110110
assert snippet.code == expected_code

packages/gapic-generator/tests/unit/configurable_snippetgen/test_libcst_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pytest
1818

1919
from gapic.configurable_snippetgen import libcst_utils
20+
from gapic.configurable_snippetgen import snippet_config_language_pb2
2021

2122

2223
def _assert_code_equal(node: libcst.CSTNode, code: str) -> str:
@@ -41,3 +42,36 @@ def test_base_function_def(is_sync, expected_code):
4142

4243
# Sometimes it is more convenient to just verify the code.
4344
_assert_code_equal(node, expected_code)
45+
46+
47+
def test_convert_expression_string_value():
48+
config_expression = snippet_config_language_pb2.Expression(
49+
string_value="hello world"
50+
)
51+
node = libcst_utils.convert_expression(config_expression)
52+
expected_node = libcst.SimpleString(value='"hello world"')
53+
54+
assert node.deep_equals(expected_node), (node, expected_node)
55+
56+
57+
def test_convert_expression_should_raise_error_if_unsupported():
58+
config_expression = snippet_config_language_pb2.Expression(
59+
default_value=snippet_config_language_pb2.Expression.DefaultValue.DEFAULT_VALUE
60+
)
61+
with pytest.raises(ValueError):
62+
libcst_utils.convert_expression(config_expression)
63+
64+
65+
def test_convert_parameter():
66+
config_parameter = snippet_config_language_pb2.Statement.Declaration(
67+
name="some_variable",
68+
value=snippet_config_language_pb2.Expression(
69+
string_value="hello world"),
70+
)
71+
node = libcst_utils.convert_parameter(config_parameter)
72+
expected_node = libcst.Param(
73+
name=libcst.Name(value="some_variable"),
74+
default=libcst.SimpleString(value='"hello world"'),
75+
)
76+
77+
assert node.deep_equals(expected_node), (node, expected_node)

0 commit comments

Comments
 (0)