Skip to content

Commit 91cf7c0

Browse files
committed
fix: set metadata.message_type from source_type in WASM generate function (v3.1.9)
Add process_generate helper to extract source_type from scenario JSON and set it as metadata.message_type for proper workflow condition routing.
1 parent 2b152be commit 91cf7c0

3 files changed

Lines changed: 70 additions & 11 deletions

File tree

wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "reframe-wasm"
3-
version = "3.1.8"
3+
version = "3.1.9"
44
edition = "2024"
55
license = "Apache-2.0"
66
description = "WebAssembly bindings for Reframe transformation engine"

wasm/src/detection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
use async_trait::async_trait;
99
use dataflow_rs::engine::error::DataflowError;
1010
use dataflow_rs::engine::{
11+
AsyncFunctionHandler, FunctionConfig,
1112
error::Result,
1213
message::{Change, Message},
13-
AsyncFunctionHandler, FunctionConfig,
1414
};
1515
use datalogic_rs::DataLogic;
1616
use regex::Regex;
17-
use serde_json::{json, Value};
17+
use serde_json::{Value, json};
1818
use std::sync::Arc;
1919

2020
pub struct Detect;

wasm/src/lib.rs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
mod detection;
3535

3636
use dataflow_rs::{AsyncFunctionHandler, Engine, Message, Workflow};
37-
use serde_json::{json, Value};
37+
use serde_json::{Value, json};
3838
use std::collections::HashMap;
3939
use std::sync::Arc;
4040
use wasm_bindgen::prelude::*;
@@ -214,34 +214,37 @@ impl ReframeEngine {
214214

215215
/// Generate a sample message.
216216
///
217-
/// The payload is stored as a raw string containing generation parameters.
217+
/// The payload should be a JSON string containing a scenario schema with `source_type` field.
218+
/// The `source_type` is used to set `metadata.message_type` for workflow routing.
218219
///
219220
/// # Arguments
220-
/// * `payload` - Raw string payload with generation parameters
221+
/// * `payload` - JSON string containing the scenario schema
221222
///
222223
/// # Returns
223224
/// A Promise that resolves to the generated message as a JSON string
224225
///
225226
/// # Example
226227
/// ```javascript
227-
/// const params = '{"message_type": "MT103", "scenario": "standard"}';
228-
/// const result = await engine.generate(params);
228+
/// const scenario = '{"source_type": "MT103", "schema": {...}}';
229+
/// const result = await engine.generate(scenario);
230+
/// const output = JSON.parse(result);
231+
/// console.log(output.data.result); // Generated MT103 message
229232
/// ```
230233
#[wasm_bindgen]
231234
pub fn generate(&self, payload: &str) -> js_sys::Promise {
232-
self.process_with_engine(&self.generate_engine, payload, false)
235+
self.process_generate(&self.generate_engine, payload, false)
233236
}
234237

235238
/// Generate a sample message with execution trace.
236239
///
237240
/// # Arguments
238-
/// * `payload` - Raw string payload with generation parameters
241+
/// * `payload` - JSON string containing the scenario schema
239242
///
240243
/// # Returns
241244
/// A Promise that resolves to the execution trace as a JSON string
242245
#[wasm_bindgen]
243246
pub fn generate_with_trace(&self, payload: &str) -> js_sys::Promise {
244-
self.process_with_engine(&self.generate_engine, payload, true)
247+
self.process_generate(&self.generate_engine, payload, true)
245248
}
246249

247250
/// Validate a message.
@@ -410,6 +413,62 @@ impl ReframeEngine {
410413
})
411414
}
412415
}
416+
417+
/// Internal helper to process a generate request.
418+
/// Parses the scenario to extract source_type and sets it as metadata.message_type.
419+
fn process_generate(
420+
&self,
421+
engine: &Arc<Engine>,
422+
payload: &str,
423+
with_trace: bool,
424+
) -> js_sys::Promise {
425+
// Parse the scenario to extract source_type for workflow routing
426+
let scenario: Value = match serde_json::from_str(payload) {
427+
Ok(v) => v,
428+
Err(e) => {
429+
let error_msg = format!("Invalid scenario JSON: {}", e);
430+
return future_to_promise(async move { Err(JsValue::from_str(&error_msg)) });
431+
}
432+
};
433+
434+
// Extract source_type from scenario
435+
let message_type = scenario
436+
.get("source_type")
437+
.and_then(|v| v.as_str())
438+
.unwrap_or("")
439+
.to_string();
440+
441+
// Create message with the scenario as payload
442+
let mut message = Message::from_value(&scenario);
443+
444+
// Set metadata.message_type for workflow condition routing
445+
if let Some(metadata) = message.metadata_mut().as_object_mut() {
446+
metadata.insert("message_type".to_string(), json!(message_type));
447+
}
448+
449+
// Clone the Arc for the async block
450+
let engine = Arc::clone(engine);
451+
452+
if with_trace {
453+
future_to_promise(async move {
454+
match engine.process_message_with_trace(&mut message).await {
455+
Ok(trace) => serde_json::to_string(&trace)
456+
.map(|s| JsValue::from_str(&s))
457+
.map_err(|e| JsValue::from_str(&e.to_string())),
458+
Err(e) => Err(JsValue::from_str(&e.to_string())),
459+
}
460+
})
461+
} else {
462+
future_to_promise(async move {
463+
match engine.process_message(&mut message).await {
464+
Ok(()) => serde_json::to_string(&message)
465+
.map(|s| JsValue::from_str(&s))
466+
.map_err(|e| JsValue::from_str(&e.to_string())),
467+
Err(e) => Err(JsValue::from_str(&e.to_string())),
468+
}
469+
})
470+
}
471+
}
413472
}
414473

415474
/// Parse an optional workflow array value into a Vec<Workflow>.

0 commit comments

Comments
 (0)