From e2b115f3a96a74f86f83907f3d7ec86bab7f133b Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 19 May 2020 14:16:29 +0200 Subject: [PATCH] Improve Number extraction/conversion function --- meilisearch-core/src/update/helpers.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/meilisearch-core/src/update/helpers.rs b/meilisearch-core/src/update/helpers.rs index a0a0ee266..d17bea3b2 100644 --- a/meilisearch-core/src/update/helpers.rs +++ b/meilisearch-core/src/update/helpers.rs @@ -4,6 +4,7 @@ use std::hash::{Hash, Hasher}; use indexmap::IndexMap; use meilisearch_schema::IndexedPos; use meilisearch_types::DocumentId; +use ordered_float::OrderedFloat; use serde_json::Value; use siphasher::sip::SipHasher; @@ -81,7 +82,14 @@ pub fn value_to_number(value: &Value) -> Option { match value { Value::Null => None, Value::Bool(boolean) => Some(Number::Unsigned(*boolean as u64)), - Value::Number(number) => Number::from_str(&number.to_string()).ok(), // TODO improve that + Value::Number(number) => { + match (number.as_i64(), number.as_u64(), number.as_f64()) { + (Some(n), _, _) => Some(Number::Signed(n)), + (_, Some(n), _) => Some(Number::Unsigned(n)), + (_, _, Some(n)) => Some(Number::Float(OrderedFloat(n))), + (None, None, None) => None, + } + }, Value::String(string) => Number::from_str(string).ok(), Value::Array(_array) => None, Value::Object(_object) => None,