@@ -120,14 +120,15 @@ def create_map(self, type_from: type, type_to: type, mapping: Dict =None):
120
120
self .mappings [key_from ][key_to ] = (type_to , mapping )
121
121
122
122
123
- def map (self , from_obj , to_type : type = type (None ), ignore_case = False , allow_none = False , excluded = None , allow_unmapped = False ):
123
+ def map (self , from_obj , to_type : type = type (None ), ignore_case : bool = False , allow_none : bool = False , excluded : List [ str ] = None , included : List [ str ] = None , allow_unmapped : bool = False ):
124
124
"""Method for creating target object instance
125
125
126
126
:param from_obj: source object to be mapped from
127
127
:param to_type: target type
128
128
:param ignore_case: if set to true, ignores attribute case when performing the mapping
129
129
:param allow_none: if set to true, returns None if the source object is None; otherwise throws an exception
130
130
:param excluded: A list of fields to exclude when performing the mapping
131
+ :param included: A list of fields to force inclusion when performing the mapping
131
132
:param allow_unmapped: if set to true, copy over the non-primitive object that didn't have a mapping defined; otherwise exception
132
133
133
134
:return: Instance of the target class with mapped attributes
@@ -172,12 +173,14 @@ def not_private(s):
172
173
def not_excluded (s ):
173
174
return not (excluded and s in excluded )
174
175
176
+ def is_included (s ):
177
+ return included and s in included
178
+
175
179
from_obj_attributes = getmembers (from_obj , lambda a : not isroutine (a ))
176
- from_obj_dict = {k : v for k , v in from_obj_attributes
177
- if not_private (k ) and not_excluded (k )}
180
+ from_obj_dict = {k : v for k , v in from_obj_attributes }
178
181
179
182
to_obj_attributes = getmembers (inst , lambda a : not isroutine (a ))
180
- to_obj_dict = {k : v for k , v in to_obj_attributes if not_private (k )}
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 ) )}
181
184
182
185
if ignore_case :
183
186
from_props = CaseDict (from_obj_dict )
@@ -192,7 +195,7 @@ def map_obj(o, allow_unmapped):
192
195
key_from_child = f"{ key_from_child_cls .__module__ } .{ key_from_child_cls .__name__ } "
193
196
if (key_from_child in self .mappings ):
194
197
# if key_to has a mapping defined, nests the map() call
195
- return self .map (o , type (None ), ignore_case , allow_none , excluded , allow_unmapped )
198
+ return self .map (o , type (None ), ignore_case , allow_none , excluded , included , allow_unmapped )
196
199
elif (key_from_child_cls in ObjectMapper .primitive_types ):
197
200
# allow primitive types without mapping
198
201
return o
@@ -209,7 +212,7 @@ def map_obj(o, allow_unmapped):
209
212
210
213
val = None
211
214
suppress_mapping = False
212
-
215
+
213
216
# mapping function take precedence over complex type mapping
214
217
if custom_mappings is not None and prop in custom_mappings :
215
218
try :
0 commit comments