Skip to content

Commit dc3746b

Browse files
Laure-discaleway-botremyleone
authored
fix(core): support marshal func in one of (#1073)
Co-authored-by: Scaleway Bot <[email protected]> Co-authored-by: Rémy Léone <[email protected]>
1 parent 87e73a4 commit dc3746b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

docs/module.rst

Whitespace-only changes.
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1+
from collections.abc import Callable
12
from dataclasses import dataclass
23
from typing import Any, Dict, Generic, List, Optional, TypeVar
4+
from _typeshed import SupportsKeysAndGetItem
5+
6+
from scaleway_core.profile import ProfileDefaults
37

48
T = TypeVar("T")
59

610

711
@dataclass
812
class 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

1619
def 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

Comments
 (0)