Skip to content

Commit

Permalink
systemfields: allow omitting key name in field
Browse files Browse the repository at this point in the history
* Adds possibility to not add a key name and instead use the attribute
  name as default.
  • Loading branch information
lnielsen committed Sep 9, 2020
1 parent ff101c5 commit 0dc5707
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
10 changes: 9 additions & 1 deletion invenio_records/systemfields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SystemField(ExtensionMixin):

def __init__(self, key=None):
"""Initialise the field."""
self.key = key
self._key = key
# The attribute is set by __set_name__ which is called by the metaclass
# during construction.
self._attr_name = None
Expand All @@ -77,6 +77,14 @@ def attr_name(self):
"""
return self._attr_name

@property
def key(self):
"""Property to access the dict key name.
Uses the attribute name if the key is not defined.
"""
return self._key or self._attr_name

#
# Data descriptor definition
#
Expand Down
4 changes: 2 additions & 2 deletions invenio_records/systemfields/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
class ConstantField(SystemField):
"""Constant fields add a constant value to a key in the record."""

def __init__(self, key, value):
def __init__(self, key=None, value=''):
"""Initialize the field.
:param key: The key to set in the dictionary (dot notation supported
for nested lookup).
:param value: The value to set for the key.
"""
self.key = key
self.value = value
super().__init__(key=key)

def pre_init(self, record, data, model=None, **kwargs):
"""Sets the key in the record during record instantiation."""
Expand Down
2 changes: 1 addition & 1 deletion invenio_records/systemfields/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DictField(SystemField):
Provides a shortcut for getting/setting a specific key on a record.
"""

def __init__(self, key, clear_none=False, create_if_missing=True):
def __init__(self, key=None, clear_none=False, create_if_missing=True):
"""Initialise the dict field.
:param key: Key to set (dot notation supported).
Expand Down
16 changes: 16 additions & 0 deletions tests/test_systemfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ class TestRecord(Record, SystemFieldsMixin):
assert record['arg_value'] == 'testval'


def test_default_key(testapp):
"""Test default key name."""
class TestRecord(Record, SystemFieldsMixin):
afield = ConstantField(value='test')

assert TestRecord({})['afield'] == 'test'


#
# DictField
#
Expand All @@ -316,6 +324,14 @@ class Record1(Record, SystemFieldsMixin):
metadata={'title': 1}) == {'metadata': {'title': 1}, 'a': 'b'}


def test_dictfield_default_key(testapp):
"""Test default key name."""
class TestRecord(Record, SystemFieldsMixin):
afield = DictField()

assert TestRecord({}, afield={'test': 1})['afield'] == {'test': 1}


def test_dict_field_complex_key():
"""More complex tests for the DictField."""
class Record1(Record, SystemFieldsMixin):
Expand Down

0 comments on commit 0dc5707

Please sign in to comment.