From a3b4d47b4e16a4383136e22cf6e9084eae5026e8 Mon Sep 17 00:00:00 2001 From: Patrick Meredith Date: Sun, 25 Aug 2019 03:42:32 -0400 Subject: [PATCH] Finish last few types and add tests --- src/commands/from_bson.rs | 48 ++++++++++++++++++++++++++--- tests/commands_test.rs | 44 ++++++++++++++++++++++++++ tests/fixtures/formats/sample.bson | Bin 0 -> 439 bytes 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/formats/sample.bson diff --git a/src/commands/from_bson.rs b/src/commands/from_bson.rs index 8d1ab7b9bc..b281390a1f 100644 --- a/src/commands/from_bson.rs +++ b/src/commands/from_bson.rs @@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand; use crate::object::base::OF64; use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; -use bson::{decode_document, Bson}; +use bson::{decode_document, Bson, spec::BinarySubtype}; pub struct FromBSON; @@ -80,14 +80,54 @@ fn convert_bson_value_to_nu_value(v: &Bson, tag: impl Into) -> Tagged { + let mut collected = TaggedDictBuilder::new(tag); + collected.insert_tagged( + "$timestamp".to_string(), + Value::Primitive(Primitive::Int(*ts as i64)).tagged(tag), + ); + collected.into_tagged_value() + } + Bson::Binary(bst, bytes) => { + let mut collected = TaggedDictBuilder::new(tag); + collected.insert_tagged( + "$binary_subtype".to_string(), + match bst { + BinarySubtype::UserDefined(u) => Value::Primitive(Primitive::Int(*u as i64)), + _ => Value::Primitive(Primitive::String(binary_subtype_to_string(*bst))), + }.tagged(tag) + ); + collected.insert_tagged( + "$binary".to_string(), + Value::Binary(bytes.to_owned()).tagged(tag), + ); + collected.into_tagged_value() + } Bson::ObjectId(obj_id) => Value::Primitive(Primitive::String(obj_id.to_hex())).tagged(tag), - x => { - println!("{:?}", x); - panic!() + Bson::UtcDatetime(dt) => Value::Primitive(Primitive::Date(*dt)).tagged(tag), + Bson::Symbol(s) => { + let mut collected = TaggedDictBuilder::new(tag); + collected.insert_tagged( + "$symbol".to_string(), + Value::Primitive(Primitive::String(String::from(s))).tagged(tag), + ); + collected.into_tagged_value() } } } +fn binary_subtype_to_string(bst: BinarySubtype) -> String { + match bst { + BinarySubtype::Generic => "generic", + BinarySubtype::Function => "function", + BinarySubtype::BinaryOld => "binary_old", + BinarySubtype::UuidOld => "uuid_old", + BinarySubtype::Uuid => "uuid", + BinarySubtype::Md5 => "md5", + _ => unreachable!(), + }.to_string() +} + #[derive(Debug)] struct BytesReader { pos: usize, diff --git a/tests/commands_test.rs b/tests/commands_test.rs index faced2631f..84c086fd7a 100644 --- a/tests/commands_test.rs +++ b/tests/commands_test.rs @@ -24,6 +24,50 @@ fn open_can_parse_csv() { assert_eq!(output, "SPAIN"); } +#[test] +fn open_can_parse_bson_1() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open sample.bson | nth 3 | get b | get '$javascript' | echo $it" + ); + + assert_eq!(output, "let x = y"); +} + +#[test] +fn open_can_parse_bson_2() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open sample.bson | nth 0 | get b | echo $it" + ); + + assert_eq!(output, "hello"); +} + +#[test] +fn open_can_parse_bson_3() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open sample.bson | nth 0 | get b | echo $it" + ); + + assert_eq!(output, "hello"); +} + +#[test] +fn open_can_parse_bson_4() { + nu!( + output, + cwd("tests/fixtures/formats"), + "open sample.bson | nth 6 | get b | get '$binary_subtype' | echo $it " + ); + + assert_eq!(output, "function"); +} + #[test] fn open_can_parse_toml() { nu!( diff --git a/tests/fixtures/formats/sample.bson b/tests/fixtures/formats/sample.bson new file mode 100644 index 0000000000000000000000000000000000000000..95c98eb4e1f1f8ec1517944c23045ba72b098940 GIT binary patch literal 439 zcmc~~U|?X6&rD&6O-z-XAXd%bZX~>oF%c*V0w3&|k{H;4jEvNroO}kBWCjZ$huMHZ z1V}Mur&fX`i%Uw=(tyH-43aQmZXhE!zbG{xD#yTJ1r&i=l%1^Sv~X^f;ww|QMXnA& zi&%k5%7Hd9FeM{d%Ak(Wd75?F%NT=`x9Zin6CrL=D9u%cGr z4Wli85`!{O9ORsw)DndX1zUwmYX$)zm$4E^fPjDl1A`)vhC1#7Q%>ym8rAbY9D>OV zj6iXw3yHBP3M`=tIH3wGK?F Zng!J=#F@yj;$_j19H0xJdIXU5007x)W4QnT literal 0 HcmV?d00001