Skip to content

Commit

Permalink
feat(csv): add metadata field mapping in CSVMultiObject.
Browse files Browse the repository at this point in the history
refs #123717
  • Loading branch information
Delsin Van Grembergen committed Jun 25, 2024
1 parent 35503e1 commit 3537eaf
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/elody/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,19 @@ def __init__(
index_mapping=None,
object_field_mapping=None,
required_metadata_values=None,
metadata_field_mapping=None,
):
super().__init__(csvstring)
self.index_mapping = dict()
if index_mapping:
self.index_mapping = index_mapping
self.object_field_mapping = dict()
if object_field_mapping:
self.object_field_mapping = object_field_mapping
self.required_metadata_values = required_metadata_values
self.index_mapping = index_mapping if index_mapping else dict()
self.object_field_mapping = (
object_field_mapping if object_field_mapping else dict()
)
self.required_metadata_values = (
required_metadata_values if required_metadata_values else dict()
)
self.metadata_field_mapping = (
metadata_field_mapping if metadata_field_mapping else dict()
)
self.objects = dict()
self.errors = dict()
self.__fill_objects_from_csv()
Expand Down Expand Up @@ -194,10 +198,14 @@ def __fill_objects_from_csv(self):
key not in self.index_mapping.values()
and self.__field_allowed(type, key, value)
):
indexed_dict[type][id].setdefault("metadata", list())
indexed_dict[type][id]["metadata"].append(
self._get_metadata_object(key, value)
)
# Map the metadata field to a unified key if applicable
metadata_info = self.metadata_field_mapping.get(key, {})
if metadata_info.get("target") == type or not metadata_info:
metadata_key = metadata_info.get("map_to", key)
indexed_dict[type][id].setdefault("metadata", list())
indexed_dict[type][id]["metadata"].append(
self._get_metadata_object(metadata_key, value)
)
self.__validate_indexed_dict(indexed_dict)
self.__add_required_fields(indexed_dict)
for object_type, objects in indexed_dict.items():
Expand Down

0 comments on commit 3537eaf

Please sign in to comment.