mirror of
https://github.com/nushell/nushell.git
synced 2025-04-10 14:08:40 +02:00
Deal with case of empty header cell
This commit is contained in:
parent
319aac505e
commit
6162027a54
@ -42,29 +42,36 @@ pub fn count(
|
|||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let rows: Vec<Value> = input.values.collect().await;
|
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 {
|
let headers: Vec<String> = match &rows[0].value {
|
||||||
UntaggedValue::Row(d) => {
|
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![];
|
//Each row is a dictionary with the headers as keys
|
||||||
for r in rows.iter().skip(1) {
|
let newrows: Vec<Value> = rows.iter().skip(1).map(|r| {
|
||||||
match &r.value {
|
match &r.value {
|
||||||
UntaggedValue::Row(d) => {
|
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() {
|
for (_, v) in d.entries.iter() {
|
||||||
newrow.insert(headers[i].clone(), v.clone());
|
entries.insert(headers[i].clone(), v.clone());
|
||||||
i += 1;
|
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))
|
yield ReturnSuccess::value(UntaggedValue::table(&newrows).into_value(name))
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user