diff --git a/reactivated/__init__.py b/reactivated/__init__.py index d33081129..23677d257 100644 --- a/reactivated/__init__.py +++ b/reactivated/__init__.py @@ -34,8 +34,15 @@ from .templates import interface as interface # noqa: F401 from .templates import template as template # noqa: F401 +import enum + def export(var: Any) -> None: + if inspect.isclass(var) and issubclass(var, enum.Enum): + name = f"{var.__module__}.{var.__qualname__}" + registry.value_registry.update({name: (var, False)}) + return + """See: https://stackoverflow.com/a/18425523""" callers_local_vars = inspect.currentframe().f_back.f_locals.items() # type: ignore[union-attr] name = [var_name for var_name, var_val in callers_local_vars if var_val is var][0] diff --git a/reactivated/serialization/registry.py b/reactivated/serialization/registry.py index 3717ff681..a8d32f514 100644 --- a/reactivated/serialization/registry.py +++ b/reactivated/serialization/registry.py @@ -2,6 +2,7 @@ Any, Callable, Dict, + Literal, Mapping, NamedTuple, Optional, @@ -22,7 +23,7 @@ } template_registry: Dict[str, Tuple[Any]] = {} interface_registry: Dict[str, Tuple[Any]] = {} -value_registry: Dict[str, Tuple[Any, bool]] = {} +value_registry: Dict[str, Tuple[Any, Literal["primitive", "class", "enum"]]] = {} definitions_registry: Dict[Any, Any] = {} rpc_registry: types.RPCRegistry = {} diff --git a/tests/exports.py b/tests/exports.py index 8abd994ea..069519e17 100644 --- a/tests/exports.py +++ b/tests/exports.py @@ -3,6 +3,14 @@ from reactivated import export, serialization from reactivated.apps import get_values from reactivated.serialization.registry import value_registry +import enum + + +class SimpleEnum(enum.Enum): + value: str + + FIRST = "First" + SECOND = "Second" class SimpleForm(forms.Form): @@ -29,3 +37,15 @@ def test_export_complex_types(): ) del value_registry["SimpleForm"] + + +def test_export_enum(): + export(SimpleEnum) + # generated_schema = serialization.create_schema(Type[SimpleEnum], {}) + + print(get_values()) + assert False + return + + + del value_registry["SimpleForm"]