Fixed get_value
This commit is contained in:
parent
254b03d601
commit
fafa588880
@ -1,5 +1,4 @@
|
|||||||
import ast
|
from typing import Type, Optional
|
||||||
from typing import Type, Optional, Any
|
|
||||||
|
|
||||||
from core.typing import T
|
from core.typing import T
|
||||||
|
|
||||||
@ -22,7 +21,9 @@ def get_value(
|
|||||||
:rtype: Optional[T]
|
:rtype: Optional[T]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if key in source:
|
if key not in source:
|
||||||
|
return default
|
||||||
|
|
||||||
value = source[key]
|
value = source[key]
|
||||||
if isinstance(
|
if isinstance(
|
||||||
value,
|
value,
|
||||||
@ -35,20 +36,18 @@ def get_value(
|
|||||||
return value.lower() in ["true", "1"]
|
return value.lower() in ["true", "1"]
|
||||||
|
|
||||||
if (
|
if (
|
||||||
cast_type
|
cast_type if not hasattr(cast_type, "__origin__") else cast_type.__origin__
|
||||||
if not hasattr(cast_type, "__origin__")
|
|
||||||
else cast_type.__origin__
|
|
||||||
) == list:
|
) == list:
|
||||||
subtype = (
|
if not (value.startswith("[") and value.endswith("]")) and list_delimiter not in value:
|
||||||
cast_type.__args__[0] if hasattr(cast_type, "__args__") else None
|
raise ValueError("List values must be enclosed in square brackets or use a delimiter.")
|
||||||
)
|
|
||||||
|
if value.startswith("[") and value.endswith("]"):
|
||||||
|
value = value[1:-1]
|
||||||
|
|
||||||
value = value.split(list_delimiter)
|
value = value.split(list_delimiter)
|
||||||
return [
|
subtype = cast_type.__args__[0] if hasattr(cast_type, "__args__") else None
|
||||||
subtype(item) if subtype is not None else item for item in value
|
return [subtype(item) if subtype is not None else item for item in value]
|
||||||
]
|
|
||||||
|
|
||||||
return cast_type(value)
|
return cast_type(value)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
return default
|
return default
|
||||||
else:
|
|
||||||
return default
|
|
||||||
|
Loading…
Reference in New Issue
Block a user