Skip to content

Commit

Permalink
Provide 'types' and 'enums' submodule view (#273)
Browse files Browse the repository at this point in the history
This is a minor hack to preserve the module interface from the
monocode: types and enums for a service live in distinct submodules.

Includes tests for enums
  • Loading branch information
software-dov authored Jan 28, 2020
1 parent 68f72be commit f4bc871
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gapic/templates/%namespace/%name_%version/%sub/enums.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends '_base.py.j2' %}

{% block content %}
{% for p in api.protos.values() if p.file_to_generate and p.enums -%}
from .types.{{ p.module_name }} import ({% for e in p.enums.values() %}{{ e.name }}, {% endfor %})
{% endfor %}

__all__ = (
{%- for p in api.protos.values() if p.file_to_generate %}{% for e in p.enums.values() %}
'{{ e.name }}',
{%- endfor %}{% endfor %}
)
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends '_base.py.j2' %}

{% block content %}
{% for p in api.protos.values() if p.file_to_generate and p.messages -%}
from .{{ p.module_name }} import ({% for m in p.messages.values() %}{{ m.name }}, {% endfor %})
{% endfor %}

__all__ = (
{%- for p in api.protos.values() if p.file_to_generate %}{% for m in p.messages.values() %}
'{{ m.name }}',
{%- endfor %}{% endfor %}
)
{% endblock %}
12 changes: 12 additions & 0 deletions gapic/templates/tests/unit/%name_%version/%sub/test_%service.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ from google.api_core import future
from google.api_core import operations_v1
from google.longrunning import operations_pb2
{% endif -%}
{% if api.protos.values()|selectattr('enums') -%}
from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }} import enums
{% endif -%}
{% for method in service.methods.values() -%}
{% for ref_type in method.ref_types
if not ((ref_type.ident.python_import.package == ('google', 'api_core') and ref_type.ident.python_import.module == 'operation')
Expand Down Expand Up @@ -431,4 +434,13 @@ def test_{{ message.name|snake_case }}_path():

{% endwith -%}
{% endfor -%}

{% with enums = [] -%}{% for p in api.protos.values() if p.file_to_generate -%}{% for e in p.enums.values() if e.ident.module == service.meta.address.module -%}{% do enums.append(e) %}
{% if enums -%}
def test_enum_path():
{%- for e in enums %}
assert enums.{{ e.name }} == {{ e.ident.module }}.{{ e.name }}
{%- endfor %}
{% endif -%}
{% endfor -%}{% endfor -%}{% endwith -%}
{% endblock %}

0 comments on commit f4bc871

Please sign in to comment.