diff --git a/src/viur/core/module.py b/src/viur/core/module.py index a2d3243f8..803b7a0e7 100644 --- a/src/viur/core/module.py +++ b/src/viur/core/module.py @@ -1,4 +1,5 @@ import copy +import enum import inspect import types import typing as t @@ -81,6 +82,8 @@ def parse_value_by_annotation(annotation: type, name: str, value: str | list | t Tries to parse a value according to a given type. May be called recursively to handle unions, lists and tuples as well. """ + # logging.debug(f"{annotation=} | {name=} | {value=}") + # simple types if annotation is str: return str(value) @@ -128,6 +131,15 @@ def parse_value_by_annotation(annotation: type, name: str, value: str | list | t return parse_value_by_annotation(int | str, name, value) + elif isinstance(annotation, enum.EnumMeta): + try: + return annotation(value) + except ValueError as exc: + for value_, member in annotation._value2member_map_.items(): + if str(value) == str(value_): # Do a string comparison, it could be a IntEnum + return member + raise errors.NotAcceptable(f"{' '.join(exc.args)} for {name}") from exc + raise errors.NotAcceptable(f"Unhandled type {annotation=} for {name}={value!r}") # examine parameters