Skip to content

Commit d956462

Browse files
author
direbearform
committed
include private specified in mapping
1 parent bf0141c commit d956462

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

mapper/object_mapper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
101101

102102
if (type(type_from) is not type):
103103
raise ObjectMapperException(f"type_from must be a type")
104+
104105
if (type(type_to) is not type):
105106
raise ObjectMapperException(f"type_to must be a type")
107+
106108
if (mapping is not None and not isinstance(mapping, Dict)):
107109
raise ObjectMapperException(f"mapping, if provided, must be a Dict type")
108110

@@ -162,7 +164,7 @@ def map(self, from_obj, to_type: type=type(None), ignore_case: bool=False, allow
162164
raise ObjectMapperException(f"No mapping defined for {key_from} to {key_to}")
163165
key_to_cls = self.mappings[key_from][key_to][0]
164166
custom_mappings = self.mappings[key_from][key_to][1]
165-
167+
166168
# Currently, all target class data members need to have default value
167169
# Object with __init__ that carries required non-default arguments are not supported
168170
inst = key_to_cls()
@@ -173,14 +175,14 @@ def not_private(s):
173175
def not_excluded(s):
174176
return not (excluded and s in excluded)
175177

176-
def is_included(s):
177-
return included and s in included
178+
def is_included(s, mapping):
179+
return (included and s in included) or (mapping and s in mapping)
178180

179181
from_obj_attributes = getmembers(from_obj, lambda a: not isroutine(a))
180182
from_obj_dict = {k: v for k, v in from_obj_attributes}
181183

182184
to_obj_attributes = getmembers(inst, lambda a: not isroutine(a))
183-
to_obj_dict = {k: v for k, v in to_obj_attributes if not_excluded(k) and (not_private(k) or is_included(k))}
185+
to_obj_dict = {k: v for k, v in to_obj_attributes if not_excluded(k) and (not_private(k) or is_included(k, custom_mappings))}
184186

185187
if ignore_case:
186188
from_props = CaseDict(from_obj_dict)

tests/object_mapper_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,24 @@ def test_mapping_included_field(self):
382382
self.assertEqual(result._actor_name, from_class._actor_name, "Private is copied if explicitly included")
383383
self.assertNotIn("surname", result.__dict__, "To class must not contain surname")
384384

385+
def test_mapping_included_field_by_mapping(self):
386+
"""Test mapping with included fields by mapping"""
387+
#Arrange
388+
from_class = FromTestClass()
389+
mapper = ObjectMapper()
390+
mapper.create_map(FromTestClass, ToTestClass, mapping={'_actor_name': lambda o: f"{o.name} acted by {o._actor_name}"})
391+
392+
#Act
393+
result = mapper.map(FromTestClass())
394+
395+
#Assert
396+
print(result)
397+
self.assertTrue(isinstance(result, ToTestClass), "Type must be ToTestClass")
398+
self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
399+
self.assertEqual(result.date, from_class.date, "Date mapping must be equal")
400+
self.assertEqual(result._actor_name, f"{from_class.name} acted by {from_class._actor_name}", "Private is copied if explicitly mapped")
401+
self.assertNotIn("surname", result.__dict__, "To class must not contain surname")
402+
385403
def test_mapping_creation_complex_without_mappings_correct(self):
386404
""" Test mapping creation for complex class without mappings """
387405

0 commit comments

Comments
 (0)