Skip to content

Commit

Permalink
feat(utils.dictionary): deep_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed May 7, 2024
1 parent ff67a73 commit be8ff73
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/hyprshade/utils/dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

from typing import Literal, TypeAlias

DeepMergeStrategy: TypeAlias = Literal["force", "keep"]


def __deep_merge_impl(
dest: dict, source: dict, /, *, strategy: DeepMergeStrategy
) -> dict:
for key in source:
if key in dest:
if isinstance(dest[key], dict) and isinstance(source[key], dict):
__deep_merge_impl(dest[key], source[key], strategy=strategy)
elif strategy == "force":
dest[key] = source[key]
else:
dest[key] = source[key]

return dest


def deep_merge(
destination: dict, /, *dicts: dict, strategy: DeepMergeStrategy = "force"
) -> dict:
"""Merge multiple dictionaries recursively.
`destination` will be mutated in place and returned. Items will only
be merged if they are both instances of `dict`; if not, the behavior will be
determined by `strategy`.
If `strategy` is `"force"`, values from the rightmost dictionary will be used.
If `strategy` is `"keep"`, values from the leftmost dictionary will be used.
"""

for d in dicts:
__deep_merge_impl(destination, d, strategy=strategy)

return destination

0 comments on commit be8ff73

Please sign in to comment.