Skip to content

Commit 47cafd4

Browse files
committed
fix: apply feedback
1 parent c935440 commit 47cafd4

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

src/serializers/type_serializers/uuid.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

3+
use pyo3::prelude::*;
34
use pyo3::types::PyDict;
4-
use pyo3::{intern, prelude::*};
55

66
use crate::definitions::DefinitionsBuilder;
77

@@ -11,17 +11,7 @@ use super::{
1111
};
1212

1313
pub(crate) fn uuid_to_string(py_uuid: &PyAny) -> PyResult<String> {
14-
let py = py_uuid.py();
15-
let int: i128 = py_uuid.getattr(intern!(py, "int"))?.extract()?;
16-
let hex = format!("{int:032x}");
17-
Ok(format!(
18-
"{}-{}-{}-{}-{}",
19-
&hex[0..8],
20-
&hex[8..12],
21-
&hex[12..16],
22-
&hex[16..20],
23-
&hex[20..]
24-
))
14+
Ok(py_uuid.str()?.to_string())
2515
}
2616

2717
#[derive(Debug, Clone)]

src/validators/uuid.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,28 +148,30 @@ impl UuidValidator {
148148
Err(_) => return Err(ValError::new(ErrorType::UuidType, input)),
149149
},
150150
};
151-
let v1 = uuid.get_version_num();
152-
match self.version {
153-
Some(v2) => {
154-
let v2 = usize::from(v2);
155-
if v1 == v2 {
156-
Ok(uuid)
157-
} else {
158-
Err(ValError::new(
159-
ErrorType::UuidVersionMismatch {
160-
version: v1,
161-
schema_version: v2,
162-
},
163-
input,
164-
))
165-
}
151+
if let Some(expected_version) = self.version {
152+
let v1 = uuid.get_version_num();
153+
let expected_version = usize::from(expected_version);
154+
if v1 != expected_version {
155+
return Err(ValError::new(
156+
ErrorType::UuidVersionMismatch {
157+
version: v1,
158+
schema_version: expected_version,
159+
},
160+
input,
161+
));
166162
}
167-
None => Ok(uuid),
168163
}
164+
165+
Ok(uuid)
169166
}
170167

168+
/// Sets the attributes in a Python dictionary object (`dc`) to represent a UUID.
169+
/// The function converts the UUID to a u128 integer and sets the corresponding attributes
170+
/// in the dictionary object to the converted value and a 'safe' flag.
171+
///
172+
/// This implementation does not use the Python `__init__` function to speed up the process,
173+
/// as the `__init__` function in the Python `uuid` module performs extensive checks.
171174
fn set_dict_call<'data>(&self, py: Python<'data>, dc: &PyAny, uuid: &Uuid) -> ValResult<'data, ()> {
172-
// python uuid use integer
173175
let int = uuid.as_u128();
174176
let safe = py
175177
.import(intern!(py, "uuid"))?

tests/serializers/test_uuid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_uuid_json(value, expected):
5050
assert v.to_json(value).decode() == f'"{expected}"'
5151

5252

53-
def test_any_datetime_key():
53+
def test_any_uuid_key():
5454
v = SchemaSerializer(core_schema.dict_schema())
5555
input_value = {UUID('12345678-1234-1234-1234-567812345678'): 1}
5656

0 commit comments

Comments
 (0)