@@ -8,8 +8,8 @@ use bytes::{BufMut, BytesMut};
88use postgres_protocol:: types;
99use pyo3:: {
1010 types:: {
11- PyAnyMethods , PyBool , PyBytes , PyDate , PyDateTime , PyDict , PyFloat , PyInt , PyList , PySet ,
12- PyString , PyTime , PyTuple ,
11+ PyAnyMethods , PyBool , PyBytes , PyDate , PyDateTime , PyDict , PyDictMethods , PyFloat , PyInt ,
12+ PyList , PySet , PyString , PyTime , PyTuple ,
1313 } ,
1414 Bound , Py , PyAny , Python , ToPyObject ,
1515} ;
@@ -336,9 +336,8 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
336336
337337 let mut serde_map: Map < String , Value > = Map :: new ( ) ;
338338
339- for dict_item in dict. iter ( ) . unwrap ( ) {
340- let dict_elem = dict_item. unwrap ( ) ;
341- let py_list = dict_elem. downcast :: < PyTuple > ( ) . map_err ( |error| {
339+ for dict_item in dict. items ( ) {
340+ let py_list = dict_item. downcast :: < PyTuple > ( ) . map_err ( |error| {
342341 RustPSQLDriverError :: PyToRustValueConversionError ( format ! (
343342 "Cannot cast to list: {error}"
344343 ) )
@@ -528,34 +527,37 @@ pub fn postgres_to_py(
528527///
529528/// # Errors
530529/// May return error if cannot convert Python type into Rust one.
531- pub fn build_serde_value ( value : & PyAny ) -> RustPSQLDriverPyResult < Value > {
532- Ok ( json ! ( 123 ) )
533- // if value.is_instance_of::<PyList>() {
534- // let mut result_vec: Vec<Value> = vec![];
535-
536- // let params: Vec<&PyAny> = value.extract::<Vec<&PyAny>>()?;
537-
538- // for inner in ¶ms {
539- // if inner.is_instance_of::<PyDict>() {
540- // let python_dto = py_to_rust(inner)?;
541- // result_vec.push(python_dto.to_serde_value()?);
542- // } else if inner.is_instance_of::<PyList>() {
543- // let serde_value = build_serde_value(inner)?;
544- // result_vec.push(serde_value);
545- // } else {
546- // return Err(RustPSQLDriverError::PyToRustValueConversionError(
547- // "PyJSON supports only list of lists or list of dicts.".to_string(),
548- // ));
549- // }
550- // }
551- // Ok(json!(result_vec))
552- // } else if value.is_instance_of::<PyDict>() {
553- // return py_to_rust(value)?.to_serde_value();
554- // } else {
555- // return Err(RustPSQLDriverError::PyToRustValueConversionError(
556- // "PyJSON must be list value.".to_string(),
557- // ));
558- // }
530+ pub fn build_serde_value ( value : Py < PyAny > ) -> RustPSQLDriverPyResult < Value > {
531+ Python :: with_gil ( |gil| {
532+ let bind_value = value. bind ( gil) ;
533+ if bind_value. is_instance_of :: < PyList > ( ) {
534+ let mut result_vec: Vec < Value > = vec ! [ ] ;
535+
536+ let params = bind_value. extract :: < Vec < Py < PyAny > > > ( ) ?;
537+
538+ for inner in params {
539+ let inner_bind = inner. bind ( gil) ;
540+ if inner_bind. is_instance_of :: < PyDict > ( ) {
541+ let python_dto = py_to_rust ( inner_bind) ?;
542+ result_vec. push ( python_dto. to_serde_value ( ) ?) ;
543+ } else if inner_bind. is_instance_of :: < PyList > ( ) {
544+ let serde_value = build_serde_value ( inner) ?;
545+ result_vec. push ( serde_value) ;
546+ } else {
547+ return Err ( RustPSQLDriverError :: PyToRustValueConversionError (
548+ "PyJSON supports only list of lists or list of dicts." . to_string ( ) ,
549+ ) ) ;
550+ }
551+ }
552+ Ok ( json ! ( result_vec) )
553+ } else if bind_value. is_instance_of :: < PyDict > ( ) {
554+ return py_to_rust ( bind_value) ?. to_serde_value ( ) ;
555+ } else {
556+ return Err ( RustPSQLDriverError :: PyToRustValueConversionError (
557+ "PyJSON must be list value." . to_string ( ) ,
558+ ) ) ;
559+ }
560+ } )
559561}
560562
561563/// Convert serde `Value` into Python object.
0 commit comments