From 4529b463b5de68a3ef00e0664925422b08f6464c Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 16 Sep 2024 15:45:02 +0100 Subject: [PATCH] array parsing by value --- proxy/src/serverless/json.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/proxy/src/serverless/json.rs b/proxy/src/serverless/json.rs index ab6c718b64..9d7a450874 100644 --- a/proxy/src/serverless/json.rs +++ b/proxy/src/serverless/json.rs @@ -1,5 +1,6 @@ use std::fmt; +use itertools::Itertools; use serde::de; use serde::Deserialize; use serde::Deserializer; @@ -433,7 +434,7 @@ fn json_value_to_pg_text(value: Value) -> Option { Value::String(s) => Some(s), // special care for arrays - Value::Array(_) => json_array_to_pg_array(&value), + Value::Array(arr) => Some(json_array_to_pg_array(arr)), } } @@ -445,7 +446,17 @@ fn json_value_to_pg_text(value: Value) -> Option { // // Example of the same escaping in node-postgres: packages/pg/lib/utils.js // -fn json_array_to_pg_array(value: &Value) -> Option { +fn json_array_to_pg_array(arr: Vec) -> String { + let vals = arr + .into_iter() + .map(json_array_value_to_pg_array) + .map(|v| v.unwrap_or_else(|| "NULL".to_string())) + .join(","); + + format!("{{{vals}}}") +} + +fn json_array_value_to_pg_array(value: Value) -> Option { match value { // special care for nulls Value::Null => None, @@ -453,19 +464,10 @@ fn json_array_to_pg_array(value: &Value) -> Option { // convert to text with escaping // here string needs to be escaped, as it is part of the array v @ (Value::Bool(_) | Value::Number(_) | Value::String(_)) => Some(v.to_string()), - v @ Value::Object(_) => json_array_to_pg_array(&Value::String(v.to_string())), + v @ Value::Object(_) => json_array_value_to_pg_array(Value::String(v.to_string())), // recurse into array - Value::Array(arr) => { - let vals = arr - .iter() - .map(json_array_to_pg_array) - .map(|v| v.unwrap_or_else(|| "NULL".to_string())) - .collect::>() - .join(","); - - Some(format!("{{{vals}}}")) - } + Value::Array(arr) => Some(json_array_to_pg_array(arr)), } }