in modular_sdk/models/pynamodb_extension/base_safe_update_model.py [0:0]
def _retrieve_additional_data(cls, document: dict,
attributes: Dict[str, Attribute]) -> dict:
"""
Additional data represents those attributes that are not defined
in Python model, but the do exist in DB. It includes all the nested
mappings and lists of mappings
:param document: raw data from DynamoDB
:param attributes: result of instance.get_attributes()
:return:
{
"not_defined_attr": "value",
"partly_defined_mapping: {
"not_defined_attr": "value"
},
"partly_defined_list": [
{},
{},
{
"not_defined_attr": "value"
}
]
}
"""
name_to_instance = {
attr.attr_name: attr for attr in attributes.values()
}
additional_data = {}
for key, value in document.items():
if key not in name_to_instance: # not defined in model
additional_data[key] = value
continue
# key in value
attr = name_to_instance[key]
if isinstance(attr, MapAttribute) and type(attr) != MapAttribute:
additional_data[key] = cls._retrieve_additional_data(
value or {}, attr.get_attributes()
)
elif isinstance(attr, ListAttribute) and \
attr.element_type and \
issubclass(attr.element_type, MapAttribute) and \
attr.element_type != MapAttribute:
inner_attributes = attr.element_type.get_attributes()
additional_data[key] = [
cls._retrieve_additional_data(v or {}, inner_attributes)
for v in value
]
# else:
# pass
return additional_data