Ensure Value::Bytes serialises correctly (#1757)

This commit is contained in:
Rushmore Mushambi 2023-04-02 14:24:43 +02:00 committed by GitHub
parent 3b7a3a5a35
commit 65f1957256
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 7 deletions

19
lib/src/sql/bytes.rs Normal file
View file

@ -0,0 +1,19 @@
use crate::sql::serde::is_internal_serialization;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Deserialize, Hash)]
pub struct Bytes(pub(super) Vec<u8>);
impl Serialize for Bytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if is_internal_serialization() {
serializer.serialize_bytes(&self.0)
} else {
serializer.serialize_none()
}
}
}

View file

@ -4,6 +4,7 @@ pub(crate) mod algorithm;
pub(crate) mod array; pub(crate) mod array;
pub(crate) mod base; pub(crate) mod base;
pub(crate) mod block; pub(crate) mod block;
pub(crate) mod bytes;
pub(crate) mod comment; pub(crate) mod comment;
pub(crate) mod common; pub(crate) mod common;
pub(crate) mod cond; pub(crate) mod cond;
@ -72,6 +73,7 @@ pub use self::algorithm::Algorithm;
pub use self::array::Array; pub use self::array::Array;
pub use self::base::Base; pub use self::base::Base;
pub use self::block::Block; pub use self::block::Block;
pub use self::bytes::Bytes;
pub use self::cond::Cond; pub use self::cond::Cond;
pub use self::data::Data; pub use self::data::Data;
pub use self::datetime::Datetime; pub use self::datetime::Datetime;

View file

@ -10,6 +10,7 @@ use crate::sql::serde::serialize_internal;
use crate::sql::value::serde::ser; use crate::sql::value::serde::ser;
use crate::sql::value::Value; use crate::sql::value::Value;
use crate::sql::Block; use crate::sql::Block;
use crate::sql::Bytes;
use crate::sql::Datetime; use crate::sql::Datetime;
use crate::sql::Duration; use crate::sql::Duration;
use crate::sql::Future; use crate::sql::Future;
@ -155,12 +156,8 @@ impl ser::Serializer for Serializer {
Ok(value.into()) Ok(value.into())
} }
fn serialize_bytes(self, values: &[u8]) -> Result<Self::Ok, Error> { fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Error> {
let mut vec = Vec::with_capacity(values.len()); Ok(Value::Bytes(Bytes(value.to_owned())))
for value in values {
vec.push(Value::from(*value));
}
Ok(vec.into())
} }
#[inline] #[inline]
@ -690,6 +687,15 @@ mod tests {
assert_eq!(expected, to_value(&expected).unwrap()); assert_eq!(expected, to_value(&expected).unwrap());
} }
#[test]
fn bytes() {
let bytes = Bytes("foobar".as_bytes().to_owned());
let value = to_value(&bytes).unwrap();
let expected = Value::Bytes(bytes);
assert_eq!(value, expected);
assert_eq!(expected, to_value(&expected).unwrap());
}
#[test] #[test]
fn param() { fn param() {
let param = Param::default(); let param = Param::default();

View file

@ -6,6 +6,7 @@ use crate::dbs::Transaction;
use crate::err::Error; use crate::err::Error;
use crate::sql::array::{array, Array}; use crate::sql::array::{array, Array};
use crate::sql::block::{block, Block}; use crate::sql::block::{block, Block};
use crate::sql::bytes::Bytes;
use crate::sql::common::commas; use crate::sql::common::commas;
use crate::sql::constant::{constant, Constant}; use crate::sql::constant::{constant, Constant};
use crate::sql::datetime::{datetime, Datetime}; use crate::sql::datetime::{datetime, Datetime};
@ -114,7 +115,7 @@ pub enum Value {
Array(Array), Array(Array),
Object(Object), Object(Object),
Geometry(Geometry), Geometry(Geometry),
Bytes(Vec<u8>), Bytes(Bytes),
// --- // ---
Param(Param), Param(Param),
Idiom(Idiom), Idiom(Idiom),