@@ -28,7 +28,7 @@ def merge(
2828 self : Self ,
2929 target : ld_merge_dict ,
3030 key : list [Union [str , int ]],
31- value : ld_merge_list ,
31+ value : Union [ ld_merge_list , str ] ,
3232 update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
3333 ) -> Union [JSON_LD_VALUE , BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]:
3434 """
@@ -39,7 +39,7 @@ def merge(
3939 target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
4040 key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
4141 parent of ``target`` out_parent ``out_parent[key[0]]...[key[-1]]`` results in ``value``.
42- value (ld_merge_list): The value inside ``target`` that is to be merged with ``update``.
42+ value (ld_merge_list | str ): The value inside ``target`` that is to be merged with ``update``.
4343 update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target``
4444 with ``value``.
4545
@@ -56,7 +56,7 @@ def merge(
5656 self : Self ,
5757 target : ld_merge_dict ,
5858 key : list [Union [str , int ]],
59- value : ld_merge_list ,
59+ value : Union [ ld_merge_list , str ] ,
6060 update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
6161 ) -> ld_merge_list :
6262 """
@@ -67,16 +67,17 @@ def merge(
6767 target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
6868 key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
6969 parent of ``target`` out_parent ``out_parent[key[0]]...[key[-1]]`` results in ``value``.
70- value (ld_merge_list): The value inside ``target`` that is to be merged with ``update``.
70+ value (ld_merge_list | str ): The value inside ``target`` that is to be merged with ``update``.
7171 This value won't be changed.
7272 update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target`` with
7373 ``value``. This value will be rejected.
7474
7575 Returns:
76- ld_merge_list: The merged value. This value will always be ``value``.
76+ ld_merge_list | str : The merged value. This value will always be ``value``.
7777 """
78- # Add the entry that data has been rejected.
79- target .reject (key , update )
78+ if value != update :
79+ # Add the entry that data has been rejected.
80+ target .reject (key , update )
8081 # Return value unchanged.
8182 return value
8283
@@ -87,7 +88,7 @@ def merge(
8788 self : Self ,
8889 target : ld_merge_dict ,
8990 key : list [Union [str , int ]],
90- value : ld_merge_list ,
91+ value : Union [ ld_merge_list , str ] ,
9192 update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
9293 ) -> Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]:
9394 """
@@ -98,16 +99,17 @@ def merge(
9899 target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
99100 key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
100101 parent of ``target`` out_parent ``out_parent[key[0]]...[key[-1]]`` results in ``value``.
101- value (ld_merge_list): The value inside ``target`` that is to be merged with ``update``.
102+ value (ld_merge_list | str ): The value inside ``target`` that is to be merged with ``update``.
102103 This value will bew replaced.
103104 update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target`` with
104105 ``value``. This value will be used instead of ``value``.
105106
106107 Returns:
107108 BASIC_TYPE | TIME_TYPE | ld_dict | ld_list: The merged value. This value will be ``update``.
108109 """
109- # Add the entry that data has been replaced.
110- target .replace (key , value )
110+ if value != update :
111+ # Add the entry that data has been replaced.
112+ target .replace (key , value )
111113 # Return the new value.
112114 return update
113115
@@ -118,7 +120,7 @@ def merge(
118120 self : Self ,
119121 target : ld_merge_dict ,
120122 key : list [Union [str , int ]],
121- value : ld_merge_list ,
123+ value : Union [ ld_merge_list , str ] ,
122124 update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
123125 ) -> ld_merge_list :
124126 """
@@ -128,12 +130,12 @@ def merge(
128130 target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
129131 key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
130132 parent of ``target`` out_parent ``out_parent[key[0]]...[key[-1]]`` results in ``value``.
131- value (ld_merge_list): The value inside ``target`` that is to be merged with ``update``.
133+ value (ld_merge_list | str ): The value inside ``target`` that is to be merged with ``update``.
132134 update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target``
133135 with ``value``.
134136
135137 Returns:
136- ld_merge_list: The merged value (``value`` concatenated with ``update``).
138+ ld_merge_list | str : The merged value (``value`` concatenated with ``update``).
137139 """
138140 # Concatenate the items and return the result.
139141 if isinstance (update , (list , ld_list )):
@@ -173,7 +175,7 @@ def merge(
173175 self : Self ,
174176 target : ld_merge_dict ,
175177 key : list [Union [str , int ]],
176- value : ld_merge_list ,
178+ value : Union [ ld_merge_list , str ] ,
177179 update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
178180 ) -> ld_merge_list :
179181 """
@@ -183,12 +185,12 @@ def merge(
183185 target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
184186 key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
185187 parent of ``target`` out_parent ``out_parent[key[0]]...[key[-1]]`` results in ``value``.
186- value (ld_merge_list): The value inside ``target`` that is to be merged with ``update``.
188+ value (ld_merge_list | str ): The value inside ``target`` that is to be merged with ``update``.
187189 update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target``
188190 with ``value``.
189191
190192 Returns:
191- ld_merge_list: The merged value.
193+ ld_merge_list | str : The merged value.
192194 """
193195 if not isinstance (update , (list , ld_list )):
194196 update = [update ]
@@ -235,7 +237,7 @@ def merge(
235237 self : Self ,
236238 target : ld_merge_dict ,
237239 key : list [Union [str , int ]],
238- value : ld_merge_list ,
240+ value : Union [ ld_merge_list , str ] ,
239241 update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
240242 ) -> ld_merge_list :
241243 """
@@ -245,12 +247,12 @@ def merge(
245247 target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
246248 key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
247249 parent of ``target`` out_parent out_parent[key[0]]...[key[-1]] results in ``value``.
248- value (ld_merge_list): The value inside ``target`` that is to be merged with ``update``.
250+ value (ld_merge_list | str ): The value inside ``target`` that is to be merged with ``update``.
249251 update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target``
250252 with ``value``.
251253
252254 Returns:
253- ld_merge_list: The merged value.
255+ ld_merge_list | str : The merged value.
254256 """
255257 if not isinstance (update , (list , ld_list )):
256258 update = [update ]
@@ -272,3 +274,32 @@ def merge(
272274 value .append (update_item )
273275 # Return the merged values.
274276 return value
277+
278+ class IdMerge (MergeAction ):
279+ """ :class:`MergeAction` providing a merge function for merging ids, i.e. error if not equals else do nothing. """
280+ def merge (
281+ self : Self ,
282+ target : ld_merge_dict ,
283+ key : list [Union [str , int ]],
284+ value : Union [ld_merge_list , str ],
285+ update : Union [BASIC_TYPE , TIME_TYPE , ld_dict , ld_list ]
286+ ) -> ld_merge_list :
287+ """
288+ Error if value != update or key != "@id". Else do nothing.
289+
290+ Args:
291+ target (ld_merge_dict): The ld_merge_dict inside of which the items are merged.
292+ key (list[str | int]): The "path" of keys so that ``target[key[-1]]`` is ``value`` and for the outermost
293+ parent of ``target`` out_parent out_parent[key[0]]...[key[-1]] results in ``value``.
294+ value (ld_merge_list | str): The value inside ``target`` that is to be merged with ``update``.
295+ update (BASIC_TYPE | TIME_TYPE | ld_dict | ld_list): The value that is to be merged into ``target``
296+ with ``value``.
297+
298+ Returns:
299+ ld_merge_list | str: The merged value.
300+ """
301+ if key [- 1 ] != "@id" :
302+ raise MergeError ("Can't merge non-'@id' values." )
303+ if value != update :
304+ raise MergeError ("Two different '@id' values are merged into the same object." )
305+ return value
0 commit comments