This repository was archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (52 loc) · 2.1 KB
/
utils.py
File metadata and controls
60 lines (52 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Utils Module - this file contains utility functions used in multiple places.
"""
from __future__ import annotations
from copy import deepcopy
from itertools import chain, combinations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any, Iterator, Sequence
def merge_objects(dictionaries: Sequence[dict[str, Any]]) -> dict[str, Any]:
"""
Deeply merge objects.
"""
output: dict[str, Any] = {}
for dictionary in dictionaries:
for key, value in dictionary.items():
if key not in output:
output[key] = value
continue
current_value = output[key]
if isinstance(current_value, list) and isinstance(value, list):
output[key] = list(chain(output[key], value))
continue
if isinstance(current_value, dict) and isinstance(value, dict):
output[key] = merge_objects([current_value, value])
continue
return output
def normalize_schema_section(schema_section: dict[str, Any]) -> dict[str, Any]:
"""
Remove allOf and handle edge uses of oneOf.
"""
output: dict[str, Any] = deepcopy(schema_section)
if output.get("allOf"):
all_of = output.pop("allOf")
output = {**output, **merge_objects(all_of)}
if output.get("oneOf") and all(item.get("enum") for item in output["oneOf"]):
# handle the way drf-spectacular is doing enums
one_of = output.pop("oneOf")
output = {**output, **merge_objects(one_of)}
for key, value in output.items():
if isinstance(value, dict):
output[key] = normalize_schema_section(value)
elif isinstance(value, list):
output[key] = [normalize_schema_section(entry) if isinstance(entry, dict) else entry for entry in value]
return output
def lazy_combinations(options_list: Sequence[dict[str, Any]]) -> Iterator[dict]:
"""
Lazily evaluate possible combinations.
"""
for i in range(2, len(options_list) + 1):
for combination in combinations(options_list, i):
yield merge_objects(combination)