2023-11-28 14:29:16 -08:00
|
|
|
from typing import Dict, Any, Union, Type, TypeVar
|
2023-11-28 14:16:05 -08:00
|
|
|
from typing_extensions import Self
|
2023-11-28 14:29:16 -08:00
|
|
|
import attr
|
2023-11-28 15:17:44 -08:00
|
|
|
from ..types import UNSET, Unset
|
2023-11-28 14:16:05 -08:00
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
from pydantic import GetCoreSchemaHandler
|
|
|
|
from pydantic_core import CoreSchema, core_schema
|
|
|
|
|
2023-11-28 14:29:16 -08:00
|
|
|
GY = TypeVar("GY", bound="{{name}}")
|
|
|
|
|
|
|
|
@attr.s(auto_attribs=True)
|
2023-11-28 14:16:05 -08:00
|
|
|
class {{name}}:
|
|
|
|
{% if description %}
|
|
|
|
"""{{description}}"""
|
|
|
|
{% endif %}
|
|
|
|
type: Union[
|
|
|
|
{% for type in types %}
|
|
|
|
{{type.name}},
|
|
|
|
{% endfor %}
|
2023-11-28 14:29:16 -08:00
|
|
|
]
|
2023-11-28 14:16:05 -08:00
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
type: Union[
|
|
|
|
{% for type in types %}
|
2023-11-28 14:29:16 -08:00
|
|
|
{{type.name}},
|
2023-11-28 14:16:05 -08:00
|
|
|
{% endfor %}
|
|
|
|
]):
|
|
|
|
self.type = type
|
|
|
|
|
2023-11-28 23:50:50 -08:00
|
|
|
def model_dump(self) -> Dict[str, Any]:
|
2023-11-28 14:16:05 -08:00
|
|
|
{% for type in types %}{% if loop.first %}
|
|
|
|
if isinstance(self.type, {{type.name}}):
|
2023-11-28 14:29:16 -08:00
|
|
|
{{type.var0}} : {{type.name}} = self.type
|
2023-11-28 23:50:50 -08:00
|
|
|
return {{type.var0}}.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
{% else %}elif isinstance(self.type, {{type.name}}):
|
2023-11-28 14:29:16 -08:00
|
|
|
{{type.var0}} : {{type.name}} = self.type
|
2023-11-28 23:50:50 -08:00
|
|
|
return {{type.var0}}.model_dump()
|
2023-11-28 14:16:05 -08:00
|
|
|
{% endif %}{% endfor %}
|
|
|
|
raise Exception("Unknown type")
|
|
|
|
|
2023-11-28 14:29:16 -08:00
|
|
|
@classmethod
|
|
|
|
def from_dict(cls: Type[GY], d: Dict[str, Any]) -> GY:
|
2023-11-28 14:16:05 -08:00
|
|
|
{% for type in types %}{% if loop.first %}
|
2023-11-28 16:37:47 -08:00
|
|
|
if d.get("{{type.check}}") == {{type.value}}:
|
2023-11-28 23:50:50 -08:00
|
|
|
{{type.var1}} : {{type.name}} = {{type.name}}(**d)
|
2023-11-28 14:29:16 -08:00
|
|
|
return cls(type={{type.var1}})
|
2023-11-28 16:37:47 -08:00
|
|
|
{% else %}elif d.get("{{type.check}}") == {{type.value}}:
|
2023-11-28 23:50:50 -08:00
|
|
|
{{type.var1}} : {{type.name}} = {{type.name}}(**d)
|
2023-11-28 14:29:16 -08:00
|
|
|
return cls(type={{type.var1}})
|
2023-11-28 14:16:05 -08:00
|
|
|
{% endif %}{% endfor %}
|
|
|
|
raise Exception("Unknown type")
|
2023-11-28 23:50:50 -08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __get_pydantic_core_schema__(
|
|
|
|
cls, source_type: Any, handler: GetCoreSchemaHandler
|
|
|
|
) -> CoreSchema:
|
|
|
|
return core_schema.no_info_after_validator_function(cls, handler(Union[
|
|
|
|
{% for type in types %}
|
|
|
|
{{type.name}},
|
|
|
|
{% endfor %}
|
|
|
|
]))
|