51 lines
1.6 KiB
Django/Jinja
51 lines
1.6 KiB
Django/Jinja
from typing import Dict, Any, Union, Type, TypeVar
|
|
from typing_extensions import Self
|
|
import attr
|
|
from ..types import UNSET, Unset
|
|
|
|
GY = TypeVar("GY", bound="{{name}}")
|
|
|
|
@attr.s(auto_attribs=True)
|
|
class {{name}}:
|
|
{% if description %}
|
|
"""{{description}}"""
|
|
{% endif %}
|
|
type: Union[
|
|
{% for type in types %}
|
|
{{type.name}},
|
|
{% endfor %}
|
|
]
|
|
|
|
def __init__(self,
|
|
type: Union[
|
|
{% for type in types %}
|
|
{{type.name}},
|
|
{% endfor %}
|
|
]):
|
|
self.type = type
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
{% for type in types %}{% if loop.first %}
|
|
if isinstance(self.type, {{type.name}}):
|
|
{{type.var0}} : {{type.name}} = self.type
|
|
return {{type.var0}}.to_dict()
|
|
{% else %}elif isinstance(self.type, {{type.name}}):
|
|
{{type.var0}} : {{type.name}} = self.type
|
|
return {{type.var0}}.to_dict()
|
|
{% endif %}{% endfor %}
|
|
raise Exception("Unknown type")
|
|
|
|
@classmethod
|
|
def from_dict(cls: Type[GY], d: Dict[str, Any]) -> GY:
|
|
{% for type in types %}{% if loop.first %}
|
|
if d.get("{{type.check}}") == {{type.value}}:
|
|
{{type.var1}} : {{type.name}} = {{type.name}}()
|
|
{{type.var1}}.from_dict(d)
|
|
return cls(type={{type.var1}})
|
|
{% else %}elif d.get("{{type.check}}") == {{type.value}}:
|
|
{{type.var1}} : {{type.name}} = {{type.name}}()
|
|
{{type.var1}}.from_dict(d)
|
|
return cls(type={{type.var1}})
|
|
{% endif %}{% endfor %}
|
|
raise Exception("Unknown type")
|