Deal with case of empty header cell

This commit is contained in:
Sam Hedin 2020-03-28 01:09:42 +01:00
parent 319aac505e
commit 2cc583bd47

View File

@ -26,15 +26,11 @@ impl WholeStreamCommand for Headers {
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, count)?.run()
args.process(registry, headers)?.run()
}
}
//Rows is an array of dictionaries. Each dictionary maps header to content for that row.
//Take the first row and extract all content and save them as headers.
//Take the rest of the rows and replace the old column names with the new headers.
pub fn count(
pub fn headers(
HeadersArgs {}: HeadersArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
@ -42,29 +38,36 @@ pub fn count(
let stream = async_stream! {
let rows: Vec<Value> = input.values.collect().await;
//the headers are the first row in the table
let headers: Vec<String> = match &rows[0].value {
UntaggedValue::Row(d) => {
d.entries.iter().map(|(_, v)| v.as_string().unwrap()).collect()
d.entries.iter().map(|(_, v)| {
match v.as_string() {
Ok(s) => s,
Err(_) => String::from("empty-header") //If a cell that should contain a header name is empty, we need to fill it with something.
}
}).collect()
}
_ => vec![]
_ => panic!("Could not find headers")
};
let mut newrows: Vec<Value> = vec![];
for r in rows.iter().skip(1) {
//Each row is a dictionary with the headers as keys
let newrows: Vec<Value> = rows.iter().skip(1).map(|r| {
match &r.value {
UntaggedValue::Row(d) => {
let mut i = 0;
let mut newrow = IndexMap::new();
let mut i = 0;
let mut entries = IndexMap::new();
for (_, v) in d.entries.iter() {
newrow.insert(headers[i].clone(), v.clone());
entries.insert(headers[i].clone(), v.clone());
i += 1;
}
newrows.push(UntaggedValue::Row(Dictionary{entries: newrow}).into_value(r.tag.clone()));
UntaggedValue::Row(Dictionary{entries}).into_value(r.tag.clone())
}
_ => panic!("huh?")
_ => panic!("Row value was not an UntaggedValue::Row")
}
}
}).collect();
yield ReturnSuccess::value(UntaggedValue::table(&newrows).into_value(name))
};