mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
Insert Column[index] if no header info is found.
This commit is contained in:
parent
ac1ef302ad
commit
ee23fce9cd
@ -32,7 +32,7 @@ impl WholeStreamCommand for Headers {
|
|||||||
|
|
||||||
pub fn headers(
|
pub fn headers(
|
||||||
HeadersArgs {}: HeadersArgs,
|
HeadersArgs {}: HeadersArgs,
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let rows: Vec<Value> = input.values.collect().await;
|
let rows: Vec<Value> = input.values.collect().await;
|
||||||
@ -40,10 +40,15 @@ pub fn headers(
|
|||||||
//the headers are the first row in the table
|
//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) => {
|
||||||
Ok(d.entries.iter().map(|(_, v)| {
|
Ok(d.entries.iter().map(|(k, v)| {
|
||||||
match v.as_string() {
|
match v.as_string() {
|
||||||
Ok(s) => s,
|
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. TODO: Add indexing to differintate between headers
|
Err(_) => { //If a cell that should contain a header name is empty, we name the column Column[index]
|
||||||
|
match d.entries.get_full(k) {
|
||||||
|
Some((index, _, _)) => format!("Column{}", index),
|
||||||
|
None => "unknownColumn".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).collect())
|
}).collect())
|
||||||
}
|
}
|
||||||
@ -54,14 +59,12 @@ pub fn headers(
|
|||||||
for r in rows.iter().skip(1) {
|
for r in rows.iter().skip(1) {
|
||||||
match &r.value {
|
match &r.value {
|
||||||
UntaggedValue::Row(d) => {
|
UntaggedValue::Row(d) => {
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut entries = IndexMap::new();
|
let mut entries = IndexMap::new();
|
||||||
for (_, v) in d.entries.iter() {
|
for (_, v) in d.entries.iter() {
|
||||||
entries.insert(headers[i].clone(), v.clone());
|
entries.insert(headers[i].clone(), v.clone());
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
yield Ok(ReturnSuccess::Value(UntaggedValue::Row(Dictionary{entries}).into_value(r.tag.clone())))
|
yield Ok(ReturnSuccess::Value(UntaggedValue::Row(Dictionary{entries}).into_value(r.tag.clone())))
|
||||||
}
|
}
|
||||||
_ => yield Err(ShellError::untagged_runtime_error("Couldn't iterate through rows, was the input a properly formatted table?"))
|
_ => yield Err(ShellError::untagged_runtime_error("Couldn't iterate through rows, was the input a properly formatted table?"))
|
||||||
|
Loading…
Reference in New Issue
Block a user