Skip to content

Commit

Permalink
Fix written CSV header order
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodb committed Feb 10, 2025
1 parent c179fd5 commit 50f11a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contrib/quirky_binder_csv/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl DynNode for WriteCsv {
let write_headers = if has_headers {
let record_definition = &graph.record_definitions()[self.inputs.single().record_type()];
let variant = &record_definition[self.inputs.single().variant_id()];
let headers = variant.data().map(|d| record_definition[d].name());
let headers = variant.data_sorted().map(|d| record_definition[d].name());
Some(quote! {{
writer.write_record([#(#headers),*])?;
}})
Expand Down
37 changes: 37 additions & 0 deletions tests/quirky_binder_tests_source/quirky_binder_tests/csv/write.qb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use quirky_binder::{
function::{
execute::function_execute,
produce::function_produce,
update::function_update,
},
},
};
Expand All @@ -25,6 +26,37 @@ use quirky_binder_csv::write_csv;
)
)

(
function_produce(
fields: [("hello", "String"), ("universe", "usize")],
body: r#"
let record = new_record("world".to_string(), 42);
output.send(Some(record))?;
output.send(None)?;
Ok(())
"#,
)
- function_update(
remove_fields: ["hello"],
add_fields: [("hello", "String")],
body: r#"
input.map(|record| {
use crate::all_chains::csv::write::streams::quirky_binder_main_0::
quirky_binder_filter_1_0::{UnpackedRecord0, Record1, UnpackedRecord1};
let UnpackedRecord0 { hello, universe } = record.unpack();
Ok(Record1::new(UnpackedRecord1 {
universe: universe ^ 0b1111,
hello: hello.chars().rev().collect::<String>(),
}))
})
"#
)
- write_csv(
output_file: "output/hello_universe_2.csv",
has_headers: true,
)
)

(
function_execute#assert_output(
thread_type: Background,
Expand All @@ -35,6 +67,11 @@ use quirky_binder_csv::write_csv;
let actual = std::fs::read_to_string("output/hello_universe.csv").expect("actual");
assert_eq!(actual, r#"hello,universe
world,42
"#);

let actual = std::fs::read_to_string("output/hello_universe_2.csv").expect("actual");
assert_eq!(actual, r#"universe,hello
37,dlrow
"#);

Ok(())
Expand Down

0 comments on commit 50f11a6

Please sign in to comment.