1+ from collections .abc import Callable
12from dataclasses import dataclass
23from typing import Any , Dict , Generic , List , Optional , TypeVar
34
5+ from scaleway_core .profile import ProfileDefaults
6+
47T = TypeVar ("T" )
58
69
710@dataclass
811class OneOfPossibility (Generic [T ]):
912 param : str
10-
1113 value : Optional [T ]
12-
13- default : Optional [T ] = None
14+ default : Optional [ T | ProfileDefaults ] = None
15+ marshal_func : Optional [Callable [[ T , ProfileDefaults | None ], Dict [ str , Any ]] ] = None
1416
1517
1618def resolve_one_of (
1719 possibilities : List [OneOfPossibility [Any ]], is_required : bool = False
18- ) -> Dict [str , Any ]:
20+ ) -> dict [str , Any | None ] | str | dict [ Any , Any ]:
1921 """
2022 Resolves the ideal parameter and value amongst an optional list.
23+ Uses marshal_func if provided.
2124 """
2225
23- # Get the first non-empty parameter
26+ # Try to resolve using non-None value
2427 for possibility in possibilities :
2528 if possibility .value is not None :
29+ if possibility .marshal_func is not None :
30+ return {
31+ possibility .param : possibility .marshal_func (
32+ possibility .value , possibility .default
33+ )
34+ }
2635 return {possibility .param : possibility .value }
2736
28- # Get the first non-empty default
37+ # Try to resolve using non-None default
2938 for possibility in possibilities :
3039 if possibility .default is not None :
40+ if possibility .marshal_func is not None :
41+ return {
42+ possibility .param : possibility .marshal_func (
43+ None , possibility .default
44+ )
45+ }
3146 return {possibility .param : possibility .default }
3247
33- # If required, raise an error
48+ # If required but unresolved , raise an error
3449 if is_required :
3550 possibilities_keys = " or " .join (
3651 [possibility .param for possibility in possibilities ]
3752 )
3853 raise ValueError (f"one of ${ possibilities_keys } must be present" )
3954
40- # Else, return an empty dict
41- return {}
55+ # Else, return empty dict
56+ return {}
0 commit comments