mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-15 07:18:58 -04:00
Change how merge_jsonlike treats lists
Current behaviour is to check item-against-item
and merge based on whether items match, irrelevant
of position. This doesn't produce the results we
need for our usecases (merging data to send to
GOVUK Frontend components).
We actually want:
- items to be compared based on their position
- new primitive items at the same position to
overwrite existing ones
- dicts or lists at the same position to be merged
For example,
Starting with this list:
[{"name": "option-1", "value": "1"}]
Merging in this list:
[{"hint": {"text": "Choose one option"}}]
You currently get this:
[
{"name": "option-1", "value": "1"},
{"hint": {"text": "Choose one option"}}
]
We want to get this:
[
{
"name": "option-1", "value": "1",
"hint": {"text": "Choose one option"}
}
]
This commit is contained in:
@@ -651,8 +651,13 @@ def merge_jsonlike(source, destination):
|
||||
return True
|
||||
|
||||
def merge_lists(source, destination):
|
||||
for item in destination:
|
||||
if item not in source:
|
||||
last_dest_idx = len(destination) - 1
|
||||
for idx, item in enumerate(destination):
|
||||
if idx <= last_dest_idx:
|
||||
# assign destination value if can't be merged into source
|
||||
if merge_items(source[idx], destination[idx]) is False:
|
||||
source[idx] = destination[idx]
|
||||
else:
|
||||
source.append(item)
|
||||
|
||||
def merge_dicts(source, destination):
|
||||
|
||||
Reference in New Issue
Block a user