Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix event_as_input bug #556

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading