diff --git a/glide-core/src/client/value_conversion.rs b/glide-core/src/client/value_conversion.rs index b8ca830936..c3c1c2ad9c 100644 --- a/glide-core/src/client/value_conversion.rs +++ b/glide-core/src/client/value_conversion.rs @@ -584,24 +584,28 @@ pub(crate) fn convert_to_expected_type( } Value::Array(outer_array) if matches!(outer_array[0], Value::Array(_) | Value::Map(_) ) => { - let first_inner_item = &outer_array[0]; - let mut converted_map = convert_to_expected_type(first_inner_item.clone(), Some(ExpectedReturnType::FunctionStatsReturnType))?; - - for inner_item in outer_array { - if let Value::Map(map) = converted_map.clone() { - if let Some((key, running_script_value)) = map.first() { - if key == &Value::BulkString(b"running_script".into()) && running_script_value != &Value::Nil { - // we have found a response with an active running script - // the server can only run a single script / function at a time - // we can return this nodes response - break; - } - } + // Helper function to check if an item contains a non-null `running_script` + fn contains_running_script(value: &Value) -> bool { + match value { + Value::Array(inner_array) => inner_array.get(1) != Some(&Value::Nil), + Value::Map(inner_map) => inner_map.get(0).map(|(_, v)| v) != Some(&Value::Nil), + _ => false, + } + } + + let mut converted_map: Option = None; + + for inner_item in &outer_array { + if contains_running_script(inner_item) { + converted_map = Some(convert_to_expected_type(inner_item.clone(), Some(ExpectedReturnType::FunctionStatsReturnType))?); + // we have found a response with an active running script + // the server can only run a single script / function at a time + break; // We can return this node's response } - converted_map = convert_to_expected_type(inner_item.clone(), Some(ExpectedReturnType::FunctionStatsReturnType))?; } - Ok(converted_map) + // Return the converted map if found, otherwise convert the first item in the array + Ok(converted_map.unwrap_or_else(|| convert_to_expected_type(outer_array[0].clone(), Some(ExpectedReturnType::FunctionStatsReturnType)).unwrap())) }, Value::Array(mut array) if array.len() == 4 => { let mut result: Vec<(Value, Value)> = Vec::with_capacity(2); @@ -2469,11 +2473,6 @@ mod tests { #[test] fn convert_function_stats_standalone_response() { - assert!(matches!( - expected_type_for_cmd(redis::cmd("FUNCTION").arg("STATS")), - Some(ExpectedReturnType::FunctionStatsReturnType) - )); - let resp2_response = Value::Array(vec![ Value::Array(vec![ Value::BulkString(b"running_script".into()),