Skip to content

Commit

Permalink
refactor json_to_array_buffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Dec 17, 2024
1 parent 0c89ee8 commit 8eaf375
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
66 changes: 24 additions & 42 deletions packages/cubejs-backend-native/src/node_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,27 @@ fn debug_js_to_clrepr_to_js(mut cx: FunctionContext) -> JsResult<JsValue> {

//============ sql orchestrator ===================

fn json_to_array_buffer<'a, C>(
mut cx: C,
json_data: Result<String, anyhow::Error>,
) -> 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<JsPromise> {
let msg = cx.argument::<JsBuffer>(0)?;
let msg_data = msg.as_slice(&cx).to_vec();
Expand Down Expand Up @@ -617,20 +638,7 @@ fn final_cubestore_result(mut cx: FunctionContext) -> JsResult<JsPromise> {
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)
}
Expand Down Expand Up @@ -679,20 +687,7 @@ fn final_cubestore_result_array(mut cx: FunctionContext) -> JsResult<JsPromise>
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)
}
Expand Down Expand Up @@ -733,20 +728,7 @@ fn final_cubestore_result_multi(mut cx: FunctionContext) -> JsResult<JsPromise>
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)
}
Expand Down
3 changes: 1 addition & 2 deletions packages/cubejs-backend-native/src/node_obj_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 8eaf375

Please sign in to comment.