Skip to content

RUST-871 Support direct serialization to BSON bytes #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ functions:
${PREPARE_SHELL}
.evergreen/run-tests-u2i.sh

"run serde tests":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than the serde tests being their own task in evergreen, I updated each of the other tasks to run it too with their associated feature flags on.

- command: shell.exec
type: test
params:
shell: bash
working_dir: "src"
script: |
${PREPARE_SHELL}
.evergreen/run-tests-serde.sh

"run decimal128 tests":
- command: shell.exec
type: test
Expand Down Expand Up @@ -170,10 +160,6 @@ tasks:
commands:
- func: "run u2i tests"

- name: "test-serde"
commands:
- func: "run serde tests"

- name: "test-decimal128"
commands:
- func: "run decimal128 tests"
Expand Down Expand Up @@ -211,7 +197,6 @@ buildvariants:
tasks:
- name: "test"
- name: "test-u2i"
- name: "test-serde"
- name: "test-decimal128"

- matrix_name: "compile only"
Expand Down
3 changes: 3 additions & 0 deletions .evergreen/run-tests-decimal128.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ set -o errexit

. ~/.cargo/env
RUST_BACKTRACE=1 cargo test --features decimal128

cd serde-tests
RUST_BACKTRACE=1 cargo test --features decimal128
3 changes: 3 additions & 0 deletions .evergreen/run-tests-u2i.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ set -o errexit

. ~/.cargo/env
RUST_BACKTRACE=1 cargo test --features u2i

cd serde-tests
RUST_BACKTRACE=1 cargo test --features u2i
3 changes: 3 additions & 0 deletions .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ set -o errexit
. ~/.cargo/env
RUST_BACKTRACE=1 cargo test
RUST_BACKTRACE=1 cargo test --features chrono-0_4,uuid-0_8

cd serde-tests
RUST_BACKTRACE=1 cargo test
7 changes: 6 additions & 1 deletion serde-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ version = "0.1.0"
authors = ["Kevin Yeh <[email protected]>"]
edition = "2018"

[features]
u2i = ["bson/u2i"]
decimal128 = ["bson/decimal128"]

[dependencies]
bson = { path = "..", features = ["decimal128"] }
bson = { path = ".." }
serde = { version = "1.0", features = ["derive"] }
pretty_assertions = "0.6.1"
hex = "0.4.2"

[lib]
name = "serde_tests"
Expand Down
110 changes: 106 additions & 4 deletions serde-tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use std::{
collections::{BTreeMap, HashSet},
};

