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 :
31- < << << << HEAD
3240 if possibility .marshal_func is not None :
33- # When no actual value, call with None as value
3441 return {
3542 possibility .param : possibility .marshal_func (
3643 None , possibility .default
3744 )
3845 }
39- == == == =
40- >> >> >> > 539 c303 (Revert "fix(core): management one_of" )
4146 return {possibility .param : possibility .default }
4247
43- # If required, raise an error
48+ # If required but unresolved , raise an error
4449 if is_required :
4550 possibilities_keys = " or " .join (
4651 [possibility .param for possibility in possibilities ]
4752 )
4853 raise ValueError (f"one of ${ possibilities_keys } must be present" )
4954
50- # Else, return an empty dict
51- return {}
55+ # Else, return empty dict
56+ return {}
0 commit comments