diff options
author | Paul Masurel <paul.masurel@gmail.com> | 2019-08-11 16:50:32 +0900 |
---|---|---|
committer | Paul Masurel <paul.masurel@gmail.com> | 2019-08-11 16:50:32 +0900 |
commit | 941f06eb9fda6ad0f6cfcc77043a09f66e78d465 (patch) | |
tree | dae17265893f38db124c41afb347502b5c765f4a | |
parent | 04832a86eb49fbfc351be2c34bb8ea6d81abe8f4 (diff) |
Added Schema.from_named_doc
-rw-r--r-- | src/query/query_parser/query_parser.rs | 4 | ||||
-rw-r--r-- | src/schema/field_type.rs | 2 | ||||
-rw-r--r-- | src/schema/schema.rs | 76 |
3 files changed, 77 insertions, 5 deletions
diff --git a/src/query/query_parser/query_parser.rs b/src/query/query_parser/query_parser.rs index 19993cc..e5b6f5e 100644 --- a/src/query/query_parser/query_parser.rs +++ b/src/query/query_parser/query_parser.rs @@ -768,13 +768,13 @@ mod test { test_parse_query_to_logical_ast_helper( "signed:{-5 TO 3}", "(Excluded(Term([0, 0, 0, 2, 127, 255, 255, 255, 255, 255, 255, 251])) TO \ - Excluded(Term([0, 0, 0, 2, 128, 0, 0, 0, 0, 0, 0, 3])))", + Excluded(Term([0, 0, 0, 2, 128, 0, 0, 0, 0, 0, 0, 3])))", false, ); test_parse_query_to_logical_ast_helper( "float:{-1.5 TO 1.5}", "(Excluded(Term([0, 0, 0, 10, 64, 7, 255, 255, 255, 255, 255, 255])) TO \ - Excluded(Term([0, 0, 0, 10, 191, 248, 0, 0, 0, 0, 0, 0])))", + Excluded(Term([0, 0, 0, 10, 191, 248, 0, 0, 0, 0, 0, 0])))", false, ); diff --git a/src/schema/field_type.rs b/src/schema/field_type.rs index bb4e935..72e5c06 100644 --- a/src/schema/field_type.rs +++ b/src/schema/field_type.rs @@ -10,7 +10,7 @@ use serde_json::Value as JsonValue; /// Possible error that may occur while parsing a field value /// At this point the JSON is known to be valid. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum ValueParsingError { /// Encountered a numerical value that overflows or underflow its integer type. OverflowError(String), diff --git a/src/schema/schema.rs b/src/schema/schema.rs index 328b4d3..5b657af 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -247,6 +247,25 @@ impl Schema { } /// Create a named document off the doc. + pub fn from_named_doc( + &self, + named_doc: NamedFieldDocument, + ) -> Result<Document, DocParsingError> { + let mut document = Document::new(); + for (field_name, values) in named_doc.0 { + if let Some(field) = self.get_field(&field_name) { + for value in values { + let field_value = FieldValue::new(field, value); + document.add(field_value); + } + } else { + return Err(DocParsingError::NoSuchFieldInSchema(field_name)); + } + } + Ok(document) + } + + /// Create a named document off the doc. pub fn to_named_doc(&self, doc: &Document) -> NamedFieldDocument { let mut field_map = BTreeMap::new(); for (field, field_values) in doc.get_sorted_field_values() { @@ -360,7 +379,7 @@ impl<'de> Deserialize<'de> for Schema { /// Error that may happen when deserializing /// a document from JSON. -#[derive(Debug, Fail)] +#[derive(Debug, Fail, PartialEq)] pub enum DocParsingError { /// The payload given is not valid JSON. #[fail(display = "The provided string is not valid JSON")] @@ -369,7 +388,10 @@ pub enum DocParsingError { #[fail(display = "The field '{:?}' could not be parsed: {:?}", _0, _1)] ValueError(String, ValueParsingError), /// The json-document contains a field that is not declared in the schema. - #[fail(display = "The json-document contains an unknown field: {:?}", _0)] + #[fail( + display = "The document contains a field that is not declared in the schema: {:?}", + _0 + )] NoSuchFieldInSchema(String), } @@ -381,6 +403,7 @@ mod tests { use crate::schema::*; use matches::{assert_matches, matches}; use serde_json; + use std::collections::BTreeMap; #[test] pub fn is_indexed_test() { @@ -496,6 +519,55 @@ mod tests { } #[test] + pub fn test_document_from_nameddoc() { + let mut schema_builder = Schema::builder(); + let title = schema_builder.add_text_field("title", TEXT); + let val = schema_builder.add_i64_field("val", INDEXED); + let schema = schema_builder.build(); + let mut named_doc_map = BTreeMap::default(); + named_doc_map.insert( + "title".to_string(), + vec![Value::from("title1"), Value::from("title2")], + ); + named_doc_map.insert( + "val".to_string(), + vec![Value::from(14u64), Value::from(-1i64)], + ); + let doc = schema + .from_named_doc(NamedFieldDocument(named_doc_map)) + .unwrap(); + assert_eq!( + doc.get_all(title), + vec![ + &Value::from("title1".to_string()), + &Value::from("title2".to_string()) + ] + ); + assert_eq!( + doc.get_all(val), + vec![&Value::from(14u64), &Value::from(-1i64)] + ); + } + + #[test] + pub fn test_document_from_nameddoc_error() { + let mut schema_builder = Schema::builder(); + let schema = schema_builder.build(); + let mut named_doc_map = BTreeMap::default(); + named_doc_map.insert( + "title".to_string(), + vec![Value::from("title1"), Value::from("title2")], + ); + let err = schema + .from_named_doc(NamedFieldDocument(named_doc_map)) + .unwrap_err(); + assert_eq!( + err, + DocParsingError::NoSuchFieldInSchema("title".to_string()) + ); + } + + #[test] pub fn test_parse_document() { let mut schema_builder = Schema::builder(); let count_options = IntOptions::default() |