|
| 1 | +use pyo3::prelude::*; |
| 2 | +use pyo3::types::PyDict; |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +/// Build a reverse mapping from alias -> field_name given a forward mapping |
| 6 | +/// of field_name -> alias. If a field has no alias (alias == field_name), |
| 7 | +/// it still gets an identity entry. |
| 8 | +/// |
| 9 | +/// This is called once per model class and cached. |
| 10 | +#[pyfunction] |
| 11 | +pub fn build_reverse_alias_map<'py>( |
| 12 | + py: Python<'py>, |
| 13 | + field_alias_map: &Bound<'py, PyDict>, |
| 14 | +) -> PyResult<Bound<'py, PyDict>> { |
| 15 | + let result = PyDict::new(py); |
| 16 | + for (key, value) in field_alias_map.iter() { |
| 17 | + let field_name: String = key.extract()?; |
| 18 | + let alias: String = value.extract()?; |
| 19 | + // Map alias -> field_name |
| 20 | + result.set_item(&alias, &field_name)?; |
| 21 | + // Also keep field_name -> field_name for identity lookups |
| 22 | + if alias != field_name { |
| 23 | + result.set_item(&field_name, &field_name)?; |
| 24 | + } |
| 25 | + } |
| 26 | + Ok(result) |
| 27 | +} |
| 28 | + |
| 29 | +/// Translate dict keys from field names to their database aliases. |
| 30 | +/// Takes the dict to translate and a field_name->alias mapping. |
| 31 | +/// Returns a new dict with aliased keys. |
| 32 | +#[pyfunction] |
| 33 | +pub fn translate_columns_to_aliases<'py>( |
| 34 | + py: Python<'py>, |
| 35 | + new_kwargs: &Bound<'py, PyDict>, |
| 36 | + field_to_alias: &Bound<'py, PyDict>, |
| 37 | +) -> PyResult<Bound<'py, PyDict>> { |
| 38 | + let result = PyDict::new(py); |
| 39 | + // Pre-extract the mapping into a Rust HashMap for fast lookup |
| 40 | + let mut alias_map: HashMap<String, String> = HashMap::new(); |
| 41 | + for (k, v) in field_to_alias.iter() { |
| 42 | + let key: String = k.extract()?; |
| 43 | + let val: String = v.extract()?; |
| 44 | + alias_map.insert(key, val); |
| 45 | + } |
| 46 | + |
| 47 | + for (key, value) in new_kwargs.iter() { |
| 48 | + let field_name: String = key.extract()?; |
| 49 | + if let Some(alias) = alias_map.get(&field_name) { |
| 50 | + result.set_item(alias, value)?; |
| 51 | + } else { |
| 52 | + result.set_item(key, value)?; |
| 53 | + } |
| 54 | + } |
| 55 | + Ok(result) |
| 56 | +} |
| 57 | + |
| 58 | +/// Translate dict keys from database aliases to field names. |
| 59 | +/// Takes the dict to translate and an alias->field_name mapping. |
| 60 | +/// Returns a new dict with field name keys. |
| 61 | +#[pyfunction] |
| 62 | +pub fn translate_aliases_to_columns<'py>( |
| 63 | + py: Python<'py>, |
| 64 | + new_kwargs: &Bound<'py, PyDict>, |
| 65 | + alias_to_field: &Bound<'py, PyDict>, |
| 66 | +) -> PyResult<Bound<'py, PyDict>> { |
| 67 | + let result = PyDict::new(py); |
| 68 | + for (key, value) in new_kwargs.iter() { |
| 69 | + if let Some(field_name) = alias_to_field.get_item(&key)? { |
| 70 | + result.set_item(field_name, value)?; |
| 71 | + } else { |
| 72 | + result.set_item(key, value)?; |
| 73 | + } |
| 74 | + } |
| 75 | + Ok(result) |
| 76 | +} |
0 commit comments