mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 14:54:27 +01:00
Prefer using direct method calls instead of using the json macros
This commit is contained in:
parent
436d2032c4
commit
b3cec1a383
@ -1,6 +1,6 @@
|
|||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
use serde_json::{json, Map, Value};
|
use serde_json::{Map, Value};
|
||||||
|
|
||||||
pub fn flatten(json: &Map<String, Value>) -> Map<String, Value> {
|
pub fn flatten(json: &Map<String, Value>) -> Map<String, Value> {
|
||||||
let mut obj = Map::new();
|
let mut obj = Map::new();
|
||||||
@ -42,7 +42,7 @@ fn insert_value(base_json: &mut Map<String, Value>, key: &str, to_insert: Value)
|
|||||||
debug_assert!(!to_insert.is_object());
|
debug_assert!(!to_insert.is_object());
|
||||||
debug_assert!(!to_insert.is_array());
|
debug_assert!(!to_insert.is_array());
|
||||||
|
|
||||||
// does the field aleardy exists?
|
// does the field already exists?
|
||||||
if let Some(value) = base_json.get_mut(key) {
|
if let Some(value) = base_json.get_mut(key) {
|
||||||
// is it already an array
|
// is it already an array
|
||||||
if let Some(array) = value.as_array_mut() {
|
if let Some(array) = value.as_array_mut() {
|
||||||
@ -50,16 +50,18 @@ fn insert_value(base_json: &mut Map<String, Value>, key: &str, to_insert: Value)
|
|||||||
// or is there a collision
|
// or is there a collision
|
||||||
} else {
|
} else {
|
||||||
let value = std::mem::take(value);
|
let value = std::mem::take(value);
|
||||||
base_json[key] = json!([value, to_insert]);
|
base_json[key] = Value::Array(vec![value, to_insert]);
|
||||||
}
|
}
|
||||||
// if it does not exist we can push the value untouched
|
// if it does not exist we can push the value untouched
|
||||||
} else {
|
} else {
|
||||||
base_json.insert(key.to_string(), json!(to_insert));
|
base_json.insert(key.to_string(), to_insert);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user