From fedc36d3af29264520182ac6a5d78282aa473b31 Mon Sep 17 00:00:00 2001 From: "chandr-andr (Kiselev Aleksandr)" Date: Sat, 21 Sep 2024 13:52:29 +0200 Subject: [PATCH] Fixed runtime error Signed-off-by: chandr-andr (Kiselev Aleksandr) --- python/tests/test_value_converter.py | 19 +++++ src/value_converter.rs | 108 ++++++++++++++------------- 2 files changed, 77 insertions(+), 50 deletions(-) diff --git a/python/tests/test_value_converter.py b/python/tests/test_value_converter.py index 710e1c1..8fe3199 100644 --- a/python/tests/test_value_converter.py +++ b/python/tests/test_value_converter.py @@ -1001,3 +1001,22 @@ async def test_incorrect_dimensions_array( ], ], ) + + +async def test_empty_array( + psql_pool: ConnectionPool, +) -> None: + await psql_pool.execute("DROP TABLE IF EXISTS test_earr") + await psql_pool.execute( + "CREATE TABLE test_earr (id serial NOT NULL PRIMARY KEY, e_array text[] NOT NULL DEFAULT array[]::text[])", + ) + + await psql_pool.execute("INSERT INTO test_earr(id) VALUES(2);") + + res = await psql_pool.execute( + "SELECT * FROM test_earr WHERE id = 2", + ) + + json_result = res.result() + assert json_result + assert not json_result[0]["e_array"] diff --git a/src/value_converter.rs b/src/value_converter.rs index 0889198..2c46722 100644 --- a/src/value_converter.rs +++ b/src/value_converter.rs @@ -835,35 +835,39 @@ fn _pythondto_array_to_serde( dimension_index: usize, mut lower_bound: usize, ) -> RustPSQLDriverPyResult { - let current_dimension = dimensions.get(dimension_index).unwrap(); - - let possible_next_dimension = dimensions.get(dimension_index + 1); - match possible_next_dimension { - Some(next_dimension) => { - let mut final_list: Value = Value::Array(vec![]); - - for _ in 0..current_dimension.len as usize { - if dimensions.get(dimension_index + 1).is_some() { - let inner_pylist = _pythondto_array_to_serde( - dimensions, - &data[lower_bound..next_dimension.len as usize + lower_bound], - dimension_index + 1, - 0, - )?; - match final_list { - Value::Array(ref mut array) => array.push(inner_pylist), - _ => unreachable!(), - } - lower_bound += next_dimension.len as usize; - }; - } + let current_dimension = dimensions.get(dimension_index); + + if let Some(current_dimension) = current_dimension { + let possible_next_dimension = dimensions.get(dimension_index + 1); + match possible_next_dimension { + Some(next_dimension) => { + let mut final_list: Value = Value::Array(vec![]); + + for _ in 0..current_dimension.len as usize { + if dimensions.get(dimension_index + 1).is_some() { + let inner_pylist = _pythondto_array_to_serde( + dimensions, + &data[lower_bound..next_dimension.len as usize + lower_bound], + dimension_index + 1, + 0, + )?; + match final_list { + Value::Array(ref mut array) => array.push(inner_pylist), + _ => unreachable!(), + } + lower_bound += next_dimension.len as usize; + }; + } - Ok(final_list) - } - None => { - return data.iter().map(|x| x.to_serde_value()).collect(); + return Ok(final_list); + } + None => { + return data.iter().map(|x| x.to_serde_value()).collect(); + } } } + + Ok(Value::Array(vec![])) } /// Convert rust array to python list. @@ -899,33 +903,37 @@ fn _postgres_array_to_py( where T: ToPyObject, { - let current_dimension = dimensions.get(dimension_index).unwrap(); - - let possible_next_dimension = dimensions.get(dimension_index + 1); - match possible_next_dimension { - Some(next_dimension) => { - let final_list = PyList::empty_bound(py); - - for _ in 0..current_dimension.len as usize { - if dimensions.get(dimension_index + 1).is_some() { - let inner_pylist = _postgres_array_to_py( - py, - dimensions, - &data[lower_bound..next_dimension.len as usize + lower_bound], - dimension_index + 1, - 0, - ); - final_list.append(inner_pylist).unwrap(); - lower_bound += next_dimension.len as usize; - }; - } + let current_dimension = dimensions.get(dimension_index); + + if let Some(current_dimension) = current_dimension { + let possible_next_dimension = dimensions.get(dimension_index + 1); + match possible_next_dimension { + Some(next_dimension) => { + let final_list = PyList::empty_bound(py); + + for _ in 0..current_dimension.len as usize { + if dimensions.get(dimension_index + 1).is_some() { + let inner_pylist = _postgres_array_to_py( + py, + dimensions, + &data[lower_bound..next_dimension.len as usize + lower_bound], + dimension_index + 1, + 0, + ); + final_list.append(inner_pylist).unwrap(); + lower_bound += next_dimension.len as usize; + }; + } - final_list.unbind() - } - None => { - return PyList::new_bound(py, data).unbind(); + return final_list.unbind(); + } + None => { + return PyList::new_bound(py, data).unbind(); + } } } + + return PyList::empty_bound(py).unbind(); } #[allow(clippy::too_many_lines)]