Skip to content

Commit

Permalink
updated to return error for non object array
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsinhaparseable committed Dec 4, 2024
1 parent 7004c8a commit 02bd556
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
23 changes: 5 additions & 18 deletions src/handlers/http/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,7 @@ mod tests {
use std::{collections::HashMap, sync::Arc};

use actix_web::test::TestRequest;
use arrow_array::{
types::Int64Type, ArrayRef, Float64Array, Int64Array, ListArray, StringArray,
};
use arrow_array::{ArrayRef, Float64Array, Int64Array, StringArray};
use arrow_schema::{DataType, Field};
use serde_json::json;

Expand Down Expand Up @@ -689,25 +687,14 @@ mod tests {
])
);

let c_a = vec![None, None, Some(vec![Some(1i64)]), Some(vec![Some(1)])];
let c_b = vec![None, None, None, Some(vec![Some(2i64)])];

assert_eq!(
rb.column_by_name("c_a")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap(),
&ListArray::from_iter_primitive::<Int64Type, _, _>(c_a)
rb.column_by_name("c_a").unwrap().as_int64_arr(),
&Int64Array::from(vec![None, None, Some(1), Some(1)])
);

assert_eq!(
rb.column_by_name("c_b")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap(),
&ListArray::from_iter_primitive::<Int64Type, _, _>(c_b)
rb.column_by_name("c_b").unwrap().as_int64_arr(),
&Int64Array::from(vec![None, None, None, Some(2)])
);
}
}
16 changes: 10 additions & 6 deletions src/utils/json/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,20 @@ pub fn flatten_json(value: &Value) -> Vec<Value> {
}
}

pub fn convert_to_array(flatterned: Vec<Value>) -> Value {
pub fn convert_to_array(flattened: Vec<Value>) -> Result<Value, anyhow::Error> {
let mut result = Vec::new();
for item in flatterned {
for item in flattened {
let mut map = Map::new();
for (key, value) in item.as_object().unwrap() {
map.insert(key.clone(), value.clone());
if let Some(item) = item.as_object() {
for (key, value) in item {
map.insert(key.clone(), value.clone());
}
result.push(Value::Object(map));
} else {
return Err(anyhow!("Expected object in array of objects"));
}
result.push(Value::Object(map));
}
Value::Array(result)
Ok(Value::Array(result))
}
#[cfg(test)]
mod tests {
Expand Down
20 changes: 11 additions & 9 deletions src/utils/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ pub fn flatten_json_body(
custom_partition: Option<String>,
validation_required: bool,
) -> Result<Value, anyhow::Error> {
let nested_value = flatten::convert_to_array(flatten::flatten_json(&body));
flatten::flatten(
nested_value,
"_",
time_partition,
time_partition_limit,
custom_partition,
validation_required,
)
match flatten::convert_to_array(flatten::flatten_json(&body)) {
Ok(nested_value) => flatten::flatten(
nested_value,
"_",
time_partition,
time_partition_limit,
custom_partition,
validation_required,
),
Err(err) => Err(err),
}
}

pub fn convert_array_to_object(
Expand Down

0 comments on commit 02bd556

Please sign in to comment.