-
Notifications
You must be signed in to change notification settings - Fork 141
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
RUST-1045 Support appending to RawDocumentBuf
#326
Merged
patrickfreed
merged 21 commits into
mongodb:master
from
patrickfreed:RUST-1045/raw-document-append
Nov 30, 2021
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d746652
wip append
patrickfreed bcf3b9c
finish implementaiton, start adding tests
patrickfreed 021f1f8
wip array buf stuff
patrickfreed b2783d0
wip wip
patrickfreed 4c4b93b
rename bson.rs -> bson_ref.rs
patrickfreed 6eaff65
document arraybuf
patrickfreed 3f46173
Revert "rename bson.rs -> bson_ref.rs"
patrickfreed 706c167
wip owned bson
patrickfreed d45ee9d
add owned raw bson to corpus field tests
patrickfreed b8089f2
minor improvements
patrickfreed ae50178
add serialize/deserialize impl to rawarraybuf, serde tests for owned
patrickfreed 52e6176
fix clippy
patrickfreed af29b3e
various cleanup
patrickfreed ed680ef
further consolidate deserialization logic
patrickfreed 9f33205
consolidate array deserialization
patrickfreed b0cbaef
standardize on from/into_bytes
patrickfreed fc96e7f
fix tests, compile on MSRV
patrickfreed 6ddebbd
fix comment
patrickfreed dd3aa9c
add `as_document_mut` and `as_array_mut`
patrickfreed 9abd6f9
refactor `RawBson` -> `RawBsonRef`, `OwnedRawBson` -> `RawBson`
patrickfreed 4261922
refactor `as_raw_bson` -> `as_raw_bson_ref`
patrickfreed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use pretty_assertions::assert_eq; | ||
use serde_json::json; | ||
|
||
use super::AllTypes; | ||
|
||
use bson::{doc, Bson, JavaScriptCodeWithScope, RawArrayBuf, RawBson, RawDocumentBuf}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[test] | ||
fn all_types_json() { | ||
let (mut v, _) = AllTypes::fixtures(); | ||
|
||
let code = match v.code { | ||
Bson::JavaScriptCode(ref c) => c.clone(), | ||
c => panic!("expected code, found {:?}", c), | ||
}; | ||
|
||
let code_w_scope = JavaScriptCodeWithScope { | ||
code: "hello world".to_string(), | ||
scope: doc! { "x": 1 }, | ||
}; | ||
let scope_json = serde_json::json!({ "x": 1 }); | ||
v.code_w_scope = code_w_scope.clone(); | ||
|
||
let json = serde_json::json!({ | ||
"x": 1, | ||
"y": 2, | ||
"s": "oke", | ||
"array": vec![ | ||
serde_json::json!(true), | ||
serde_json::json!("oke".to_string()), | ||
serde_json::json!({ "12": 24 }), | ||
], | ||
"bson": 1234.5, | ||
"oid": { "$oid": v.oid.to_hex() }, | ||
"null": serde_json::Value::Null, | ||
"subdoc": { "k": true, "b": { "hello": "world" } }, | ||
"b": true, | ||
"d": 12.5, | ||
"binary": v.binary.bytes, | ||
"binary_old": { "$binary": { "base64": base64::encode(&v.binary_old.bytes), "subType": "02" } }, | ||
"binary_other": { "$binary": { "base64": base64::encode(&v.binary_old.bytes), "subType": "81" } }, | ||
"date": { "$date": { "$numberLong": v.date.timestamp_millis().to_string() } }, | ||
"regex": { "$regularExpression": { "pattern": v.regex.pattern, "options": v.regex.options } }, | ||
"ts": { "$timestamp": { "t": 123, "i": 456 } }, | ||
"i": { "a": v.i.a, "b": v.i.b }, | ||
"undefined": { "$undefined": true }, | ||
"code": { "$code": code }, | ||
"code_w_scope": { "$code": code_w_scope.code, "$scope": scope_json }, | ||
"decimal": { "$numberDecimalBytes": v.decimal.bytes() }, | ||
"symbol": { "$symbol": "ok" }, | ||
"min_key": { "$minKey": 1 }, | ||
"max_key": { "$maxKey": 1 }, | ||
}); | ||
|
||
assert_eq!(serde_json::to_value(&v).unwrap(), json); | ||
} | ||
|
||
#[test] | ||
fn owned_raw_bson() { | ||
#[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
struct Foo { | ||
doc_buf: RawDocumentBuf, | ||
array_buf: RawArrayBuf, | ||
bson_array: RawBson, | ||
bson_doc: RawBson, | ||
bson_integer: RawBson, | ||
bson_string: RawBson, | ||
bson_bool: RawBson, | ||
bson_null: RawBson, | ||
bson_float: RawBson, | ||
} | ||
|
||
let json = json!({ | ||
"doc_buf": { | ||
"a": "key", | ||
"number": 12, | ||
"bool": false, | ||
"nu": null | ||
}, | ||
"array_buf": [ | ||
json!(1), | ||
json!("string"), | ||
], | ||
"bson_array": [ | ||
json!(1), | ||
json!("string"), | ||
], | ||
"bson_doc": { | ||
"first": true, | ||
"second": "string", | ||
}, | ||
"bson_integer": 12, | ||
"bson_string": "String", | ||
"bson_bool": true, | ||
"bson_null": null, | ||
"bson_float": 123.4 | ||
}); | ||
|
||
let mut doc_buf = RawDocumentBuf::new(); | ||
doc_buf.append("a", "key"); | ||
doc_buf.append("number", 12); | ||
doc_buf.append("bool", false); | ||
doc_buf.append("nu", RawBson::Null); | ||
|
||
let mut array_buf = RawArrayBuf::new(); | ||
array_buf.push(1); | ||
array_buf.push("string"); | ||
|
||
let mut bson_doc = RawDocumentBuf::new(); | ||
bson_doc.append("first", true); | ||
bson_doc.append("second", "string"); | ||
|
||
let expected = Foo { | ||
doc_buf, | ||
array_buf: array_buf.clone(), | ||
bson_array: RawBson::Array(array_buf), | ||
bson_doc: RawBson::Document(bson_doc), | ||
bson_integer: RawBson::Int32(12), | ||
bson_string: RawBson::String("String".to_string()), | ||
bson_bool: RawBson::Boolean(true), | ||
bson_null: RawBson::Null, | ||
bson_float: RawBson::Double(123.4), | ||
}; | ||
|
||
let f: Foo = serde_json::from_value(json.clone()).unwrap(); | ||
assert_eq!(f, expected); | ||
|
||
let round_trip = serde_json::to_value(&f).unwrap(); | ||
assert_eq!(round_trip, json); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test was moved without modification from
test.rs
. The one after it is new though.