Skip to content

Commit 6393266

Browse files
committed
fix(core): management one_of
1 parent 32421ae commit 6393266

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
from dataclasses import dataclass
2-
from typing import Any, Dict, Generic, List, Optional, TypeVar
2+
from typing import Any, Dict, Generic, List, Optional, TypeVar, Callable
3+
4+
from scaleway_core.profile import ProfileDefaults
35

46
T = TypeVar("T")
57

68

79
@dataclass
810
class OneOfPossibility(Generic[T]):
911
param: str
10-
1112
value: Optional[T]
12-
1313
default: Optional[T] = None
14+
marshal_func: Optional[Callable[[T, ProfileDefaults], Dict[str, Any]]] = None
1415

1516

1617
def resolve_one_of(
1718
possibilities: List[OneOfPossibility[Any]], is_required: bool = False
18-
) -> Dict[str, Any]:
19+
) -> dict[str, Any | None] | str | dict[Any, Any]:
1920
"""
2021
Resolves the ideal parameter and value amongst an optional list.
22+
Uses marshal_func if provided.
2123
"""
2224

23-
# Get the first non-empty parameter
25+
# Try to resolve using non-None value
2426
for possibility in possibilities:
2527
if possibility.value is not None:
28+
if possibility.marshal_func is not None:
29+
return {
30+
possibility.param: possibility.marshal_func(
31+
possibility.value, possibility.default
32+
)
33+
}
2634
return {possibility.param: possibility.value}
2735

28-
# Get the first non-empty default
36+
# Try to resolve using non-None default
2937
for possibility in possibilities:
3038
if possibility.default is not None:
39+
if possibility.marshal_func is not None:
40+
# When no actual value, call with None as value
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
55+
# Else, return empty dict
4156
return {}

0 commit comments

Comments
 (0)