1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use mongodb::bson::{Bson, Document};
use nu_protocol::{LabeledError, Record, Span, Value};
pub fn doc_to_value(doc: Document, span: Span) -> Value {
let mut rec = Record::new();
for (k, v) in doc {
let val = match v {
Bson::Null => Value::nothing(span),
Bson::Double(n) => Value::float(n, span),
Bson::String(s) => Value::string(s, span),
Bson::Boolean(v) => Value::bool(v, span),
Bson::Int32(i) => Value::int(i.into(), span),
Bson::Int64(i) => Value::int(i.into(), span),
Bson::ObjectId(oid) => Value::string(oid.to_string(), span),
Bson::Document(d) => doc_to_value(d, span),
Bson::DateTime(dt) => Value::date(dt.to_chrono().into(), span),
Bson::Binary(b) => Value::binary(b.bytes, span),
other => Value::string(other.to_string(), span),
};
rec.push(k, val);
}
Value::record(rec, span)
}
fn to_bson(v: Value) -> Result<Bson, LabeledError> {
let val_span = v.span();
let bson_val = match v {
Value::Record { val, .. } => Bson::Document(value_to_doc(val.into_owned())?),
Value::Int { val, .. } => Bson::Int64(val),
Value::Bool { val, .. } => Bson::Boolean(val),
Value::Float { val, .. } => Bson::Double(val),
Value::String { val, .. } | Value::Glob { val, .. } => {
// FIXME: there are some issues on record value interpolation
// see more: https://github.com/nushell/nushell/issues/13602
// // may parse as an mongodb ObjectId
// if val.starts_with("ObjectId(\"") && val.ends_with("\")") {
// let object_id = val
// .trim_start_matches("ObjectId(\"")
// .trim_end_matches("\")");
// match bson::oid::ObjectId::parse_str(object_id) {
// Err(e) => {
// return Err(LabeledError::new(format!(
// "invalid ObjectId, detailed: {e}"
// )))
// }
// Ok(object_id) => Bson::ObjectId(object_id),
// let object_id = val.trim_start_matches("ObjectId(\'").trim_end_matches("')");
// match bson::oid::ObjectId::parse_str(object_id) {
// Err(e) => {
// return Err(LabeledError::new(format!(
// "invalid ObjectId, detailed: {e}"
// )))
// }
// Ok(object_id) => Bson::ObjectId(object_id),
// }
// } else {
// Bson::String(val)
// }
//
// A workaround
if val.starts_with("ObjectId") {
let object_id = val.trim_start_matches("ObjectId");
match bson::oid::ObjectId::parse_str(object_id) {
Err(e) => {
return Err(LabeledError::new(format!("invalid ObjectId"))
.with_label(format!("{e}"), val_span));
}
Ok(object_id) => Bson::ObjectId(object_id),
}
} else {
Bson::String(val)
}
}
Value::Date { val, .. } => {
Bson::DateTime(bson::DateTime::from_chrono::<chrono::Utc>(val.into()))
}
Value::Binary { val, .. } => Bson::Binary(bson::Binary {
subtype: bson::spec::BinarySubtype::Generic,
bytes: val,
}),
Value::List { vals, .. } => {
let mut array_vals = vec![];
for v in vals {
array_vals.push(to_bson(v)?)
}
Bson::Array(array_vals)
}
other => {
return Err(
LabeledError::new(format!("can't convert to mongo doc")).with_label(
format!("invalid value type: {}", other.get_type()),
val_span,
),
);
}
};
Ok(bson_val)
}
pub fn value_to_doc(val: Record) -> Result<Document, LabeledError> {
let mut doc = Document::new();
for (k, v) in Record::clone(&val).into_iter() {
let bson_val = to_bson(v)?;
doc.insert(k, bson_val);
}
Ok(doc)
}