|
1 | | -from collections.abc import Callable |
2 | 1 | from dataclasses import dataclass |
3 | 2 | from typing import Any, Dict, Generic, List, Optional, TypeVar |
4 | 3 |
|
5 | | -from scaleway_core.profile import ProfileDefaults |
6 | | - |
7 | 4 | T = TypeVar("T") |
8 | 5 |
|
9 | 6 |
|
10 | 7 | @dataclass |
11 | 8 | class OneOfPossibility(Generic[T]): |
12 | 9 | param: str |
| 10 | + |
13 | 11 | value: Optional[T] |
14 | | - default: Optional[T | ProfileDefaults] = None |
15 | | - marshal_func: Optional[Callable[[T, ProfileDefaults | None], Dict[str, Any]]] = None |
| 12 | + |
| 13 | + default: Optional[T] = None |
16 | 14 |
|
17 | 15 |
|
18 | 16 | def resolve_one_of( |
19 | 17 | possibilities: List[OneOfPossibility[Any]], is_required: bool = False |
20 | | -) -> dict[str, Any | None] | str | dict[Any, Any]: |
| 18 | +) -> Dict[str, Any]: |
21 | 19 | """ |
22 | 20 | Resolves the ideal parameter and value amongst an optional list. |
23 | | - Uses marshal_func if provided. |
24 | 21 | """ |
25 | 22 |
|
26 | | - # Try to resolve using non-None value |
| 23 | + # Get the first non-empty parameter |
27 | 24 | for possibility in possibilities: |
28 | 25 | 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 | | - } |
35 | 26 | return {possibility.param: possibility.value} |
36 | 27 |
|
37 | | - # Try to resolve using non-None default |
| 28 | + # Get the first non-empty default |
38 | 29 | for possibility in possibilities: |
39 | 30 | if possibility.default is not None: |
| 31 | +<<<<<<< HEAD |
40 | 32 | if possibility.marshal_func is not None: |
41 | 33 | # When no actual value, call with None as value |
42 | 34 | return { |
43 | 35 | possibility.param: possibility.marshal_func( |
44 | 36 | None, possibility.default |
45 | 37 | ) |
46 | 38 | } |
| 39 | +======= |
| 40 | +>>>>>>> 539c303 (Revert "fix(core): management one_of") |
47 | 41 | return {possibility.param: possibility.default} |
48 | 42 |
|
49 | | - # If required but unresolved, raise an error |
| 43 | + # If required, raise an error |
50 | 44 | if is_required: |
51 | 45 | possibilities_keys = " or ".join( |
52 | 46 | [possibility.param for possibility in possibilities] |
53 | 47 | ) |
54 | 48 | raise ValueError(f"one of ${possibilities_keys} must be present") |
55 | 49 |
|
56 | | - # Else, return empty dict |
| 50 | + # Else, return an empty dict |
57 | 51 | return {} |
0 commit comments