Skip to content

Commit ac36a28

Browse files
author
notactuallyfinn
committed
updated strategies and match functions
1 parent 08619ee commit ac36a28

4 files changed

Lines changed: 659 additions & 469 deletions

File tree

src/hermes/model/merge/action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,6 @@ def merge(
259259
""" FIXME: log error """
260260
break
261261
else:
262-
value.append(item)
262+
value.append(update_item)
263263
# Return the merged values.
264264
return value

src/hermes/model/merge/match.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@
1010
from ..types import ld_dict
1111

1212

13-
def match_equals(a: Any, b: Any) -> bool:
14-
"""
15-
Wrapper method for normal == comparison.
16-
17-
:param a: First item for the comparison.
18-
:type a: Any
19-
:param b: Second item for the comparison.
20-
:type b: Any
21-
22-
:return: Truth value of a == b.
23-
:rtype: bool
24-
"""
25-
return a == b
26-
27-
2813
def match_keys(*keys: list[str], fall_back_to_equals: bool = False) -> Callable[[Any, Any], bool]:
2914
"""
3015
Creates a function taking to parameters that returns true
@@ -81,3 +66,19 @@ def match_person(left: Any, right: Any) -> bool:
8166
mails_right = right["schema:email"]
8267
return any((mail in mails_right) for mail in left["schema:email"])
8368
return left == right
69+
70+
71+
def match_multiple_types(
72+
*functions_for_types: list[tuple[str, Callable[[Any, Any], bool]]],
73+
fall_back_function: Callable[[Any, Any], bool] = match_keys("@id", fall_back_to_equals=True)
74+
) -> Callable[[Any, Any], bool]:
75+
def match_func(left: Any, right: Any) -> bool:
76+
if not ((isinstance(left, ld_dict) and isinstance(right, ld_dict)) and "@type" in left and "@type" in right):
77+
return fall_back_function(left, right)
78+
types_left = left["@type"]
79+
types_right = right["@type"]
80+
for ld_type, func in functions_for_types:
81+
if ld_type in types_left and ld_type in types_right:
82+
return func(left, right)
83+
return fall_back_function(left, right)
84+
return match_func

0 commit comments

Comments
 (0)