Skip to content

Commit

Permalink
fix event_as_input bug
Browse files Browse the repository at this point in the history
  • Loading branch information
XxChang committed Jun 15, 2024
1 parent a346f35 commit 2488d09
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
28 changes: 18 additions & 10 deletions apis/c++/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::any::Any;
use std::{any::Any, vec};

use dora_node_api::{
self,
arrow::array::{AsArray, BinaryArray},
arrow::array::{AsArray, UInt8Array},
merged::{MergeExternal, MergedEvent},
Event, EventStream,
};
Expand Down Expand Up @@ -138,18 +138,26 @@ fn event_type(event: &DoraEvent) -> ffi::DoraEventType {
}

fn event_as_input(event: Box<DoraEvent>) -> eyre::Result<ffi::DoraInput> {
let Some(Event::Input {
id,
metadata: _,
data,
}) = event.0
else {
let Some(Event::Input { id, metadata, data }) = event.0 else {
bail!("not an input event");
};
let data: Option<&BinaryArray> = data.as_binary_opt();
let data = match metadata.type_info.data_type {
dora_node_api::arrow::datatypes::DataType::UInt8 => {
let array: &UInt8Array = data.as_primitive();
array.values().to_vec()
}
dora_node_api::arrow::datatypes::DataType::Null => {
vec![]
}
_ => {
todo!("dora C++ Node does not yet support higher level type of arrow. Only UInt8.
The ultimate solution should be based on arrow FFI interface. Feel free to contribute :)")
}
};

Ok(ffi::DoraInput {
id: id.into(),
data: data.map(|d| d.value(0).to_owned()).unwrap_or_default(),
data,
})
}

Expand Down
3 changes: 2 additions & 1 deletion binaries/cli/src/template/cxx/listener-template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ int main()
{
auto input = event_as_input(std::move(event));
auto input_id = input.id;
std::cout << "I heard from " << std::string(input_id) << std::endl;
auto message = std::string(reinterpret_cast<const char*>(input.data.data()), input.data.size());
std::cout << "I heard " << message << " from " << std::string(input_id) << std::endl;
}
else {
std::cerr << "Unknown event type " << static_cast<int>(ty) << std::endl;
Expand Down

0 comments on commit 2488d09

Please sign in to comment.