This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Na'aman Hirschfeld
authored and
Na'aman Hirschfeld
committed
Feb 28, 2021
1 parent
e89a5cc
commit c281ffc
Showing
4 changed files
with
33 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,47 @@ | ||
""" Utils Module - this file contains utility functions used in multiple places """ | ||
from typing import Any, Dict, Sequence | ||
from copy import deepcopy | ||
from itertools import chain, combinations | ||
from typing import Any, Dict, Iterator, Sequence | ||
|
||
|
||
def merge_objects(dictionaries: Sequence[Dict[str, Any]]) -> Dict[str, Any]: | ||
""" helper function to deep merge objects """ | ||
output: Dict[str, Any] = {} | ||
for dictionary in dictionaries: | ||
for key, value in dictionary.items(): | ||
if isinstance(value, dict) and "allOf" in value: | ||
all_of = merge_objects(value.pop("allOf")) | ||
value = merge_objects([value, all_of]) | ||
if key not in output: | ||
output[key] = value | ||
continue | ||
current_value = output[key] | ||
if isinstance(current_value, list) and isinstance(value, list): | ||
output[key] = [*output[key], *value] | ||
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) -> dict: | ||
""" helper method to remove allOf and handle edge uses of oneOf""" | ||
output: Dict[str, Any] = {**schema_section} | ||
if "allOf" in schema_section: | ||
all_of = schema_section.pop("allOf") | ||
schema_section = {**schema_section, **merge_objects(all_of)} | ||
if schema_section.get("oneOf") and all(item.get("enum") for item in schema_section["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 = schema_section.pop("oneOf") | ||
schema_section = {**schema_section, **merge_objects(one_of)} | ||
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 schema_section | ||
return output | ||
|
||
|
||
def lazy_combinations(options_list: Sequence[Dict[str, Any]]) -> Iterator[dict]: | ||
""" helper to lazy evaluate possible permutations of possible combinations """ | ||
for i in range(2, len(options_list) + 1): | ||
for combination in combinations(options_list, i): | ||
yield merge_objects(combination) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters