Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 3676e4a

Browse files
committed
match on *all* bson types for get_value and get_type functions
1 parent d2cb5c8 commit 3676e4a

File tree

6 files changed

+69
-62
lines changed

6 files changed

+69
-62
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ coverage/
22
target/
33
tmp/
44
dist/
5+
*.code-workspace
56
pkg/
67
wasm-pack.log
78
npm-debug.log*

Diff for: src/field_type.rs

+28-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::option_map_unit_fn)]
12
use super::{Bson, SchemaParser, ValueType};
23

34
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
@@ -38,10 +39,7 @@ impl FieldType {
3839
match value {
3940
Bson::Array(arr) => {
4041
for val in arr.iter() {
41-
let value_type = Self::get_value(val);
42-
if let Some(value_type) = value_type {
43-
self.values.push(value_type);
44-
}
42+
Self::get_value(val).map(|v| self.values.push(v));
4543
}
4644
self
4745
}
@@ -56,10 +54,7 @@ impl FieldType {
5654
self
5755
}
5856
_ => {
59-
let value_type = Self::get_value(&bson_value);
60-
if let Some(value_type) = value_type {
61-
self.values.push(value_type);
62-
}
57+
Self::get_value(&bson_value).map(|v| self.values.push(v));
6358
self
6459
}
6560
}
@@ -89,12 +84,21 @@ impl FieldType {
8984

9085
pub fn get_value(value: &Bson) -> Option<ValueType> {
9186
match value {
87+
Bson::RegExp(val, _)
88+
| Bson::JavaScriptCode(val)
89+
| Bson::JavaScriptCodeWithScope(val, _)
90+
| Bson::Symbol(val) => Some(ValueType::Str(val.to_string())),
91+
Bson::I64(num) | Bson::TimeStamp(num) => Some(ValueType::I64(*num)),
9292
Bson::FloatingPoint(num) => Some(ValueType::FloatingPoint(*num)),
93-
Bson::String(string) => Some(ValueType::Str(string.to_string())),
93+
Bson::UtcDatetime(date) => Some(ValueType::Str(date.clone().to_string())),
94+
Bson::Decimal128(d128) => Some(ValueType::Decimal128(d128.to_string())),
9495
Bson::Boolean(boolean) => Some(ValueType::Boolean(*boolean)),
96+
Bson::String(string) => Some(ValueType::Str(string.to_string())),
97+
Bson::Binary(_, vec) => Some(ValueType::Binary(vec.clone())),
9598
Bson::ObjectId(id) => Some(ValueType::Str(id.to_string())),
9699
Bson::I32(num) => Some(ValueType::I32(*num)),
97-
Bson::I64(num) => Some(ValueType::I64(*num)),
100+
Bson::Null => Some(ValueType::Null("Null".to_string())),
101+
// Array and Document get handeled separately
98102
_ => None,
99103
}
100104
}
@@ -107,16 +111,25 @@ impl FieldType {
107111

108112
pub fn get_type(value: &Bson) -> String {
109113
match value {
110-
Bson::FloatingPoint(_) | Bson::I32(_) | Bson::I64(_) => {
111-
"Number".to_string()
114+
Bson::JavaScriptCodeWithScope(_, _) => {
115+
"JavaScriptCodeWithScope".to_string()
112116
}
117+
Bson::JavaScriptCode(_) => "JavaScriptCode".to_string(),
118+
Bson::FloatingPoint(_) => "Double".to_string(),
119+
Bson::UtcDatetime(_) => "UtcDatetime".to_string(),
120+
Bson::Decimal128(_) => "Decimal128".to_string(),
121+
Bson::TimeStamp(_) => "Timestamp".to_string(),
122+
Bson::Binary(_, _) => "BinData".to_string(),
123+
Bson::RegExp(_, _) => "Regex".to_string(),
113124
Bson::Document(_) => "Document".to_string(),
114125
Bson::ObjectId(_) => "ObjectId".to_string(),
115126
Bson::Boolean(_) => "Boolean".to_string(),
127+
Bson::Symbol(_) => "Symbol".to_string(),
116128
Bson::String(_) => "String".to_string(),
117129
Bson::Array(_) => "Array".to_string(),
130+
Bson::I32(_) => "Int".to_string(),
131+
Bson::I64(_) => "Long".to_string(),
118132
Bson::Null => "Null".to_string(),
119-
_ => unimplemented!(),
120133
}
121134
}
122135

@@ -158,18 +171,11 @@ impl FieldType {
158171
match value {
159172
Bson::Array(arr) => {
160173
for val in arr.iter() {
161-
let value_type = Self::get_value(val);
162-
163-
if let Some(value_type) = value_type {
164-
self.values.push(value_type)
165-
}
174+
Self::get_value(val).map(|v| self.values.push(v));
166175
}
167176
}
168177
_ => {
169-
let value_type = Self::get_value(&value);
170-
if let Some(value_type) = value_type {
171-
self.values.push(value_type)
172-
}
178+
Self::get_value(&value).map(|v| self.values.push(v));
173179
}
174180
}
175181
}

Diff for: src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,9 @@ impl SchemaParser {
206206
/// println!("{}", schema);
207207
/// ```
208208
#[inline]
209-
pub fn to_json(&self) -> Result<String, failure::Error> {
210-
console::log_2(
211-
&"converting to Json".into(),
212-
&JsValue::from_str(&serde_json::to_string(&self).unwrap()),
213-
);
214-
Ok(serde_json::to_string(&self)?)
209+
pub fn into_json(mut self) -> Result<String, failure::Error> {
210+
let schema = self.flush();
211+
Ok(serde_json::to_string(&schema)?)
215212
}
216213

217214
#[inline]

Diff for: src/lib_wasm.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -63,43 +63,43 @@ impl SchemaParser {
6363
/// console.log(result) //
6464
/// ````
6565
#[wasm_bindgen(js_name = "toJson")]
66-
pub fn wasm_to_json(&mut self) -> Result<String, JsValue> {
67-
self.flush();
68-
match self.to_json() {
66+
pub fn wasm_into_json(self) -> Result<String, JsValue> {
67+
match self.into_json() {
6968
Err(e) => Err(JsValue::from_str(&format!("{}", e))),
7069
Ok(val) => Ok(val),
7170
}
7271
}
7372

74-
/// Wrapper method for `schema_parser.to_json()` to be used in JavaScript.
75-
/// `wasm_bindgen(js_name = "toJson")`
76-
///
77-
/// ```js, ignore
78-
/// import { SchemaParser } from "mongodb-schema-parser"
79-
///
80-
/// var schemaParser = new SchemaParser()
81-
/// var json = "{"name": "Nori", "type": "Cat"}"
82-
/// schemaParser.writeJson(json)
83-
/// // get the result as a json string
84-
/// var result = schemaParser.toObject()
85-
/// console.log(result) //
86-
/// ````
87-
#[wasm_bindgen(js_name = "toObject")]
88-
pub fn wasm_to_js_object(&mut self) -> Result<Object, JsValue> {
89-
self.flush();
90-
match self.to_js_object() {
91-
Err(e) => Err(JsValue::from_str(&format!("{}", e))),
92-
Ok(val) => Ok(val),
93-
}
94-
}
73+
// /// Wrapper method for `schema_parser.to_json()` to be used in JavaScript.
74+
// /// `wasm_bindgen(js_name = "toJson")`
75+
// ///
76+
// /// ```js, ignore
77+
// /// import { SchemaParser } from "mongodb-schema-parser"
78+
// ///
79+
// /// var schemaParser = new SchemaParser()
80+
// /// var json = "{"name": "Nori", "type": "Cat"}"
81+
// /// schemaParser.writeJson(json)
82+
// /// // get the result as a json string
83+
// /// var result = schemaParser.toObject()
84+
// /// console.log(result) //
85+
// /// ````
86+
// #[wasm_bindgen(js_name = "toObject")]
87+
// pub fn wasm_to_js_object(&mut self) -> Result<String, JsValue> {
88+
// // self.flush();
89+
// Ok(String::from("butts"))
90+
// // match self.to_js_object() {
91+
// // Err(e) => Err(JsValue::from_str(&format!("{}", e))),
92+
// // Ok(val) => Ok(val),
93+
// // }
94+
// }
9595

96-
fn to_js_object(&self) -> Result<Object, failure::Error> {
97-
let js_val = JsValue::from_serde(&serde_json::to_value(&self)?)?;
98-
let js_obj = Object::try_from(&js_val);
99-
if let Some(js_obj) = js_obj {
100-
Ok(js_obj.clone())
101-
} else {
102-
Err(format_err!("Cannot create JavaScript Object from Schema."))
103-
}
104-
}
96+
// fn to_js_object(&self) -> Result<Object, failure::Error> {
97+
// let js_val = JsValue::from_serde(&serde_json::to_value(&self)?)?;
98+
// let js_obj = Object::try_from(&js_val);
99+
// if let Some(js_obj) = js_obj {
100+
// Ok(js_obj.clone())
101+
// } else {
102+
// Err(format_err!("Cannot create JavaScript Object from Schema."))
103+
// }
104+
// }
105105
}

Diff for: src/value_type.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pub enum ValueType {
44
Str(String),
55
I32(i32),
66
I64(i64),
7+
Decimal128(String),
78
FloatingPoint(f64),
9+
Binary(Vec<u8>),
810
Boolean(bool),
11+
Null(String),
912
}

Diff for: tests/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn json_file_gen() -> Result<(), Error> {
1212
// this panics i think ?
1313
schema_parser.write_json(&json)?;
1414
}
15-
let schema = schema_parser.to_json();
15+
let schema = schema_parser.into_json();
1616
println!("{:?}", schema);
1717
Ok(())
1818
}

0 commit comments

Comments
 (0)