|
| 1 | +use pyo3::intern; |
| 2 | +use pyo3::prelude::*; |
| 3 | +use pyo3::types::{PyDict, PyList, PyString}; |
| 4 | + |
| 5 | +use serde::ser::SerializeMap; |
| 6 | +use serde::Serialize; |
| 7 | + |
| 8 | +use crate::build_tools::SchemaDict; |
| 9 | +use crate::serializers::filter::SchemaFilter; |
| 10 | + |
| 11 | +use super::errors::py_err_se_err; |
| 12 | +use super::infer::{infer_serialize, infer_serialize_known, infer_to_python, infer_to_python_known}; |
| 13 | +use super::ob_type::ObType; |
| 14 | +use super::Extra; |
| 15 | + |
| 16 | +use super::type_serializers::function::get_json_return_type; |
| 17 | + |
| 18 | +#[derive(Debug, Clone)] |
| 19 | +pub(super) struct ComputedFields(Vec<ComputedField>); |
| 20 | + |
| 21 | +impl ComputedFields { |
| 22 | + pub fn new(schema: &PyDict) -> PyResult<Option<Self>> { |
| 23 | + let py = schema.py(); |
| 24 | + if let Some(computed_fields) = schema.get_as::<&PyList>(intern!(py, "computed_fields"))? { |
| 25 | + let computed_fields = computed_fields |
| 26 | + .iter() |
| 27 | + .map(ComputedField::new) |
| 28 | + .collect::<PyResult<Vec<_>>>()?; |
| 29 | + Ok(Some(Self(computed_fields))) |
| 30 | + } else { |
| 31 | + Ok(None) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub fn to_python( |
| 36 | + &self, |
| 37 | + model: &PyAny, |
| 38 | + output_dict: &PyDict, |
| 39 | + filter: &SchemaFilter<isize>, |
| 40 | + include: Option<&PyAny>, |
| 41 | + exclude: Option<&PyAny>, |
| 42 | + extra: &Extra, |
| 43 | + ) -> PyResult<()> { |
| 44 | + for computed_fields in self.0.iter() { |
| 45 | + computed_fields.to_python(model, output_dict, filter, include, exclude, extra)?; |
| 46 | + } |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | + |
| 50 | + pub fn serde_serialize<S: serde::ser::Serializer>( |
| 51 | + &self, |
| 52 | + model: &PyAny, |
| 53 | + map: &mut S::SerializeMap, |
| 54 | + filter: &SchemaFilter<isize>, |
| 55 | + include: Option<&PyAny>, |
| 56 | + exclude: Option<&PyAny>, |
| 57 | + extra: &Extra, |
| 58 | + ) -> Result<(), S::Error> { |
| 59 | + for computed_field in self.0.iter() { |
| 60 | + let property_name_py = computed_field.property_name_py.as_ref(model.py()); |
| 61 | + if let Some((next_include, next_exclude)) = filter |
| 62 | + .key_filter(property_name_py, include, exclude) |
| 63 | + .map_err(py_err_se_err)? |
| 64 | + { |
| 65 | + let cfs = ComputedFieldSerializer { |
| 66 | + model, |
| 67 | + computed_field, |
| 68 | + include: next_include, |
| 69 | + exclude: next_exclude, |
| 70 | + extra, |
| 71 | + }; |
| 72 | + let key = match extra.by_alias { |
| 73 | + true => computed_field.alias.as_str(), |
| 74 | + false => computed_field.property_name.as_str(), |
| 75 | + }; |
| 76 | + map.serialize_entry(key, &cfs)?; |
| 77 | + } |
| 78 | + } |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Debug, Clone)] |
| 84 | +struct ComputedField { |
| 85 | + property_name: String, |
| 86 | + property_name_py: Py<PyString>, |
| 87 | + return_ob_type: Option<ObType>, |
| 88 | + alias: String, |
| 89 | + alias_py: Py<PyString>, |
| 90 | +} |
| 91 | + |
| 92 | +impl ComputedField { |
| 93 | + pub fn new(schema: &PyAny) -> PyResult<Self> { |
| 94 | + let py = schema.py(); |
| 95 | + let schema: &PyDict = schema.downcast()?; |
| 96 | + let property_name: &PyString = schema.get_as_req(intern!(py, "property_name"))?; |
| 97 | + let return_ob_type = get_json_return_type(schema)?; |
| 98 | + let alias_py: &PyString = schema.get_as(intern!(py, "alias"))?.unwrap_or(property_name); |
| 99 | + Ok(Self { |
| 100 | + property_name: property_name.extract()?, |
| 101 | + property_name_py: property_name.into_py(py), |
| 102 | + return_ob_type, |
| 103 | + alias: alias_py.extract()?, |
| 104 | + alias_py: alias_py.into_py(py), |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + fn to_python( |
| 109 | + &self, |
| 110 | + model: &PyAny, |
| 111 | + output_dict: &PyDict, |
| 112 | + filter: &SchemaFilter<isize>, |
| 113 | + include: Option<&PyAny>, |
| 114 | + exclude: Option<&PyAny>, |
| 115 | + extra: &Extra, |
| 116 | + ) -> PyResult<()> { |
| 117 | + let py = model.py(); |
| 118 | + let property_name_py = self.property_name_py.as_ref(py); |
| 119 | + |
| 120 | + if let Some((next_include, next_exclude)) = filter.key_filter(property_name_py, include, exclude)? { |
| 121 | + let next_value = model.getattr(property_name_py)?; |
| 122 | + |
| 123 | + // TODO fix include & exclude |
| 124 | + let value = match self.return_ob_type { |
| 125 | + Some(ref ob_type) => infer_to_python_known(ob_type, next_value, next_include, next_exclude, extra), |
| 126 | + None => infer_to_python(next_value, next_include, next_exclude, extra), |
| 127 | + }?; |
| 128 | + let key = match extra.by_alias { |
| 129 | + true => self.alias_py.as_ref(py), |
| 130 | + false => property_name_py, |
| 131 | + }; |
| 132 | + output_dict.set_item(key, value)?; |
| 133 | + } |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +pub(crate) struct ComputedFieldSerializer<'py> { |
| 139 | + model: &'py PyAny, |
| 140 | + computed_field: &'py ComputedField, |
| 141 | + include: Option<&'py PyAny>, |
| 142 | + exclude: Option<&'py PyAny>, |
| 143 | + extra: &'py Extra<'py>, |
| 144 | +} |
| 145 | + |
| 146 | +impl<'py> Serialize for ComputedFieldSerializer<'py> { |
| 147 | + fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 148 | + let py = self.model.py(); |
| 149 | + let property_name_py = self.computed_field.property_name_py.as_ref(py); |
| 150 | + let next_value = self.model.getattr(property_name_py).map_err(py_err_se_err)?; |
| 151 | + |
| 152 | + match self.computed_field.return_ob_type { |
| 153 | + Some(ref ob_type) => { |
| 154 | + infer_serialize_known(ob_type, next_value, serializer, self.include, self.exclude, self.extra) |
| 155 | + } |
| 156 | + None => infer_serialize(next_value, serializer, self.include, self.exclude, self.extra), |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments