From 02dab516e2e4b488cb48e5d04fcec5a6c1ab344a Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Fri, 5 Apr 2024 12:14:47 +0200 Subject: [PATCH] refactor: Don't use untyped JsObject in QueryResult --- .../runtime/src/napi_interface/query.rs | 29 ++++++++++++------- .../src/generated/napi_interface/query.rs | 29 ++++++++++++------- .../src/generated/napi_interface/query.rs | 29 ++++++++++++------- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/crates/codegen/parser/runtime/src/napi_interface/query.rs b/crates/codegen/parser/runtime/src/napi_interface/query.rs index 0a91717a97..f9042073db 100644 --- a/crates/codegen/parser/runtime/src/napi_interface/query.rs +++ b/crates/codegen/parser/runtime/src/napi_interface/query.rs @@ -3,7 +3,10 @@ // The functions are meant to be definitions for export, so they're not really used #![allow(clippy::return_self_not_must_use)] -use napi::{Env, JsObject}; +use std::collections::HashMap; + +use napi::bindgen_prelude::ClassInstance; +use napi::Env; use napi_derive::napi; use crate::napi_interface::cursor::Cursor; @@ -36,22 +39,28 @@ pub struct QueryResultIterator(RustQueryResultIterator); pub struct QueryResult { pub query_number: u32, #[napi(ts_type = "{ [key: string]: cursor.Cursor[] }")] - pub bindings: JsObject, + pub bindings: HashMap>>, } impl QueryResult { fn new(env: Env, result: RustQueryResult) -> napi::Result { #[allow(clippy::cast_possible_truncation)] let query_number = result.query_number as u32; - let mut bindings = env.create_object()?; - // transer all of the bindings eagerly on the assumption + // transfer all of the bindings eagerly on the assumption // that they've all been explicitly requested. - for (key, value) in result.bindings { - bindings.set_named_property( - &key, - value.into_iter().map(|x| x.into()).collect::>(), - )?; - } + let bindings = result + .bindings + .into_iter() + .map(|(key, values)| { + let instances = values + .into_iter() + .map(|cursor| Cursor(cursor).into_instance(env)) + .collect::>()?; + + Ok((key, instances)) + }) + .collect::>()?; + Ok(Self { query_number, bindings, diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/query.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/query.rs index bd5daaba67..795651013b 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/query.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/query.rs @@ -5,7 +5,10 @@ // The functions are meant to be definitions for export, so they're not really used #![allow(clippy::return_self_not_must_use)] -use napi::{Env, JsObject}; +use std::collections::HashMap; + +use napi::bindgen_prelude::ClassInstance; +use napi::Env; use napi_derive::napi; use crate::napi_interface::cursor::Cursor; @@ -38,22 +41,28 @@ pub struct QueryResultIterator(RustQueryResultIterator); pub struct QueryResult { pub query_number: u32, #[napi(ts_type = "{ [key: string]: cursor.Cursor[] }")] - pub bindings: JsObject, + pub bindings: HashMap>>, } impl QueryResult { fn new(env: Env, result: RustQueryResult) -> napi::Result { #[allow(clippy::cast_possible_truncation)] let query_number = result.query_number as u32; - let mut bindings = env.create_object()?; - // transer all of the bindings eagerly on the assumption + // transfer all of the bindings eagerly on the assumption // that they've all been explicitly requested. - for (key, value) in result.bindings { - bindings.set_named_property( - &key, - value.into_iter().map(|x| x.into()).collect::>(), - )?; - } + let bindings = result + .bindings + .into_iter() + .map(|(key, values)| { + let instances = values + .into_iter() + .map(|cursor| Cursor(cursor).into_instance(env)) + .collect::>()?; + + Ok((key, instances)) + }) + .collect::>()?; + Ok(Self { query_number, bindings, diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/query.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/query.rs index bd5daaba67..795651013b 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/query.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/query.rs @@ -5,7 +5,10 @@ // The functions are meant to be definitions for export, so they're not really used #![allow(clippy::return_self_not_must_use)] -use napi::{Env, JsObject}; +use std::collections::HashMap; + +use napi::bindgen_prelude::ClassInstance; +use napi::Env; use napi_derive::napi; use crate::napi_interface::cursor::Cursor; @@ -38,22 +41,28 @@ pub struct QueryResultIterator(RustQueryResultIterator); pub struct QueryResult { pub query_number: u32, #[napi(ts_type = "{ [key: string]: cursor.Cursor[] }")] - pub bindings: JsObject, + pub bindings: HashMap>>, } impl QueryResult { fn new(env: Env, result: RustQueryResult) -> napi::Result { #[allow(clippy::cast_possible_truncation)] let query_number = result.query_number as u32; - let mut bindings = env.create_object()?; - // transer all of the bindings eagerly on the assumption + // transfer all of the bindings eagerly on the assumption // that they've all been explicitly requested. - for (key, value) in result.bindings { - bindings.set_named_property( - &key, - value.into_iter().map(|x| x.into()).collect::>(), - )?; - } + let bindings = result + .bindings + .into_iter() + .map(|(key, values)| { + let instances = values + .into_iter() + .map(|cursor| Cursor(cursor).into_instance(env)) + .collect::>()?; + + Ok((key, instances)) + }) + .collect::>()?; + Ok(Self { query_number, bindings,