REF: Refactor plugin.py to use modular dataclasses in tree-like structure to represent parsed data (#121)
Refactor plugin to parse input into data-class based hierarchical structure
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							cbd3437080
						
					
				
				
					commit
					b5dcac1250
				
			| @@ -1,13 +1,13 @@ | ||||
| # Generated by the protocol buffer compiler.  DO NOT EDIT! | ||||
| # sources: {{ ', '.join(description.files) }} | ||||
| # sources: {{ ', '.join(description.input_filenames) }} | ||||
| # plugin: python-betterproto | ||||
| from dataclasses import dataclass | ||||
| {% if description.datetime_imports %} | ||||
| from datetime import {% for i in description.datetime_imports %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} | ||||
| from datetime import {% for i in description.datetime_imports|sort %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} | ||||
|  | ||||
| {% endif%} | ||||
| {% if description.typing_imports %} | ||||
| from typing import {% for i in description.typing_imports %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} | ||||
| from typing import {% for i in description.typing_imports|sort %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %} | ||||
|  | ||||
| {% endif %} | ||||
|  | ||||
| @@ -40,13 +40,13 @@ class {{ message.py_name }}(betterproto.Message): | ||||
| {{ message.comment }} | ||||
|  | ||||
|     {% endif %} | ||||
|     {% for field in message.properties %} | ||||
|     {% for field in message.fields %} | ||||
|         {% if field.comment %} | ||||
| {{ field.comment }} | ||||
|         {% endif %} | ||||
|     {{ field.py_name }}: {{ field.type }} = betterproto.{{ field.field_type }}_field({{ field.number }}{% if field.field_type == 'map'%}, betterproto.{{ field.map_types[0] }}, betterproto.{{ field.map_types[1] }}{% endif %}{% if field.one_of %}, group="{{ field.one_of }}"{% endif %}{% if field.field_wraps %}, wraps={{ field.field_wraps }}{% endif %}) | ||||
|     {{ field.get_field_string() }} | ||||
|     {% endfor %} | ||||
|     {% if not message.properties %} | ||||
|     {% if not message.fields %} | ||||
|     pass | ||||
|     {% endif %} | ||||
|  | ||||
| @@ -61,32 +61,37 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|     {% for method in service.methods %} | ||||
|     async def {{ method.py_name }}(self | ||||
|         {%- if not method.client_streaming -%} | ||||
|             {%- if method.input_message and method.input_message.properties -%}, *, | ||||
|                 {%- for field in method.input_message.properties -%} | ||||
|                     {{ field.py_name }}: {% if field.zero == "None" and not field.type.startswith("Optional[") -%} | ||||
|                                             Optional[{{ field.type }}] | ||||
|             {%- if method.py_input_message and method.py_input_message.fields -%}, *, | ||||
|                 {%- for field in method.py_input_message.fields -%} | ||||
|                     {{ field.py_name }}: {% if field.py_name in method.mutable_default_args and not field.annotation.startswith("Optional[") -%} | ||||
|                                             Optional[{{ field.annotation }}] | ||||
|                                          {%- else -%} | ||||
|                                             {{ field.type }} | ||||
|                                          {%- endif -%} = {{ field.zero }} | ||||
|                                             {{ field.annotation }} | ||||
|                                          {%- endif -%} =  | ||||
|                                             {%- if field.py_name not in method.mutable_default_args -%} | ||||
|                                                 {{ field.default_value_string }} | ||||
|                                             {%- else -%} | ||||
|                                                 None | ||||
|                                             {% endif -%} | ||||
|                     {%- if not loop.last %}, {% endif -%} | ||||
|                 {%- endfor -%} | ||||
|             {%- endif -%} | ||||
|         {%- else -%} | ||||
|             {# Client streaming: need a request iterator instead #} | ||||
|             , request_iterator: Union[AsyncIterable["{{ method.input }}"], Iterable["{{ method.input }}"]] | ||||
|             , request_iterator: Union[AsyncIterable["{{ method.py_input_message_type }}"], Iterable["{{ method.py_input_message_type }}"]] | ||||
|         {%- endif -%} | ||||
|             ) -> {% if method.server_streaming %}AsyncIterator[{{ method.output }}]{% else %}{{ method.output }}{% endif %}: | ||||
|             ) -> {% if method.server_streaming %}AsyncIterator["{{ method.py_output_message_type }}"]{% else %}"{{ method.py_output_message_type }}"{% endif %}: | ||||
|         {% if method.comment %} | ||||
| {{ method.comment }} | ||||
|  | ||||
|         {% endif %} | ||||
|         {%- for py_name, zero in method.mutable_default_args %} | ||||
|         {%- for py_name, zero in method.mutable_default_args.items() %} | ||||
|         {{ py_name }} = {{ py_name }} or {{ zero }} | ||||
|         {% endfor %} | ||||
|  | ||||
|         {% if not method.client_streaming %} | ||||
|         request = {{ method.input }}() | ||||
|         {% for field in method.input_message.properties %} | ||||
|         request = {{ method.py_input_message_type }}() | ||||
|         {% for field in method.py_input_message.fields %} | ||||
|             {% if field.field_type == 'message' %} | ||||
|         if {{ field.py_name }} is not None: | ||||
|             request.{{ field.py_name }} = {{ field.py_name }} | ||||
| @@ -101,15 +106,15 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|         async for response in self._stream_stream( | ||||
|             "{{ method.route }}", | ||||
|             request_iterator, | ||||
|             {{ method.input }}, | ||||
|             {{ method.output.strip('"') }}, | ||||
|             {{ method.py_input_message_type }}, | ||||
|             {{ method.py_output_message_type.strip('"') }}, | ||||
|         ): | ||||
|             yield response | ||||
|             {% else %}{# i.e. not client streaming #} | ||||
|         async for response in self._unary_stream( | ||||
|             "{{ method.route }}", | ||||
|             request, | ||||
|             {{ method.output.strip('"') }}, | ||||
|             {{ method.py_output_message_type.strip('"') }}, | ||||
|         ): | ||||
|             yield response | ||||
|  | ||||
| @@ -119,14 +124,14 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|         return await self._stream_unary( | ||||
|             "{{ method.route }}", | ||||
|             request_iterator, | ||||
|             {{ method.input }}, | ||||
|             {{ method.output.strip('"') }} | ||||
|             {{ method.py_input_message_type }}, | ||||
|             {{ method.py_output_message_type.strip('"') }} | ||||
|         ) | ||||
|             {% else %}{# i.e. not client streaming #} | ||||
|         return await self._unary_unary( | ||||
|             "{{ method.route }}", | ||||
|             request, | ||||
|             {{ method.output.strip('"') }} | ||||
|             {{ method.py_output_message_type.strip('"') }} | ||||
|         ) | ||||
|             {% endif %}{# client streaming #} | ||||
|         {% endif %} | ||||
| @@ -134,6 +139,6 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub): | ||||
|     {% endfor %} | ||||
| {% endfor %} | ||||
|  | ||||
| {% for i in description.imports %} | ||||
| {% for i in description.imports|sort %} | ||||
| {{ i }} | ||||
| {% endfor %} | ||||
| {% endfor %} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user