From d3622856c7518fab8a67481ebb19c8716d35478f Mon Sep 17 00:00:00 2001 From: Stefan Date: Fri, 27 Sep 2024 15:18:53 +0200 Subject: [PATCH] fix bindings and improve clone accuracy --- bindings/nodejs/src/custom_node.rs | 4 ++-- bindings/nodejs/src/decision.rs | 5 +++-- bindings/nodejs/src/engine.rs | 5 +++-- bindings/nodejs/src/expression.rs | 12 +++++++----- bindings/nodejs/src/types.rs | 11 ++++++----- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/bindings/nodejs/src/custom_node.rs b/bindings/nodejs/src/custom_node.rs index 481d5331..e8426b9e 100644 --- a/bindings/nodejs/src/custom_node.rs +++ b/bindings/nodejs/src/custom_node.rs @@ -31,7 +31,7 @@ impl CustomNodeAdapter for CustomNode { let promise: Promise = function .clone() .call_async(ZenEngineHandlerRequest { - input: request.input.clone(), + input: request.input.to_value(), node: node_data, }) .await @@ -40,7 +40,7 @@ impl CustomNodeAdapter for CustomNode { let result = promise.await.map_err(|err| anyhow!(err.reason))?; Ok(NodeResponse { - output: result.output, + output: result.output.into(), trace_data: result.trace_data, }) } diff --git a/bindings/nodejs/src/decision.rs b/bindings/nodejs/src/decision.rs index d9cf4ff6..0e7a0a18 100644 --- a/bindings/nodejs/src/decision.rs +++ b/bindings/nodejs/src/decision.rs @@ -39,13 +39,14 @@ impl ZenDecision { async move { decision .evaluate_with_opts( - &context, + context.into(), EvaluationOptions { max_depth: options.max_depth, trace: options.trace, }, ) .await + .map(ZenEngineResponse::from) } }) .await @@ -54,7 +55,7 @@ impl ZenDecision { anyhow!(serde_json::to_string(e.as_ref()).unwrap_or_else(|_| e.to_string())) })?; - Ok(ZenEngineResponse::from(result)) + Ok(result) } #[napi(ts_return_type = "Promise>")] diff --git a/bindings/nodejs/src/engine.rs b/bindings/nodejs/src/engine.rs index 78452b38..2b2cd702 100644 --- a/bindings/nodejs/src/engine.rs +++ b/bindings/nodejs/src/engine.rs @@ -118,13 +118,14 @@ impl ZenEngine { graph .evaluate_with_opts( key, - &context, + context.into(), EvaluationOptions { max_depth: options.max_depth, trace: options.trace, }, ) .await + .map(ZenEngineResponse::from) } }) .await @@ -133,7 +134,7 @@ impl ZenEngine { anyhow!(serde_json::to_string(e.as_ref()).unwrap_or_else(|_| e.to_string())) })?; - Ok(ZenEngineResponse::from(result)) + Ok(result) } #[napi] diff --git a/bindings/nodejs/src/expression.rs b/bindings/nodejs/src/expression.rs index 6c786560..0fdc1d99 100644 --- a/bindings/nodejs/src/expression.rs +++ b/bindings/nodejs/src/expression.rs @@ -7,8 +7,9 @@ pub fn evaluate_expression_sync(expression: String, context: Option) -> n let ctx = context.unwrap_or(Value::Null); Ok( - zen_expression::evaluate_expression(expression.as_str(), &ctx) - .map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?, + zen_expression::evaluate_expression(expression.as_str(), ctx.into()) + .map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))? + .to_value(), ) } @@ -16,7 +17,7 @@ pub fn evaluate_expression_sync(expression: String, context: Option) -> n #[napi] pub fn evaluate_unary_expression_sync(expression: String, context: Value) -> napi::Result { Ok( - zen_expression::evaluate_unary_expression(expression.as_str(), &context) + zen_expression::evaluate_unary_expression(expression.as_str(), context.into()) .map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?, ) } @@ -24,8 +25,9 @@ pub fn evaluate_unary_expression_sync(expression: String, context: Value) -> nap #[allow(dead_code)] #[napi] pub fn render_template_sync(template: String, context: Value) -> napi::Result { - Ok(zen_tmpl::render(template.as_str(), &context) - .map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?) + Ok(zen_tmpl::render(template.as_str(), context.into()) + .map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))? + .to_value()) } #[allow(dead_code)] diff --git a/bindings/nodejs/src/types.rs b/bindings/nodejs/src/types.rs index 7851f282..4f5e6d50 100644 --- a/bindings/nodejs/src/types.rs +++ b/bindings/nodejs/src/types.rs @@ -7,6 +7,7 @@ use serde_json::Value; use zen_engine::handler::custom_node_adapter::CustomDecisionNode; use zen_engine::{DecisionGraphResponse, DecisionGraphTrace}; +use zen_expression::Variable; #[napi(object)] pub struct ZenEngineTrace { @@ -23,8 +24,8 @@ impl From for ZenEngineTrace { Self { id: value.id, name: value.name, - input: value.input, - output: value.output, + input: value.input.to_value(), + output: value.output.to_value(), performance: value.performance, trace_data: value.trace_data, } @@ -42,7 +43,7 @@ impl From for ZenEngineResponse { fn from(value: DecisionGraphResponse) -> Self { Self { performance: value.performance, - result: value.result, + result: value.result.to_value(), trace: value.trace.map(|opt| { opt.into_iter() .map(|(key, value)| (key, ZenEngineTrace::from(value))) @@ -104,10 +105,10 @@ impl ZenEngineHandlerRequest { return Ok(selected_value); }; - let template_value = zen_tmpl::render(template.as_str(), &self.input) + let template_value = zen_tmpl::render(template.as_str(), Variable::from(&self.input)) .map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?; - Ok(template_value) + Ok(template_value.to_value()) } #[napi(ts_return_type = "unknown")]