Skip to content

Commit

Permalink
dumpers: support for different load type
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Panero committed Mar 16, 2022
1 parent bb4448a commit c672bae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
15 changes: 7 additions & 8 deletions invenio_records/dumpers/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _dump_model_field(self, record, model_field_name, dump, dump_key,
dump[dump_key] = self._serialize(val, dump_type)

def _load_model_field(self, record_cls, model_field_name, dump, dump_key,
dump_type):
load_type):
"""Helper method to load model fields from dump.
:param record_cls: The record class being used for loading.
Expand All @@ -169,12 +169,11 @@ def _load_model_field(self, record_cls, model_field_name, dump, dump_key,
return val

# Determine dump data type if not provided
if dump_type is None:
sa_field = getattr(record_cls.model_cls, model_field_name)
dump_type = self._sa_type(record_cls.model_cls, model_field_name)
if load_type is None:
load_type = self._sa_type(record_cls.model_cls, model_field_name)

# Deserialize the value
return self._deserialize(val, dump_type)
return self._deserialize(val, load_type)

@staticmethod
def _iter_modelfields(record_cls):
Expand Down Expand Up @@ -247,15 +246,15 @@ def load(self, dump_data, record_cls):
# Load explicitly defined model fields.
model_data = {}
it = self._model_fields.items()
for model_field_name, (dump_key, dump_type) in it:
for model_field_name, (dump_key, load_type) in it:
model_data[model_field_name] = self._load_model_field(
record_cls, model_field_name, dump_data, dump_key, dump_type)
record_cls, model_field_name, dump_data, dump_key, load_type)

# Load model fields defined as system fields
for systemfield in self._iter_modelfields(record_cls):
model_data[systemfield.model_field_name] = self._load_model_field(
record_cls, systemfield.model_field_name, dump_data,
systemfield.dump_key, systemfield.dump_type)
systemfield.dump_key, systemfield.load_type)

# Initialize model if an id was provided.
if model_data.get('id') is not None:
Expand Down
11 changes: 10 additions & 1 deletion invenio_records/systemfields/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ModelField(SystemField):
"""Model field for providing get and set access on a model field."""

def __init__(self, model_field_name=None, dump=True, dump_key=None,
dump_type=None):
dump_type=None, load_type=None):
"""Initialize the field.
:param model_field_name: Name of field on the database model.
Expand All @@ -29,6 +29,7 @@ def __init__(self, model_field_name=None, dump=True, dump_key=None,
self.dump = dump
self._dump_key = dump_key
self._dump_type = dump_type
self._load_type = load_type

#
# Helpers
Expand Down Expand Up @@ -58,6 +59,14 @@ def dump_type(self):
"""
return self._dump_type

@property
def load_type(self):
"""The data type used to determine how to deserialize the model field.
Defaults to dump_type.
"""
return self._load_type or self._dump_type

def _set(self, model, value):
"""Internal method to set value on the model's field."""
setattr(model, self.model_field_name, value)
Expand Down

0 comments on commit c672bae

Please sign in to comment.