Merge pull request #199 from jonathandturner/csv_rows

Switch from_csv to output rows
This commit is contained in:
Jonathan Turner 2019-07-20 18:59:49 +12:00 committed by GitHub
commit a86a11413f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 12 deletions

View file

@ -1,4 +1,4 @@
use crate::object::{Primitive, SpannedDictBuilder, SpannedListBuilder, Value}; use crate::object::{Primitive, SpannedDictBuilder, Value};
use crate::prelude::*; use crate::prelude::*;
use csv::ReaderBuilder; use csv::ReaderBuilder;
@ -13,8 +13,7 @@ pub fn from_csv_string_to_value(
let mut fields: VecDeque<String> = VecDeque::new(); let mut fields: VecDeque<String> = VecDeque::new();
let mut iter = reader.records(); let mut iter = reader.records();
let mut root = SpannedDictBuilder::new(span); let mut rows = vec![];
let mut rows = SpannedListBuilder::new(span);
if let Some(result) = iter.next() { if let Some(result) = iter.next() {
let line = result?; 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 { } else {
break; break;
} }
} }
root.insert_spanned("root", rows.into_spanned_value()); Ok(Spanned {
Ok(root.into_spanned_value()) item: Value::List(rows),
span,
})
} }
pub fn from_csv(args: CommandArgs) -> Result<OutputStream, ShellError> { pub fn from_csv(args: CommandArgs) -> Result<OutputStream, ShellError> {

View file

@ -39,14 +39,23 @@ command! {
} }
match contents { match contents {
Value::Primitive(Primitive::String(string)) => Value::Primitive(Primitive::String(string)) => {
stream.push_back(ReturnSuccess::value(parse_as_value( let value = parse_as_value(
file_extension, file_extension,
string, string,
contents_span, contents_span,
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))), other => stream.push_back(ReturnSuccess::value(other.spanned(contents_span))),
}; };

View file

@ -7,5 +7,5 @@ crate mod process;
crate mod types; crate mod types;
crate use base::{Block, Primitive, Switch, Value}; crate use base::{Block, Primitive, Switch, Value};
crate use dict::{Dictionary, SpannedDictBuilder, SpannedListBuilder}; crate use dict::{Dictionary, SpannedDictBuilder};
crate use files::dir_entry_dict; crate use files::dir_entry_dict;

View file

@ -17,7 +17,7 @@ fn open_csv() {
nu!( nu!(
output, output,
cwd("tests/fixtures/formats"), 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"); assert_eq!(output, "SPAIN");