2019-08-24 19:14:06 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-11 07:53:05 +00:00
|
|
|
use crate::data::{Primitive, TaggedDictBuilder, Value};
|
2019-09-12 18:29:16 +00:00
|
|
|
use crate::errors::ExpectedRange;
|
2019-08-24 19:14:06 +00:00
|
|
|
use crate::prelude::*;
|
2019-08-26 18:19:05 +00:00
|
|
|
use bson::{decode_document, spec::BinarySubtype, Bson};
|
2019-08-31 14:22:45 +00:00
|
|
|
use std::str::FromStr;
|
2019-08-24 19:14:06 +00:00
|
|
|
|
|
|
|
pub struct FromBSON;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromBSON {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-bson"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-bson")
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .bson and create table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_bson(args, registry)
|
|
|
|
}
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 16:20:31 +00:00
|
|
|
fn bson_array(input: &Vec<Bson>, tag: Tag) -> Result<Vec<Tagged<Value>>, ShellError> {
|
|
|
|
let mut out = vec![];
|
|
|
|
|
|
|
|
for value in input {
|
2019-10-13 04:12:43 +00:00
|
|
|
out.push(convert_bson_value_to_nu_value(value, &tag)?);
|
2019-09-01 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn convert_bson_value_to_nu_value(
|
|
|
|
v: &Bson,
|
|
|
|
tag: impl Into<Tag>,
|
|
|
|
) -> Result<Tagged<Value>, ShellError> {
|
2019-08-24 19:14:06 +00:00
|
|
|
let tag = tag.into();
|
|
|
|
|
2019-09-01 16:20:31 +00:00
|
|
|
Ok(match v {
|
2019-10-13 04:12:43 +00:00
|
|
|
Bson::FloatingPoint(n) => Value::Primitive(Primitive::from(*n)).tagged(&tag),
|
|
|
|
Bson::String(s) => Value::Primitive(Primitive::String(String::from(s))).tagged(&tag),
|
|
|
|
Bson::Array(a) => Value::Table(bson_array(a, tag.clone())?).tagged(&tag),
|
2019-08-24 19:14:06 +00:00
|
|
|
Bson::Document(doc) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-24 19:14:06 +00:00
|
|
|
for (k, v) in doc.iter() {
|
2019-10-13 04:12:43 +00:00
|
|
|
collected.insert_tagged(k.clone(), convert_bson_value_to_nu_value(v, &tag)?);
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
collected.into_tagged_value()
|
|
|
|
}
|
2019-10-13 04:12:43 +00:00
|
|
|
Bson::Boolean(b) => Value::Primitive(Primitive::Boolean(*b)).tagged(&tag),
|
|
|
|
Bson::Null => Value::Primitive(Primitive::Nothing).tagged(&tag),
|
2019-08-24 23:58:32 +00:00
|
|
|
Bson::RegExp(r, opts) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-26 18:19:05 +00:00
|
|
|
collected.insert_tagged(
|
|
|
|
"$regex".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(r))).tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.insert_tagged(
|
|
|
|
"$options".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(opts))).tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.into_tagged_value()
|
2019-08-24 23:58:32 +00:00
|
|
|
}
|
2019-10-13 04:12:43 +00:00
|
|
|
Bson::I32(n) => Value::number(n).tagged(&tag),
|
|
|
|
Bson::I64(n) => Value::number(n).tagged(&tag),
|
2019-08-31 14:22:45 +00:00
|
|
|
Bson::Decimal128(n) => {
|
2019-09-01 16:20:31 +00:00
|
|
|
// TODO: this really isn't great, and we should update this to do a higher
|
|
|
|
// fidelity translation
|
|
|
|
let decimal = BigDecimal::from_str(&format!("{}", n)).map_err(|_| {
|
|
|
|
ShellError::range_error(
|
|
|
|
ExpectedRange::BigDecimal,
|
2019-10-13 04:12:43 +00:00
|
|
|
&n.tagged(&tag),
|
2019-09-01 16:20:31 +00:00
|
|
|
format!("converting BSON Decimal128 to BigDecimal"),
|
|
|
|
)
|
|
|
|
})?;
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::Decimal(decimal)).tagged(&tag)
|
2019-08-31 14:22:45 +00:00
|
|
|
}
|
2019-08-24 23:58:32 +00:00
|
|
|
Bson::JavaScriptCode(js) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-26 18:19:05 +00:00
|
|
|
collected.insert_tagged(
|
|
|
|
"$javascript".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(js))).tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.into_tagged_value()
|
2019-08-24 23:58:32 +00:00
|
|
|
}
|
|
|
|
Bson::JavaScriptCodeWithScope(js, doc) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-26 18:19:05 +00:00
|
|
|
collected.insert_tagged(
|
|
|
|
"$javascript".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(js))).tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.insert_tagged(
|
|
|
|
"$scope".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
convert_bson_value_to_nu_value(&Bson::Document(doc.to_owned()), tag.clone())?,
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.into_tagged_value()
|
2019-08-24 23:58:32 +00:00
|
|
|
}
|
2019-08-25 07:42:32 +00:00
|
|
|
Bson::TimeStamp(ts) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
|
|
|
collected.insert_tagged("$timestamp".to_string(), Value::number(ts).tagged(&tag));
|
2019-08-26 18:19:05 +00:00
|
|
|
collected.into_tagged_value()
|
2019-08-25 07:42:32 +00:00
|
|
|
}
|
|
|
|
Bson::Binary(bst, bytes) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-26 18:19:05 +00:00
|
|
|
collected.insert_tagged(
|
|
|
|
"$binary_subtype".to_string(),
|
|
|
|
match bst {
|
2019-09-01 16:20:31 +00:00
|
|
|
BinarySubtype::UserDefined(u) => Value::number(u),
|
2019-08-26 18:19:05 +00:00
|
|
|
_ => Value::Primitive(Primitive::String(binary_subtype_to_string(*bst))),
|
|
|
|
}
|
2019-10-13 04:12:43 +00:00
|
|
|
.tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.insert_tagged(
|
|
|
|
"$binary".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::Binary(bytes.to_owned())).tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.into_tagged_value()
|
2019-08-25 07:42:32 +00:00
|
|
|
}
|
2019-08-26 14:16:34 +00:00
|
|
|
Bson::ObjectId(obj_id) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-26 14:16:34 +00:00
|
|
|
collected.insert_tagged(
|
|
|
|
"$object_id".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(obj_id.to_hex())).tagged(&tag),
|
2019-08-26 14:16:34 +00:00
|
|
|
);
|
|
|
|
collected.into_tagged_value()
|
|
|
|
}
|
2019-10-13 04:12:43 +00:00
|
|
|
Bson::UtcDatetime(dt) => Value::Primitive(Primitive::Date(*dt)).tagged(&tag),
|
2019-08-25 07:42:32 +00:00
|
|
|
Bson::Symbol(s) => {
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag.clone());
|
2019-08-26 18:19:05 +00:00
|
|
|
collected.insert_tagged(
|
|
|
|
"$symbol".to_string(),
|
2019-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(s))).tagged(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
|
|
|
collected.into_tagged_value()
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
2019-09-01 16:20:31 +00:00
|
|
|
})
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 07:42:32 +00:00
|
|
|
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!(),
|
2019-08-26 18:19:05 +00:00
|
|
|
}
|
|
|
|
.to_string()
|
2019-08-25 07:42:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 19:14:06 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct BytesReader {
|
|
|
|
pos: usize,
|
|
|
|
inner: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BytesReader {
|
|
|
|
fn new(bytes: Vec<u8>) -> BytesReader {
|
|
|
|
BytesReader {
|
|
|
|
pos: 0,
|
|
|
|
inner: bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::io::Read for BytesReader {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
|
|
|
let src: &mut &[u8] = &mut self.inner[self.pos..].as_ref();
|
|
|
|
let diff = src.read(buf)?;
|
|
|
|
self.pos += diff;
|
|
|
|
Ok(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_bson_bytes_to_value(
|
|
|
|
bytes: Vec<u8>,
|
2019-08-24 23:38:33 +00:00
|
|
|
tag: impl Into<Tag>,
|
2019-09-01 16:20:31 +00:00
|
|
|
) -> Result<Tagged<Value>, ShellError> {
|
2019-08-24 23:38:33 +00:00
|
|
|
let mut docs = Vec::new();
|
2019-08-24 19:14:06 +00:00
|
|
|
let mut b_reader = BytesReader::new(bytes);
|
|
|
|
while let Ok(v) = decode_document(&mut b_reader) {
|
2019-08-24 23:38:33 +00:00
|
|
|
docs.push(Bson::Document(v));
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
2019-09-01 16:20:31 +00:00
|
|
|
|
|
|
|
convert_bson_value_to_nu_value(&Bson::Array(docs), tag)
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_bson(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
2019-09-14 16:30:24 +00:00
|
|
|
let tag = args.name_tag();
|
2019-08-24 19:14:06 +00:00
|
|
|
let input = args.input;
|
|
|
|
|
2019-09-26 00:22:17 +00:00
|
|
|
let stream = async_stream! {
|
2019-08-24 19:14:06 +00:00
|
|
|
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
|
|
|
|
|
|
|
for value in values {
|
|
|
|
let value_tag = value.tag();
|
|
|
|
match value.item {
|
2019-09-12 18:29:16 +00:00
|
|
|
Value::Primitive(Primitive::Binary(vb)) =>
|
2019-10-13 04:12:43 +00:00
|
|
|
match from_bson_bytes_to_value(vb, tag.clone()) {
|
2019-08-24 19:14:06 +00:00
|
|
|
Ok(x) => yield ReturnSuccess::value(x),
|
2019-08-25 07:52:06 +00:00
|
|
|
Err(_) => {
|
2019-08-24 19:14:06 +00:00
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as BSON",
|
|
|
|
"input cannot be parsed as BSON",
|
2019-10-13 04:12:43 +00:00
|
|
|
tag.clone(),
|
2019-08-24 19:14:06 +00:00
|
|
|
"value originates from here",
|
2019-09-14 16:30:24 +00:00
|
|
|
value_tag,
|
2019-08-24 19:14:06 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-10-13 04:12:43 +00:00
|
|
|
tag.clone(),
|
2019-08-24 19:14:06 +00:00
|
|
|
"value originates from here",
|
2019-09-14 16:30:24 +00:00
|
|
|
value_tag,
|
2019-08-24 19:14:06 +00:00
|
|
|
)),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|