#[cfg(feature = "decimal128")]
use bson::Decimal128;
use bson::{
doc,
oid::ObjectId,
spec::BinarySubtype,
Binary,
Bson,
DateTime,
Decimal128,
Deserializer,
Document,
JavaScriptCodeWithScope,
Expand All @@ -37,6 +38,7 @@ use bson::{
/// - deserializing a `T` from the raw BSON version of `expected_doc` produces `expected_value`
/// - deserializing a `Document` from the raw BSON version of `expected_doc` produces
/// `expected_doc`
/// - `bson::to_writer` and `Document::to_writer` produce the same result given the same input
fn run_test<T>(expected_value: &T, expected_doc: &Document, description: &str)
where
T: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug,
Expand All @@ -46,6 +48,16 @@ where
.to_writer(&mut expected_bytes)
.expect(description);

let expected_bytes_serde = bson::to_vec(&expected_value).expect(description);
assert_eq!(expected_bytes_serde, expected_bytes, "{}", description);

let expected_bytes_from_doc_serde = bson::to_vec(&expected_doc).expect(description);
assert_eq!(
expected_bytes_from_doc_serde, expected_bytes,
"{}",
description
);

let serialized_doc = bson::to_document(&expected_value).expect(description);
assert_eq!(&serialized_doc, expected_doc, "{}", description);
assert_eq!(
Expand Down Expand Up @@ -702,7 +714,7 @@ fn all_types() {
undefined: Bson,
code: Bson,
code_w_scope: JavaScriptCodeWithScope,
decimal: Decimal128,
decimal: Bson,
symbol: Bson,
min_key: Bson,
max_key: Bson,
Expand Down Expand Up @@ -737,6 +749,16 @@ fn all_types() {
let oid = ObjectId::new();
let subdoc = doc! { "k": true, "b": { "hello": "world" } };

#[cfg(not(feature = "decimal128"))]
let decimal = {
let bytes = hex::decode("18000000136400D0070000000000000000000000003A3000").unwrap();
let d = Document::from_reader(bytes.as_slice()).unwrap();
d.get("d").unwrap().clone()
};

#[cfg(feature = "decimal128")]
let decimal = Bson::Decimal128(Decimal128::from_str("2.000"));

let doc = doc! {
"x": 1,
"y": 2_i64,
Expand All @@ -758,7 +780,7 @@ fn all_types() {
"undefined": Bson::Undefined,
"code": code.clone(),
"code_w_scope": code_w_scope.clone(),
"decimal": Bson::Decimal128(Decimal128::from_i32(5)),
"decimal": decimal.clone(),
"symbol": Bson::Symbol("ok".to_string()),
"min_key": Bson::MinKey,
"max_key": Bson::MaxKey,
Expand Down Expand Up @@ -789,7 +811,7 @@ fn all_types() {
undefined: Bson::Undefined,
code,
code_w_scope,
decimal: Decimal128::from_i32(5),
decimal,
symbol: Bson::Symbol("ok".to_string()),
min_key: Bson::MinKey,
max_key: Bson::MaxKey,
Expand Down Expand Up @@ -851,3 +873,83 @@ fn borrowed() {
bson::from_slice(bson.as_slice()).expect("deserialization should succeed");
assert_eq!(deserialized, v);
}

#[cfg(feature = "u2i")]
#[test]
fn u2i() {
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Foo {
u_8: u8,
u_16: u16,
u_32: u32,
u_32_max: u32,
u_64: u64,
i_64_max: u64,
}

let v = Foo {
u_8: 15,
u_16: 123,
u_32: 1234,
u_32_max: u32::MAX,
u_64: 12345,
i_64_max: i64::MAX as u64,
};

let expected = doc! {
"u_8": 15_i32,
"u_16": 123_i32,
"u_32": 1234_i64,
"u_32_max": u32::MAX as i64,
"u_64": 12345_i64,
"i_64_max": i64::MAX as u64,
};

run_test(&v, &expected, "u2i - valid");

#[derive(Serialize, Debug)]
struct TooBig {
u_64: u64,
}
let v = TooBig {
u_64: i64::MAX as u64 + 1,
};
bson::to_document(&v).unwrap_err();
bson::to_vec(&v).unwrap_err();
}

#[cfg(not(feature = "u2i"))]
#[test]
fn unsigned() {
#[derive(Serialize, Debug)]
struct U8 {
v: u8,
}
let v = U8 { v: 1 };
bson::to_document(&v).unwrap_err();
bson::to_vec(&v).unwrap_err();

#[derive(Serialize, Debug)]
struct U16 {
v: u16,
}
let v = U16 { v: 1 };
bson::to_document(&v).unwrap_err();
bson::to_vec(&v).unwrap_err();

#[derive(Serialize, Debug)]
struct U32 {
v: u32,
}
let v = U32 { v: 1 };
bson::to_document(&v).unwrap_err();
bson::to_vec(&v).unwrap_err();

#[derive(Serialize, Debug)]
struct U64 {
v: u64,
}
let v = U64 { v: 1 };
bson::to_document(&v).unwrap_err();
bson::to_vec(&v).unwrap_err();
}
16 changes: 15 additions & 1 deletion src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! BSON definition

use std::{
convert::TryFrom,
convert::{TryFrom, TryInto},
fmt::{self, Debug, Display, Formatter},
};

Expand Down Expand Up @@ -698,6 +698,20 @@ impl Bson {
}
}

["$numberDecimalBytes"] => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintuitively, this function is used in the existing serializer to convert between the intermediate serialized form (i.e. and "extended document") and the final serialized form. Now that decimal128 can serialize this way, this needed to be updated to interpret it.

if let Ok(bytes) = doc.get_binary_generic("$numberDecimalBytes") {
if let Ok(b) = bytes.clone().try_into() {
#[cfg(not(feature = "decimal128"))]
return Bson::Decimal128(Decimal128 { bytes: b });

#[cfg(feature = "decimal128")]
unsafe {
return Bson::Decimal128(Decimal128::from_raw_bytes_le(b));
}
}
}
}

["$binary"] => {
if let Some(binary) = Binary::from_extended_doc(&doc) {
return Bson::Binary(binary);
Expand Down
34 changes: 26 additions & 8 deletions src/extjson/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use chrono::Utc;
use serde::{
de::{Error, Unexpected},
Deserialize,
Serialize,
};

use crate::{extjson, oid, spec::BinarySubtype, Bson};
Expand All @@ -27,7 +28,7 @@ impl Int32 {
}
}

#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Int64 {
#[serde(rename = "$numberLong")]
Expand Down Expand Up @@ -72,7 +73,7 @@ impl Double {
}
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ObjectId {
#[serde(rename = "$oid")]
Expand All @@ -86,6 +87,12 @@ impl ObjectId {
}
}

impl From<crate::oid::ObjectId> for ObjectId {
fn from(id: crate::oid::ObjectId) -> Self {
Self { oid: id.to_hex() }
}
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Symbol {
Expand All @@ -100,7 +107,7 @@ pub(crate) struct Regex {
body: RegexBody,
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct RegexBody {
pub(crate) pattern: String,
Expand All @@ -127,7 +134,7 @@ pub(crate) struct Binary {
pub(crate) body: BinaryBody,
}

#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct BinaryBody {
pub(crate) base64: String,
Expand Down Expand Up @@ -207,10 +214,13 @@ pub(crate) struct Timestamp {
body: TimestampBody,
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct TimestampBody {
#[serde(serialize_with = "crate::serde_helpers::serialize_u32_as_i64")]
pub(crate) t: u32,

#[serde(serialize_with = "crate::serde_helpers::serialize_u32_as_i64")]
pub(crate) i: u32,
}

Expand All @@ -223,20 +233,28 @@ impl Timestamp {
}
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct DateTime {
#[serde(rename = "$date")]
pub(crate) body: DateTimeBody,
}

#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub(crate) enum DateTimeBody {
Canonical(Int64),
Relaxed(String),
}

impl DateTimeBody {
pub(crate) fn from_millis(m: i64) -> Self {
DateTimeBody::Canonical(Int64 {
value: m.to_string(),
})
}
}

impl DateTime {
pub(crate) fn parse(self) -> extjson::de::Result<crate::DateTime> {
match self.body {
Expand Down Expand Up @@ -307,7 +325,7 @@ pub(crate) struct DbPointer {
body: DbPointerBody,
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct DbPointerBody {
#[serde(rename = "$ref")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub use self::{
Deserializer,
},
decimal128::Decimal128,
ser::{to_bson, to_document, Serializer},
ser::{to_bson, to_document, to_vec, Serializer},
};

#[macro_use]
Expand Down
Loading