Finish last few types and add tests

This commit is contained in:
Patrick Meredith 2019-08-25 03:42:32 -04:00
parent 722e192c14
commit a3b4d47b4e
3 changed files with 88 additions and 4 deletions

View file

@ -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<Tag>) -> Tagged<Value
);
collected.into_tagged_value()
}
Bson::TimeStamp(ts) => {
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,

View file

@ -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!(

BIN
tests/fixtures/formats/sample.bson vendored Normal file

Binary file not shown.