Skip to content

Commit

Permalink
perf: test nesting level only once
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Jan 5, 2025
1 parent 4a47e07 commit bd7e652
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
16 changes: 2 additions & 14 deletions src/utils/json/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub enum JsonFlattenError {
ExpectedObjectInArray,
#[error("Found non-object element while flattening array of objects")]
NonObjectInArray,
#[error("heavily nested, cannot flatten this JSON")]
HeavilyNestedJson,
}

// Recursively flattens JSON objects and arrays, e.g. with the separator `.`, starting from the TOP
Expand Down Expand Up @@ -285,10 +283,6 @@ pub fn flatten_array_objects(
/// 4. `{"a": [{"b": 1}, {"c": 2}], "d": {"e": 4}}` ~> `[{"a": {"b":1}, "d": {"e":4}}, {"a": {"c":2}, "d": {"e":4}}]`
/// 5. `{"a":{"b":{"c":{"d":{"e":["a","b"]}}}}}` ~> returns error - heavily nested, cannot flatten this JSON
pub fn generic_flattening(value: &Value) -> Result<Vec<Value>, JsonFlattenError> {
if has_more_than_four_levels(value, 1) {
return Err(JsonFlattenError::HeavilyNestedJson);
}

match value {
Value::Array(arr) => Ok(arr
.iter()
Expand Down Expand Up @@ -342,7 +336,7 @@ pub fn generic_flattening(value: &Value) -> Result<Vec<Value>, JsonFlattenError>
/// example -
/// 1. `{"a":{"b":{"c":{"d":{"e":["a","b"]}}}}}` ~> returns true
/// 2. `{"a": [{"b": 1}, {"c": 2}], "d": {"e": 4}}` ~> returns false
fn has_more_than_four_levels(value: &Value, current_level: usize) -> bool {
pub fn has_more_than_four_levels(value: &Value, current_level: usize) -> bool {
if current_level > 4 {
return true;
}
Expand Down Expand Up @@ -649,15 +643,9 @@ mod tests {
}

#[test]
fn flatten_json_success() {
fn flatten_json() {
let value = json!({"a":{"b":{"e":["a","b"]}}});
let expected = vec![json!({"a":{"b":{"e":"a"}}}), json!({"a":{"b":{"e":"b"}}})];
assert_eq!(generic_flattening(&value).unwrap(), expected);
}

#[test]
fn flatten_json_error() {
let value = json!({"a":{"b":{"c":{"d":{"e":["a","b"]}}}}});
assert!(generic_flattening(&value).is_err());
}
}
14 changes: 6 additions & 8 deletions src/utils/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use std::num::NonZeroU32;

use flatten::{convert_to_array, generic_flattening};
use flatten::{convert_to_array, generic_flattening, has_more_than_four_levels};
use serde_json;
use serde_json::Value;

Expand All @@ -37,14 +37,12 @@ pub fn flatten_json_body(
schema_version: SchemaVersion,
validation_required: bool,
) -> Result<Value, anyhow::Error> {
let mut nested_value = if schema_version == SchemaVersion::V1 {
if let Ok(flattened_json) = generic_flattening(&body) {
convert_to_array(flattened_json)?
} else {
body
}
} else {
// Flatten the json body only if new schema and has less than 4 levels of nesting
let mut nested_value = if schema_version == SchemaVersion::V0 || has_more_than_four_levels(&body, 1) {
body
} else {
let flattened_json = generic_flattening(&body)?;
convert_to_array(flattened_json)?
};
flatten::flatten(
&mut nested_value,
Expand Down

0 comments on commit bd7e652

Please sign in to comment.