mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-25 11:35:05 +08:00
Refactor empty arrays/objects should return empty instead of null
This commit is contained in:
parent
462b4654c4
commit
393be40179
@ -186,12 +186,16 @@ fn create_value(value: &Document, mut selectors: HashSet<&str>) -> Document {
|
|||||||
let array = create_array(array, &sub_selectors);
|
let array = create_array(array, &sub_selectors);
|
||||||
if !array.is_empty() {
|
if !array.is_empty() {
|
||||||
new_value.insert(key.to_string(), array.into());
|
new_value.insert(key.to_string(), array.into());
|
||||||
|
} else {
|
||||||
|
new_value.insert(key.to_string(), Value::Array(vec![]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Object(object) => {
|
Value::Object(object) => {
|
||||||
let object = create_value(object, sub_selectors);
|
let object = create_value(object, sub_selectors);
|
||||||
if !object.is_empty() {
|
if !object.is_empty() {
|
||||||
new_value.insert(key.to_string(), object.into());
|
new_value.insert(key.to_string(), object.into());
|
||||||
|
} else {
|
||||||
|
new_value.insert(key.to_string(), Value::Object(Map::new()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
@ -211,6 +215,8 @@ fn create_array(array: &[Value], selectors: &HashSet<&str>) -> Vec<Value> {
|
|||||||
let array = create_array(array, selectors);
|
let array = create_array(array, selectors);
|
||||||
if !array.is_empty() {
|
if !array.is_empty() {
|
||||||
res.push(array.into());
|
res.push(array.into());
|
||||||
|
} else {
|
||||||
|
res.push(Value::Array(vec![]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Object(object) => {
|
Value::Object(object) => {
|
||||||
@ -637,6 +643,24 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_array_object_return_empty() {
|
||||||
|
let value: Value = json!({
|
||||||
|
"array": [],
|
||||||
|
"object": {},
|
||||||
|
});
|
||||||
|
let value: &Document = value.as_object().unwrap();
|
||||||
|
|
||||||
|
let res: Value = select_values(value, vec!["array.name", "object.name"]).into();
|
||||||
|
assert_eq!(
|
||||||
|
res,
|
||||||
|
json!({
|
||||||
|
"array": [],
|
||||||
|
"object": {},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn all_conflict_variation() {
|
fn all_conflict_variation() {
|
||||||
let value: Value = json!({
|
let value: Value = json!({
|
||||||
|
Loading…
Reference in New Issue
Block a user