Skip to content

Commit

Permalink
Minor _BaseODMModel.__doc refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gsakkis committed Mar 21, 2023
1 parent 9dbc2b8 commit 5b11b67
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions odmantic/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,33 +701,31 @@ def __doc(
for field_name, field in model.__odm_fields__.items():
if include is not None and field_name not in include:
continue

raw_value = raw_doc[field_name]
if isinstance(field, ODMReference):
doc[field.key_name] = raw_doc[field_name][field.model.__primary_field__]
value = raw_value[field.model.__primary_field__]
elif isinstance(field, ODMEmbedded):
doc[field.key_name] = self.__doc(raw_doc[field_name], field.model)
value = self.__doc(raw_value, field.model)
elif isinstance(field, ODMEmbeddedGeneric):
if field.generic_origin is dict:
doc[field.key_name] = {
value = {
item_key: self.__doc(item_value, field.model)
for item_key, item_value in raw_doc[field_name].items()
for item_key, item_value in raw_value.items()
}
elif field.generic_origin in (list, tuple, set):
doc[field.key_name] = [
self.__doc(item, field.model) for item in raw_doc[field_name]
]
value = [self.__doc(item, field.model) for item in raw_value]
elif field.generic_origin is Union: # actually Optional
if raw_doc[field_name] is not None:
doc[field.key_name] = self.__doc(
raw_doc[field_name], field.model
)
if raw_value is not None:
value = self.__doc(raw_value, field.model)
else:
doc[field.key_name] = None
value = None
elif field_name in model.__bson_serialized_fields__:
doc[field.key_name] = model.__fields__[field_name].type_.__bson__(
raw_doc[field_name]
)
value = model.__fields__[field_name].type_.__bson__(raw_value)
else:
doc[field.key_name] = raw_doc[field_name]
value = raw_value

doc[field.key_name] = value

if model.Config.extra == "allow":
extras = set(raw_doc.keys()) - set(model.__odm_fields__.keys())
Expand Down

0 comments on commit 5b11b67

Please sign in to comment.