2019-08-24 19:14:06 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
2019-08-26 18:19:05 +00:00
|
|
|
use bson::{decode_document, spec::BinarySubtype, Bson};
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::{ExpectedRange, ShellError};
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-11-21 14:33:14 +00:00
|
|
|
use nu_source::SpannedItem;
|
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 {
|
2020-05-04 08:44:33 +00:00
|
|
|
"from bson"
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 08:44:33 +00:00
|
|
|
Signature::build("from bson")
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-05-04 08:44:33 +00:00
|
|
|
"Parse binary as .bson and create table."
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_bson(args, registry)
|
|
|
|
}
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:28:26 +00:00
|
|
|
fn bson_array(input: &[Bson], tag: Tag) -> Result<Vec<Value>, ShellError> {
|
2019-09-01 16:20:31 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn convert_bson_value_to_nu_value(v: &Bson, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
2019-08-24 19:14:06 +00:00
|
|
|
let tag = tag.into();
|
2019-11-21 14:33:14 +00:00
|
|
|
let span = tag.span;
|
2019-08-24 19:14:06 +00:00
|
|
|
|
2019-09-01 16:20:31 +00:00
|
|
|
Ok(match v {
|
2019-11-21 14:33:14 +00:00
|
|
|
Bson::FloatingPoint(n) => UntaggedValue::Primitive(Primitive::from(*n)).into_value(&tag),
|
|
|
|
Bson::String(s) => {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag)
|
|
|
|
}
|
|
|
|
Bson::Array(a) => UntaggedValue::Table(bson_array(a, tag.clone())?).into_value(&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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(k.clone(), convert_bson_value_to_nu_value(v, &tag)?);
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_value()
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
Bson::Boolean(b) => UntaggedValue::Primitive(Primitive::Boolean(*b)).into_value(&tag),
|
|
|
|
Bson::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$regex".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(r))).into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$options".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(opts))).into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_value()
|
2019-08-24 23:58:32 +00:00
|
|
|
}
|
2019-12-04 19:52:31 +00:00
|
|
|
Bson::I32(n) => UntaggedValue::int(*n).into_value(&tag),
|
|
|
|
Bson::I64(n) => UntaggedValue::int(*n).into_value(&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-11-21 14:33:14 +00:00
|
|
|
&n.spanned(span),
|
2019-12-06 15:28:26 +00:00
|
|
|
"converting BSON Decimal128 to BigDecimal".to_owned(),
|
2019-09-01 16:20:31 +00:00
|
|
|
)
|
|
|
|
})?;
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::Decimal(decimal)).into_value(&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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$javascript".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(js))).into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$javascript".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(js))).into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$scope".to_string(),
|
2019-12-31 07:36:08 +00:00
|
|
|
convert_bson_value_to_nu_value(&Bson::Document(doc.to_owned()), tag)?,
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_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());
|
2019-12-04 19:52:31 +00:00
|
|
|
collected.insert_value(
|
|
|
|
"$timestamp".to_string(),
|
|
|
|
UntaggedValue::int(*ts).into_value(&tag),
|
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$binary_subtype".to_string(),
|
|
|
|
match bst {
|
2019-12-04 19:52:31 +00:00
|
|
|
BinarySubtype::UserDefined(u) => UntaggedValue::int(*u),
|
2019-11-21 14:33:14 +00:00
|
|
|
_ => {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(binary_subtype_to_string(*bst)))
|
|
|
|
}
|
2019-08-26 18:19:05 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$binary".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::Binary(bytes.to_owned())).into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 14:16:34 +00:00
|
|
|
"$object_id".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(obj_id.to_hex())).into_value(&tag),
|
2019-08-26 14:16:34 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_value()
|
2019-08-26 14:16:34 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
Bson::UtcDatetime(dt) => UntaggedValue::Primitive(Primitive::Date(*dt)).into_value(&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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(
|
2019-08-26 18:19:05 +00:00
|
|
|
"$symbol".to_string(),
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag),
|
2019-08-26 18:19:05 +00:00
|
|
|
);
|
2019-11-21 14:33:14 +00:00
|
|
|
collected.into_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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn from_bson_bytes_to_value(bytes: Vec<u8>, tag: impl Into<Tag>) -> Result<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! {
|
2020-03-06 16:06:39 +00:00
|
|
|
let bytes = input.collect_binary(tag.clone()).await?;
|
|
|
|
|
|
|
|
match from_bson_bytes_to_value(bytes.item, tag.clone()) {
|
|
|
|
Ok(x) => yield ReturnSuccess::value(x),
|
|
|
|
Err(_) => {
|
|
|
|
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",
|
2020-03-06 16:06:39 +00:00
|
|
|
bytes.tag,
|
|
|
|
))
|
2019-08-24 19:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|