1+ from collections .abc import Callable
12from dataclasses import dataclass
23from typing import Any , Dict , Generic , List , Optional , TypeVar
4+ from _typeshed import SupportsKeysAndGetItem
5+
6+ from scaleway_core .profile import ProfileDefaults
37
48T = TypeVar ("T" )
59
610
711@dataclass
812class OneOfPossibility (Generic [T ]):
913 param : str
10-
1114 value : Optional [T ]
12-
13- default : Optional [T ] = None
15+ default : Optional [ T | ProfileDefaults ] = None
16+ marshal_func : Optional [Callable [[ T , T | None ], Dict [ str , Any ]] ] = None
1417
1518
1619def resolve_one_of (
1720 possibilities : List [OneOfPossibility [Any ]], is_required : bool = False
18- ) -> Dict [str , Any ]:
21+ ) -> SupportsKeysAndGetItem [str , Any ]:
1922 """
2023 Resolves the ideal parameter and value amongst an optional list.
24+ Uses marshal_func if provided.
2125 """
2226
23- # Get the first non-empty parameter
27+ # Try to resolve using non-None value
2428 for possibility in possibilities :
2529 if possibility .value is not None :
30+ if possibility .marshal_func is not None :
31+ return {
32+ possibility .param : possibility .marshal_func (
33+ possibility .value , possibility .default
34+ )
35+ }
2636 return {possibility .param : possibility .value }
2737
28- # Get the first non-empty default
38+ # Try to resolve using non-None default
2939 for possibility in possibilities :
3040 if possibility .default is not None :
3141 if possibility .marshal_func is not None :
@@ -37,12 +47,12 @@ def resolve_one_of(
3747 }
3848 return {possibility .param : possibility .default }
3949
40- # If required, raise an error
50+ # If required but unresolved , raise an error
4151 if is_required :
4252 possibilities_keys = " or " .join (
4353 [possibility .param for possibility in possibilities ]
4454 )
4555 raise ValueError (f"one of ${ possibilities_keys } must be present" )
4656
47- # Else, return an empty dict
57+ # Else, return empty dict
4858 return {}
0 commit comments