mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
Deal with case of empty header cell
This commit is contained in:
parent
319aac505e
commit
2cc583bd47
@ -26,15 +26,11 @@ impl WholeStreamCommand for Headers {
|
|||||||
args: CommandArgs,
|
args: CommandArgs,
|
||||||
registry: &CommandRegistry,
|
registry: &CommandRegistry,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> 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.
|
pub fn headers(
|
||||||
//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(
|
|
||||||
HeadersArgs {}: HeadersArgs,
|
HeadersArgs {}: HeadersArgs,
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
@ -42,29 +38,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.
|
||||||
}
|
}
|
||||||
_ => vec![]
|
}).collect()
|
||||||
|
}
|
||||||
|
_ => 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