Skip to content

Commit

Permalink
Add exporting enums
Browse files Browse the repository at this point in the history
  • Loading branch information
silviogutierrez committed Nov 7, 2023
1 parent b8aa507 commit 243f026
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions reactivated/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion reactivated/serialization/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Any,
Callable,
Dict,
Literal,
Mapping,
NamedTuple,
Optional,
Expand All @@ -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 = {}

Expand Down
20 changes: 20 additions & 0 deletions tests/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"]

0 comments on commit 243f026

Please sign in to comment.