From 8eaf3756961d0c06176b98de92b778e89ab2761e Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Tue, 17 Dec 2024 11:52:03 +0200 Subject: [PATCH] refactor json_to_array_buffer() --- .../cubejs-backend-native/src/node_export.rs | 66 +++++++------------ .../src/node_obj_deserializer.rs | 3 +- 2 files changed, 25 insertions(+), 44 deletions(-) diff --git a/packages/cubejs-backend-native/src/node_export.rs b/packages/cubejs-backend-native/src/node_export.rs index 68bc32f11307f..f7af6da7c8865 100644 --- a/packages/cubejs-backend-native/src/node_export.rs +++ b/packages/cubejs-backend-native/src/node_export.rs @@ -514,6 +514,27 @@ fn debug_js_to_clrepr_to_js(mut cx: FunctionContext) -> JsResult { //============ sql orchestrator =================== +fn json_to_array_buffer<'a, C>( + mut cx: C, + json_data: Result, +) -> JsResult<'a, JsArrayBuffer> +where + C: Context<'a>, +{ + match json_data { + Ok(json_data) => { + let json_bytes = json_data.as_bytes(); + let mut js_buffer = cx.array_buffer(json_bytes.len())?; + { + let buffer = js_buffer.as_mut_slice(&mut cx); + buffer.copy_from_slice(json_bytes); + } + Ok(js_buffer) + } + Err(err) => cx.throw_error(err.to_string()), + } +} + fn parse_cubestore_ws_result_message(mut cx: FunctionContext) -> JsResult { let msg = cx.argument::(0)?; let msg_data = msg.as_slice(&cx).to_vec(); @@ -617,20 +638,7 @@ fn final_cubestore_result(mut cx: FunctionContext) -> JsResult { Err(err) => Err(anyhow::Error::from(err)), } }) - .promise(move |mut cx, json_string| match json_string { - Ok(json_string) => { - let json_bytes = json_string.as_bytes(); - - let mut js_buffer = cx.array_buffer(json_bytes.len())?; - { - let buffer = js_buffer.as_mut_slice(&mut cx); - buffer.copy_from_slice(json_bytes); - } - - Ok(js_buffer) - } - Err(err) => cx.throw_error(err.to_string()), - }); + .promise(move |cx, json_data| json_to_array_buffer(cx, json_data)); Ok(promise) } @@ -679,20 +687,7 @@ fn final_cubestore_result_array(mut cx: FunctionContext) -> JsResult Err(err) => Err(anyhow::Error::from(err)), } }) - .promise(move |mut cx, json_data| match json_data { - Ok(json_data) => { - let json_bytes = json_data.as_bytes(); - - let mut js_buffer = cx.array_buffer(json_bytes.len())?; - { - let buffer = js_buffer.as_mut_slice(&mut cx); - buffer.copy_from_slice(json_bytes); - } - - Ok(js_buffer) - } - Err(err) => cx.throw_error(err.to_string()), - }); + .promise(move |cx, json_data| json_to_array_buffer(cx, json_data)); Ok(promise) } @@ -733,20 +728,7 @@ fn final_cubestore_result_multi(mut cx: FunctionContext) -> JsResult Err(err) => Err(anyhow::Error::from(err)), } }) - .promise(move |mut cx, json_data| match json_data { - Ok(json_data) => { - let json_bytes = json_data.as_bytes(); - - let mut js_buffer = cx.array_buffer(json_bytes.len())?; - { - let buffer = js_buffer.as_mut_slice(&mut cx); - buffer.copy_from_slice(json_bytes); - } - - Ok(js_buffer) - } - Err(err) => cx.throw_error(err.to_string()), - }); + .promise(move |cx, json_data| json_to_array_buffer(cx, json_data)); Ok(promise) } diff --git a/packages/cubejs-backend-native/src/node_obj_deserializer.rs b/packages/cubejs-backend-native/src/node_obj_deserializer.rs index c30d86796a559..3b666599e568f 100644 --- a/packages/cubejs-backend-native/src/node_obj_deserializer.rs +++ b/packages/cubejs-backend-native/src/node_obj_deserializer.rs @@ -255,8 +255,7 @@ impl<'de, 'a, 'b> Deserializer<'de> for JsValueDeserializer<'a, 'b> { forward_to_deserialize_any! { bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string - bytes byte_buf - identifier ignored_any + bytes byte_buf identifier ignored_any } }