mirror of
https://github.com/nushell/nushell
synced 2024-12-27 13:33:16 +00:00
Changes implementation of --headerless for from-tsv.
This commit is contained in:
parent
282cb46ff1
commit
f8dc06ef49
1 changed files with 19 additions and 36 deletions
|
@ -39,53 +39,36 @@ pub fn from_tsv_string_to_value(
|
||||||
tag: impl Into<Tag>,
|
tag: impl Into<Tag>,
|
||||||
) -> Result<Tagged<Value>, csv::Error> {
|
) -> Result<Tagged<Value>, csv::Error> {
|
||||||
let mut reader = ReaderBuilder::new()
|
let mut reader = ReaderBuilder::new()
|
||||||
.has_headers(false)
|
.has_headers(!headerless)
|
||||||
.delimiter(b'\t')
|
.delimiter(b'\t')
|
||||||
.from_reader(s.as_bytes());
|
.from_reader(s.as_bytes());
|
||||||
let tag = tag.into();
|
let tag = tag.into();
|
||||||
|
|
||||||
let mut fields: VecDeque<String> = VecDeque::new();
|
let headers = if headerless {
|
||||||
let mut iter = reader.records();
|
(1..=reader.headers()?.len())
|
||||||
|
.map(|i| format!("Column{}", i))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
} else {
|
||||||
|
reader.headers()?.iter().map(String::from).collect()
|
||||||
|
};
|
||||||
|
|
||||||
let mut rows = vec![];
|
let mut rows = vec![];
|
||||||
|
for row in reader.records() {
|
||||||
if let Some(result) = iter.next() {
|
let mut tagged_row = TaggedDictBuilder::new(&tag);
|
||||||
let line = result?;
|
for (value, header) in row?.iter().zip(headers.iter()) {
|
||||||
|
tagged_row.insert_tagged(
|
||||||
for (idx, item) in line.iter().enumerate() {
|
header,
|
||||||
if headerless {
|
Value::Primitive(Primitive::String(String::from(value))).tagged(&tag),
|
||||||
fields.push_back(format!("Column{}", idx + 1));
|
)
|
||||||
} else {
|
|
||||||
fields.push_back(item.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
|
||||||
if let Some(row_values) = iter.next() {
|
|
||||||
let row_values = row_values?;
|
|
||||||
|
|
||||||
let mut row = TaggedDictBuilder::new(&tag);
|
|
||||||
|
|
||||||
for (idx, entry) in row_values.iter().enumerate() {
|
|
||||||
row.insert_tagged(
|
|
||||||
fields.get(idx).unwrap(),
|
|
||||||
Value::Primitive(Primitive::String(String::from(entry))).tagged(&tag),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.push(row.into_tagged_value());
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
rows.push(tagged_row.into_tagged_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Value::Table(rows).tagged(&tag))
|
Ok(Value::Table(rows).tagged(&tag))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tsv(
|
fn from_tsv(
|
||||||
FromTSVArgs {
|
FromTSVArgs { headerless }: FromTSVArgs,
|
||||||
headerless: skip_headers,
|
|
||||||
}: FromTSVArgs,
|
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let name_tag = name;
|
let name_tag = name;
|
||||||
|
@ -115,7 +98,7 @@ fn from_tsv(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match from_tsv_string_to_value(concat_string, skip_headers, name_tag.clone()) {
|
match from_tsv_string_to_value(concat_string, headerless, name_tag.clone()) {
|
||||||
Ok(x) => match x {
|
Ok(x) => match x {
|
||||||
Tagged { item: Value::Table(list), .. } => {
|
Tagged { item: Value::Table(list), .. } => {
|
||||||
for l in list {
|
for l in list {
|
||||||
|
|
Loading…
Reference in a new issue