diff --git a/docs/module.rst b/docs/module.rst new file mode 100644 index 00000000..e69de29b diff --git a/scaleway-core/scaleway_core/utils/resolve_one_of.py b/scaleway-core/scaleway_core/utils/resolve_one_of.py index a829d602..87f1dc5e 100644 --- a/scaleway-core/scaleway_core/utils/resolve_one_of.py +++ b/scaleway-core/scaleway_core/utils/resolve_one_of.py @@ -1,5 +1,9 @@ +from collections.abc import Callable from dataclasses import dataclass from typing import Any, Dict, Generic, List, Optional, TypeVar +from _typeshed import SupportsKeysAndGetItem + +from scaleway_core.profile import ProfileDefaults T = TypeVar("T") @@ -7,25 +11,31 @@ @dataclass class OneOfPossibility(Generic[T]): param: str - value: Optional[T] - - default: Optional[T] = None + default: Optional[T | ProfileDefaults] = None + marshal_func: Optional[Callable[[T, T | None], Dict[str, Any]]] = None def resolve_one_of( possibilities: List[OneOfPossibility[Any]], is_required: bool = False -) -> Dict[str, Any]: +) -> SupportsKeysAndGetItem[str, Any]: """ Resolves the ideal parameter and value amongst an optional list. + Uses marshal_func if provided. """ - # Get the first non-empty parameter + # Try to resolve using non-None value for possibility in possibilities: if possibility.value is not None: + if possibility.marshal_func is not None: + return { + possibility.param: possibility.marshal_func( + possibility.value, possibility.default + ) + } return {possibility.param: possibility.value} - # Get the first non-empty default + # Try to resolve using non-None default for possibility in possibilities: if possibility.default is not None: if possibility.marshal_func is not None: @@ -37,12 +47,12 @@ def resolve_one_of( } return {possibility.param: possibility.default} - # If required, raise an error + # If required but unresolved, raise an error if is_required: possibilities_keys = " or ".join( [possibility.param for possibility in possibilities] ) raise ValueError(f"one of ${possibilities_keys} must be present") - # Else, return an empty dict + # Else, return empty dict return {}