deserializing lists
This commit is contained in:
@@ -4,7 +4,7 @@ use crate::{
|
||||
};
|
||||
use prost_reflect::{
|
||||
prost_types::{
|
||||
field_descriptor_proto::Type, DescriptorProto, FieldDescriptorProto, FileDescriptorProto,
|
||||
field_descriptor_proto::{Type, Label}, DescriptorProto, FieldDescriptorProto, FileDescriptorProto,
|
||||
},
|
||||
DescriptorPool, MessageDescriptor,
|
||||
};
|
||||
@@ -65,6 +65,10 @@ fn create_cached_descriptor_in_pool<'py>(
|
||||
create_cached_descriptor_in_pool(instance, pool)?;
|
||||
}
|
||||
|
||||
if meta.is_list_field(field_name)? {
|
||||
field.set_label(Label::Repeated);
|
||||
}
|
||||
|
||||
field
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::{
|
||||
py_any_extras::PyAnyExtras,
|
||||
};
|
||||
use prost_reflect::{DynamicMessage, ReflectMessage, Value};
|
||||
use pyo3::{PyAny, ToPyObject, PyObject};
|
||||
use pyo3::{PyAny, PyObject, ToPyObject};
|
||||
|
||||
pub fn merge_msg_into_pyobj(obj: &PyAny, msg: &DynamicMessage) -> Result<()> {
|
||||
for field in msg.descriptor().fields() {
|
||||
@@ -19,11 +19,7 @@ pub fn merge_msg_into_pyobj(obj: &PyAny, msg: &DynamicMessage) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn map_field_value(
|
||||
field_name: &str,
|
||||
field_value: &Value,
|
||||
proto_meta: &PyAny,
|
||||
) -> Result<PyObject> {
|
||||
fn map_field_value(field_name: &str, field_value: &Value, proto_meta: &PyAny) -> Result<PyObject> {
|
||||
let py = proto_meta.py();
|
||||
match field_value {
|
||||
Value::Bool(x) => Ok(x.to_object(py)),
|
||||
@@ -40,6 +36,11 @@ fn map_field_value(
|
||||
merge_msg_into_pyobj(obj, msg)?;
|
||||
Ok(obj.to_object(py))
|
||||
}
|
||||
Value::List(ls) => Ok(ls
|
||||
.iter()
|
||||
.map(|x| map_field_value(field_name, x, proto_meta))
|
||||
.collect::<Result<Vec<PyObject>>>()?
|
||||
.to_object(py)),
|
||||
value => Err(Error::UnsupportedType(value.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ pub trait PyAnyExtras<'py> {
|
||||
fn qualified_class_name(&self) -> Result<String>;
|
||||
fn get_proto_meta(&self) -> Result<&'py PyAny>;
|
||||
fn create_instance(&self, field_name: &str) -> Result<&'py PyAny>;
|
||||
fn is_list_field(&self, field_name: &str) -> Result<bool>;
|
||||
}
|
||||
|
||||
impl<'py> PyAnyExtras<'py> for &'py PyAny {
|
||||
@@ -26,4 +27,11 @@ impl<'py> PyAnyExtras<'py> for &'py PyAny {
|
||||
.call0()?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn is_list_field(&self, field_name: &str) -> Result<bool> {
|
||||
let cls = self.getattr("default_gen")?.get_item(field_name)?;
|
||||
let module = cls.getattr("__module__")?;
|
||||
let name = cls.getattr("__name__")?;
|
||||
Ok(module.to_string() == "builtins" && name.to_string() == "list")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user