Fix i64 overflow check and merge NotJSON with NotJSONObject

This commit is contained in:
Laurentiu Nicola
2017-05-08 07:09:54 +03:00
parent 2c798e3147
commit 7b733dd34f
2 changed files with 26 additions and 25 deletions

View File

@@ -65,16 +65,11 @@ impl FieldType {
JsonValue::Number(ref field_val_num) => {
match *self {
FieldType::I64(_) => {
if let Some(field_val_u64) = field_val_num.as_u64() {
if field_val_u64 > (i64::max_value() as u64) {
Err(ValueParsingError::OverflowError(format!("Expected i64, but value {:?} overflows.", field_val_u64)))
}
else {
Ok(Value::I64(field_val_u64 as i64))
}
if let Some(field_val_i64) = field_val_num.as_i64() {
Ok(Value::I64(field_val_i64))
}
else {
Err(ValueParsingError::TypeError(format!("Expected a u32 int, got {:?}", json)))
Err(ValueParsingError::OverflowError(format!("Expected an i64 int, got {:?}", json)))
}
}
FieldType::U64(_) => {
@@ -82,7 +77,7 @@ impl FieldType {
Ok(Value::U64(field_val_u64))
}
else {
Err(ValueParsingError::TypeError(format!("Expected a u64 int, got {:?}", json)))
Err(ValueParsingError::TypeError(format!("Expected an u64 int, got {:?}", json)))
}
}
FieldType::Str(_) => {

View File

@@ -226,7 +226,7 @@ impl Schema {
else {
format!("{:?}...", &doc_json[0..20])
};
DocParsingError::NotJSONObject(doc_json_sample)
DocParsingError::NotJSON(doc_json_sample)
})?;
let mut doc = Document::default();
@@ -335,22 +335,13 @@ impl From<SchemaBuilder> for Schema {
#[derive(Debug)]
pub enum DocParsingError {
/// The payload given is not valid JSON.
NotJSON(serde_json::Error),
/// The payload given is not a JSON Object (`{...}`).
NotJSONObject(String),
NotJSON(String),
/// One of the value node could not be parsed.
ValueError(String, ValueParsingError),
/// The json-document contains a field that is not declared in the schema.
NoSuchFieldInSchema(String),
}
impl From<serde_json::Error> for DocParsingError {
fn from(err: serde_json::Error) -> DocParsingError {
DocParsingError::NotJSON(err)
}
}
#[cfg(test)]
mod tests {
@@ -358,7 +349,7 @@ mod tests {
use schema::*;
use serde_json;
use schema::field_type::ValueParsingError;
use schema::schema::DocParsingError::{NotJSON, NotJSONObject};
use schema::schema::DocParsingError::NotJSON;
#[test]
pub fn test_schema_serialization() {
@@ -527,7 +518,7 @@ mod tests {
}"#);
match json_err {
Err(DocParsingError::ValueError(_, ValueParsingError::OverflowError(_))) => {
assert!(false);
panic!("expected 9223372036854775808 to fit into u64, but it didn't");
}
_ => {
assert!(true);
@@ -539,14 +530,29 @@ mod tests {
"title": "my title",
"author": "fulmicoton",
"count": 50,
"popularity": 9223372036854775808,
"popularity": 9223372036854775808
}"#);
match json_err {
Err(NotJSON(_)) | Err(NotJSONObject(_)) => {
Err(DocParsingError::ValueError(_, ValueParsingError::OverflowError(_))) => {
assert!(true);
},
_ => {
panic!("expected overflow but didn't");
panic!("expected 9223372036854775808 to overflow i64, but it didn't");
}
}
}
{
let json_err = schema.parse_document(r#"{
"title": "my title",
"author": "fulmicoton",
"count": 50,
}"#);
match json_err {
Err(NotJSON(_)) => {
assert!(true);
},
_ => {
panic!("expected invalid JSON to fail parsing, but it didn't");
}
}
}