Files
kittycad.py/generate/union-type.py.jinja2
Jess Frazelle 6b8807feea improvements
Signed-off-by: Jess Frazelle <github@jessfraz.com>
2023-11-28 14:16:05 -08:00

47 lines
1.4 KiB
Django/Jinja

from typing import Dict, Any, Union
from typing_extensions import Self
class {{name}}:
{% if description %}
"""{{description}}"""
{% endif %}
type: Union[
{% for type in types %}
{{type.name}},
{% endfor %}
] = None
def __init__(self,
type: Union[
{% for type in types %}
type({{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}}):
n : {{type.name}} = self.type
return n.to_dict()
{% else %}elif isinstance(self.type, {{type.name}}):
n : {{type.name}} = self.type
return n.to_dict()
{% endif %}{% endfor %}
raise Exception("Unknown type")
def from_dict(self, d) -> Self:
{% for type in types %}{% if loop.first %}
if d.get("type") == "{{type.name}}":
n : {{type.name}} = {{type.name}}()
n.from_dict(d)
self.type = n
return Self
{% else %}elif d.get("type") == "{{type.name}}":
n : {{type.name}} = {{type.name}}()
n.from_dict(d)
self.type = n
return self
{% endif %}{% endfor %}
raise Exception("Unknown type")