|
10 | 10 | from ..types import ld_dict |
11 | 11 |
|
12 | 12 |
|
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 | | - |
28 | 13 | def match_keys(*keys: list[str], fall_back_to_equals: bool = False) -> Callable[[Any, Any], bool]: |
29 | 14 | """ |
30 | 15 | Creates a function taking to parameters that returns true |
@@ -81,3 +66,19 @@ def match_person(left: Any, right: Any) -> bool: |
81 | 66 | mails_right = right["schema:email"] |
82 | 67 | return any((mail in mails_right) for mail in left["schema:email"]) |
83 | 68 | 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