ModelItem: serialize enum members to their values

This commit is contained in:
miruka 2019-11-05 18:03:48 -04:00
parent 7408322fbe
commit 8f3df28b9d

View File

@ -1,3 +1,4 @@
from enum import Enum
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
@ -23,8 +24,14 @@ class ModelItem:
@property @property
def serialized(self) -> Dict[str, Any]: def serialized(self) -> Dict[str, Any]:
return { return {
name: getattr(self, name) for name in dir(self) name: self._process_attr(getattr(self, name)) for name in dir(self)
if not ( if not (
name.startswith("_") or name in ("parent_model", "serialized") name.startswith("_") or name in ("parent_model", "serialized")
) )
} }
@staticmethod
def _process_attr(value: Any) -> Any:
if hasattr(value, "__class__") and issubclass(value.__class__, Enum):
return value.value
return value