From 8f3df28b9d28a35a680b64ffafcf878634a37f10 Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 5 Nov 2019 18:03:48 -0400 Subject: [PATCH] ModelItem: serialize enum members to their values --- src/python/models/model_item.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/python/models/model_item.py b/src/python/models/model_item.py index ef2114a8..1fef7e6a 100644 --- a/src/python/models/model_item.py +++ b/src/python/models/model_item.py @@ -1,3 +1,4 @@ +from enum import Enum from typing import Any, Dict, Optional @@ -23,8 +24,14 @@ class ModelItem: @property def serialized(self) -> Dict[str, Any]: return { - name: getattr(self, name) for name in dir(self) + name: self._process_attr(getattr(self, name)) for name in dir(self) if not ( 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