Skip to content

Commit f67adc2

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

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
1+
from collections.abc import Callable
12
from dataclasses import dataclass
23
from typing import Any, Dict, Generic, List, Optional, TypeVar
34

5+
from scaleway_core.profile import ProfileDefaults
6+
47
T = TypeVar("T")
58

69

710
@dataclass
811
class OneOfPossibility(Generic[T]):
912
param: str
10-
1113
value: Optional[T]
12-
1314
default: Optional[T] = None
15+
marshal_func: Optional[Callable[[T, ProfileDefaults], Dict[str, Any]]] = None
1416

1517

1618
def 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+
# When no actual value, call with None as value
42+
return {
43+
possibility.param: possibility.marshal_func(
44+
None, possibility.default
45+
)
46+
}
3147
return {possibility.param: possibility.default}
3248

33-
# If required, raise an error
49+
# If required but unresolved, raise an error
3450
if is_required:
3551
possibilities_keys = " or ".join(
3652
[possibility.param for possibility in possibilities]
3753
)
3854
raise ValueError(f"one of ${possibilities_keys} must be present")
3955

40-
# Else, return an empty dict
56+
# Else, return empty dict
4157
return {}

0 commit comments

Comments
 (0)