Skip to content

Commit 68955e6

Browse files
committed
separate exclude functions
1 parent 66da277 commit 68955e6

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

src/serializers/fields.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,32 @@ impl SerField {
7575
}
7676
}
7777

78-
fn exclude_default_or_if(
78+
79+
fn exclude_if(
7980
exclude_if_callable: &Option<Py<PyAny>>,
8081
value: &Bound<'_, PyAny>,
81-
extra: &Extra,
8282
serializer: &CombinedSerializer,
8383
) -> PyResult<bool> {
84-
let py = value.py();
85-
8684
if let Some(exclude_if_callable) = exclude_if_callable {
85+
let py = value.py();
8786
let result = exclude_if_callable.call1(py, (value,))?;
8887
let exclude = result.extract::<bool>(py)?;
8988
if exclude {
9089
return Ok(true);
9190
}
9291
}
92+
Ok(false)
93+
}
94+
95+
96+
fn exclude_default(
97+
value: &Bound<'_, PyAny>,
98+
extra: &Extra,
99+
serializer: &CombinedSerializer,
100+
) -> PyResult<bool> {
93101

94102
if extra.exclude_defaults {
95-
if let Some(default) = serializer.get_default(py)? {
103+
if let Some(default) = serializer.get_default(value.py())? {
96104
if value.eq(default)? {
97105
return Ok(true);
98106
}
@@ -195,16 +203,20 @@ impl GeneralFieldsSerializer {
195203
if let Some((next_include, next_exclude)) = self.filter.key_filter(&key, include, exclude)? {
196204
if let Some(field) = op_field {
197205
if let Some(ref serializer) = field.serializer {
198-
if !exclude_default_or_if(&field.exclude_if, &value, &field_extra, serializer)? {
199-
let value = serializer.to_python(
200-
&value,
201-
next_include.as_ref(),
202-
next_exclude.as_ref(),
203-
&field_extra,
204-
)?;
205-
let output_key = field.get_key_py(output_dict.py(), &field_extra);
206-
output_dict.set_item(output_key, value)?;
206+
if exclude_default(&value, &field_extra, serializer)? {
207+
continue;
208+
}
209+
if exclude_if(&field.exclude_if, &value, serializer)? {
210+
continue;
207211
}
212+
let value = serializer.to_python(
213+
&value,
214+
next_include.as_ref(),
215+
next_exclude.as_ref(),
216+
&field_extra,
217+
)?;
218+
let output_key = field.get_key_py(output_dict.py(), &field_extra);
219+
output_dict.set_item(output_key, value)?;
208220
}
209221

210222
if field.required {
@@ -282,19 +294,22 @@ impl GeneralFieldsSerializer {
282294
if let Some((next_include, next_exclude)) = filter {
283295
if let Some(field) = self.fields.get(key_str) {
284296
if let Some(ref serializer) = field.serializer {
285-
if !exclude_default_or_if(&field.exclude_if, &value, &field_extra, serializer)
286-
.map_err(py_err_se_err)?
287-
{
288-
let s = PydanticSerializer::new(
289-
&value,
290-
serializer,
291-
next_include.as_ref(),
292-
next_exclude.as_ref(),
293-
&field_extra,
294-
);
295-
let output_key = field.get_key_json(key_str, &field_extra);
296-
map.serialize_entry(&output_key, &s)?;
297+
if exclude_default(&value, &field_extra, serializer)
298+
.map_err(py_err_se_err)?{
299+
continue;
300+
}
301+
if exclude_if(&field.exclude_if, &value, serializer).map_err(py_err_se_err)? {
302+
continue;
297303
}
304+
let s = PydanticSerializer::new(
305+
&value,
306+
serializer,
307+
next_include.as_ref(),
308+
next_exclude.as_ref(),
309+
&field_extra,
310+
);
311+
let output_key = field.get_key_json(key_str, &field_extra);
312+
map.serialize_entry(&output_key, &s)?;
298313
}
299314
} else if self.mode == FieldsMode::TypedDictAllow {
300315
let output_key = infer_json_key(&key, &field_extra).map_err(py_err_se_err)?;

0 commit comments

Comments
 (0)