mirror of
https://github.com/nushell/nushell
synced 2024-11-14 17:07:07 +00:00
Merge pull request #199 from jonathandturner/csv_rows
Switch from_csv to output rows
This commit is contained in:
commit
a86a11413f
4 changed files with 22 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::object::{Primitive, SpannedDictBuilder, SpannedListBuilder, Value};
|
||||
use crate::object::{Primitive, SpannedDictBuilder, Value};
|
||||
use crate::prelude::*;
|
||||
use csv::ReaderBuilder;
|
||||
|
||||
|
@ -13,8 +13,7 @@ pub fn from_csv_string_to_value(
|
|||
|
||||
let mut fields: VecDeque<String> = VecDeque::new();
|
||||
let mut iter = reader.records();
|
||||
let mut root = SpannedDictBuilder::new(span);
|
||||
let mut rows = SpannedListBuilder::new(span);
|
||||
let mut rows = vec![];
|
||||
|
||||
if let Some(result) = iter.next() {
|
||||
let line = result?;
|
||||
|
@ -37,14 +36,16 @@ pub fn from_csv_string_to_value(
|
|||
);
|
||||
}
|
||||
|
||||
rows.insert_spanned(row.into_spanned_value());
|
||||
rows.push(row.into_spanned_value());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
root.insert_spanned("root", rows.into_spanned_value());
|
||||
Ok(root.into_spanned_value())
|
||||
Ok(Spanned {
|
||||
item: Value::List(rows),
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_csv(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
|
|
|
@ -39,14 +39,23 @@ command! {
|
|||
}
|
||||
|
||||
match contents {
|
||||
Value::Primitive(Primitive::String(string)) =>
|
||||
stream.push_back(ReturnSuccess::value(parse_as_value(
|
||||
Value::Primitive(Primitive::String(string)) => {
|
||||
let value = parse_as_value(
|
||||
file_extension,
|
||||
string,
|
||||
contents_span,
|
||||
span,
|
||||
)?)
|
||||
),
|
||||
)?;
|
||||
|
||||
match value {
|
||||
Spanned { item: Value::List(list), .. } => {
|
||||
for elem in list {
|
||||
stream.push_back(ReturnSuccess::value(elem));
|
||||
}
|
||||
}
|
||||
x => stream.push_back(ReturnSuccess::value(x))
|
||||
}
|
||||
},
|
||||
|
||||
other => stream.push_back(ReturnSuccess::value(other.spanned(contents_span))),
|
||||
};
|
||||
|
|
|
@ -7,5 +7,5 @@ crate mod process;
|
|||
crate mod types;
|
||||
|
||||
crate use base::{Block, Primitive, Switch, Value};
|
||||
crate use dict::{Dictionary, SpannedDictBuilder, SpannedListBuilder};
|
||||
crate use dict::{Dictionary, SpannedDictBuilder};
|
||||
crate use files::dir_entry_dict;
|
||||
|
|
|
@ -17,7 +17,7 @@ fn open_csv() {
|
|||
nu!(
|
||||
output,
|
||||
cwd("tests/fixtures/formats"),
|
||||
"open caco3_plastics.csv | get root | first 1 | get origin | echo $it"
|
||||
"open caco3_plastics.csv | first 1 | get origin | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(output, "SPAIN");
|
||||
|
|
Loading…
Reference in a new issue