-
Notifications
You must be signed in to change notification settings - Fork 69
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
feat: generate snippet metadata #1129
Changes from all commits
a623d6e
d356b12
89df318
b50dd72
7465546
237f076
8e07b88
872c156
e176b02
8315803
c58c5ae
906be9c
767a0ae
2d1e738
823ad2f
0ada00e
1e3defc
11d84b4
446591a
f710bb7
d6f2490
e358e25
02f1258
c7a41c5
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 |
---|---|---|
|
@@ -24,13 +24,13 @@ | |
|
||
from gapic import utils | ||
|
||
from gapic.samplegen_utils import types | ||
from gapic.samplegen_utils import types, snippet_metadata_pb2 # type: ignore | ||
from gapic.samplegen_utils.utils import is_valid_sample_cfg | ||
from gapic.schema import api | ||
from gapic.schema import wrappers | ||
|
||
from collections import defaultdict, namedtuple, ChainMap as chainmap | ||
from typing import Any, ChainMap, Dict, FrozenSet, Generator, List, Mapping, Optional, Sequence | ||
from typing import Any, ChainMap, Dict, FrozenSet, Generator, List, Mapping, Optional, Sequence, Tuple | ||
|
||
# There is no library stub file for this module, so ignore it. | ||
from google.api import resource_pb2 # type: ignore | ||
|
@@ -915,8 +915,6 @@ def _validate_loop(self, loop): | |
def parse_handwritten_specs(sample_configs: Sequence[str]) -> Generator[Dict[str, Any], None, None]: | ||
"""Parse a handwritten sample spec""" | ||
|
||
STANDALONE_TYPE = "standalone" | ||
|
||
for config_fpath in sample_configs: | ||
with open(config_fpath) as f: | ||
configs = yaml.safe_load_all(f.read()) | ||
|
@@ -925,13 +923,9 @@ def parse_handwritten_specs(sample_configs: Sequence[str]) -> Generator[Dict[str | |
valid = is_valid_sample_cfg(cfg) | ||
if not valid: | ||
raise types.InvalidConfig( | ||
"Sample config is invalid", valid) | ||
"Sample config in '{}' is invalid\n\n{}".format(config_fpath, cfg), valid) | ||
for spec in cfg.get("samples", []): | ||
# If unspecified, assume a sample config describes a standalone. | ||
# If sample_types are specified, standalone samples must be | ||
# explicitly enabled. | ||
if STANDALONE_TYPE in spec.get("sample_type", [STANDALONE_TYPE]): | ||
yield spec | ||
yield spec | ||
|
||
|
||
def _generate_resource_path_request_object(field_name: str, message: wrappers.MessageType) -> List[Dict[str, str]]: | ||
|
@@ -1050,7 +1044,6 @@ def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, A | |
# [{START|END} ${apishortname}_generated_${api}_${apiVersion}_${serviceName}_${rpcName}_{sync|async}_${overloadDisambiguation}] | ||
region_tag = f"{api_short_name}_generated_{api_schema.naming.versioned_module_name}_{service_name}_{rpc_name}_{transport_type}" | ||
spec = { | ||
"sample_type": "standalone", | ||
"rpc": rpc_name, | ||
"transport": transport, | ||
# `request` and `response` is populated in `preprocess_sample` | ||
|
@@ -1062,7 +1055,7 @@ def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, A | |
yield spec | ||
|
||
|
||
def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> str: | ||
def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> Tuple[str, Any]: | ||
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. Why use 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. Yes, since the type is ignored I cannot use it in an annotation. I learned about mypy-protobuf from a PR with Tres though, so I'll open a separate PR to start type checking all the protobuf types. |
||
"""Generate a standalone, runnable sample. | ||
|
||
Writing the rendered output is left for the caller. | ||
|
@@ -1073,7 +1066,7 @@ def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> str | |
sample_template (jinja2.Template): The template representing a generic sample. | ||
|
||
Returns: | ||
str: The rendered sample. | ||
Tuple(str, snippet_metadata_pb2.Snippet): The rendered sample. | ||
""" | ||
service_name = sample["service"] | ||
service = api_schema.services.get(service_name) | ||
|
@@ -1100,11 +1093,22 @@ def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> str | |
|
||
v.validate_response(sample["response"]) | ||
|
||
# Snippet Metadata can't be fully filled out in any one function | ||
# In this function we add information from | ||
# the API schema and sample dictionary. | ||
snippet_metadata = snippet_metadata_pb2.Snippet() # type: ignore | ||
snippet_metadata.region_tag = sample["region_tag"] | ||
setattr(snippet_metadata.client_method, "async", | ||
sample["transport"] == api.TRANSPORT_GRPC_ASYNC) | ||
snippet_metadata.client_method.method.short_name = sample["rpc"] | ||
snippet_metadata.client_method.method.service.short_name = sample["service"].split( | ||
".")[-1] | ||
|
||
return sample_template.render( | ||
sample=sample, | ||
imports=[], | ||
calling_form=calling_form, | ||
calling_form_enum=types.CallingForm, | ||
trim_blocks=True, | ||
lstrip_blocks=True, | ||
) | ||
), snippet_metadata |
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.
Doh! Good catch.