You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to implement a type that has a data field of type serde_json::Value which should be an object, that can be inserted into any composite type so long as the name and fields match.
Now I took the generated ToSql implementation of the derive macro that normally should work for a struct with named fields and tried to adapt it, but it keeps giving the error: db error: ERROR: insufficient data left in message, and I'm not sure what the problem is.
The ToSql implementation is as follows:
implToSqlforCompositeType{fnto_sql(&self,ty:&Type,buf:&mutBytesMut) -> Result<IsNull,Box<dynError + Sync + Send>>whereSelf:Sized{// Make sure our data is an objectlet data = match&self.data{
serde_json::Value::Object(data) => data,
_ => returnErr(format!("(@impl ToSql for CompositeType) Expected data to be an object, got: {:?}", self.data).into())};// Make sure it is a composite type we're inserting intolet fields = match ty.kind(){&Kind::Composite(ref fields) => {// Make sure the name matchesif ty.name() != self.composite_name{returnErr(format!("(@impl ToSql for CompositeType) Type name mismatch, expected: {}, got: {}", ty.name(), self.composite_name).into());}
fields
},
kind => returnErr(format!("(@impl ToSql for CompositeType) Unsupported type: {:?}, can only insert into composite types", kind).into())};// This is copied from the derive macro
buf.extend_from_slice(&(fields.len()asi32).to_be_bytes());for field in fields {
buf.extend_from_slice(&field.type_().oid().to_be_bytes());let base = buf.len();
buf.extend_from_slice(&[0;4]);// Only this line is different, no more matching field names, just get the datalet r = postgres_types::ToSql::to_sql(&self.data.get(field.name()), field.type_(), buf);let count = match r? {
postgres_types::IsNull::Yes => -1,
postgres_types::IsNull::No => {let len = buf.len() - base - 4;if len > i32::max_value()asusize{return std::result::Result::Err(std::convert::Into::into("value too large to transmit"));}
len asi32}};
buf[base..base + 4].copy_from_slice(&count.to_be_bytes());}
std::result::Result::Ok(postgres_types::IsNull::No)}fnaccepts(ty:&Type) -> boolwhereSelf:Sized{true}to_sql_checked!();}
The text was updated successfully, but these errors were encountered:
I think I may have found the issue, a serde_json::Value containing a number encodes as b"5", while a i32 encodes as b"\0\0\0\x05". So I guess I'll have to do a nice match statement to cast data before encoding it
I'm trying to implement a type that has a
data
field of typeserde_json::Value
which should be an object, that can be inserted into any composite type so long as the name and fields match.So if for example I have a composite type
An instance of
CompositeType
like the following should be allowed to be inserted there:Now I took the generated
ToSql
implementation of the derive macro that normally should work for a struct with named fields and tried to adapt it, but it keeps giving the error:db error: ERROR: insufficient data left in message
, and I'm not sure what the problem is.The
ToSql
implementation is as follows:The text was updated successfully, but these errors were encountered: