forked from extern/nushell
To csv fix (#7850)
# Description Fixes #7800 . `to csv` and `to tsv` no longer: - accept anything but records and tables as input, - accept lists that are not tables, - accept tables and records with values that are not primitives (other lists, tables and records). # User-Facing Changes Using `to csv` and `to tsv` on any of inputs mentioned above will result in `cant_convert` error. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
This commit is contained in:
parent
e03c354e89
commit
b9419e0f36
@ -17,7 +17,10 @@ impl Command for ToCsv {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("to csv")
|
Signature::build("to csv")
|
||||||
.input_output_types(vec![(Type::Any, Type::String)])
|
.input_output_types(vec![
|
||||||
|
(Type::Record(vec![]), Type::String),
|
||||||
|
(Type::Table(vec![]), Type::String),
|
||||||
|
])
|
||||||
.named(
|
.named(
|
||||||
"separator",
|
"separator",
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
@ -44,6 +47,11 @@ impl Command for ToCsv {
|
|||||||
example: "[[foo bar]; [1 2]] | to csv -s ';' ",
|
example: "[[foo bar]; [1 2]] | to csv -s ';' ",
|
||||||
result: Some(Value::test_string("foo;bar\n1;2\n")),
|
result: Some(Value::test_string("foo;bar\n1;2\n")),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Outputs an CSV string representing the contents of this record",
|
||||||
|
example: "{a: 1 b: 2} | to csv",
|
||||||
|
result: Some(Value::test_string("a,b\n1,2\n")),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use csv::WriterBuilder;
|
use csv::{Writer, WriterBuilder};
|
||||||
use indexmap::{indexset, IndexSet};
|
use indexmap::{indexset, IndexSet};
|
||||||
use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Value};
|
use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Value};
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
fn from_value_to_delimited_string(
|
fn from_value_to_delimited_string(
|
||||||
value: &Value,
|
value: &Value,
|
||||||
@ -11,6 +12,23 @@ fn from_value_to_delimited_string(
|
|||||||
) -> Result<String, ShellError> {
|
) -> Result<String, ShellError> {
|
||||||
match value {
|
match value {
|
||||||
Value::Record { cols, vals, span } => {
|
Value::Record { cols, vals, span } => {
|
||||||
|
record_to_delimited(cols, vals, span, separator, config, head)
|
||||||
|
}
|
||||||
|
Value::List { vals, span } => table_to_delimited(vals, span, separator, config, head),
|
||||||
|
// Propagate errors by explicitly matching them before the final case.
|
||||||
|
Value::Error { error } => Err(error.clone()),
|
||||||
|
v => Err(make_unsupported_input_error(v, head, v.expect_span())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn record_to_delimited(
|
||||||
|
cols: &[String],
|
||||||
|
vals: &[Value],
|
||||||
|
span: &Span,
|
||||||
|
separator: char,
|
||||||
|
config: &Config,
|
||||||
|
head: Span,
|
||||||
|
) -> Result<String, ShellError> {
|
||||||
let mut wtr = WriterBuilder::new()
|
let mut wtr = WriterBuilder::new()
|
||||||
.delimiter(separator as u8)
|
.delimiter(separator as u8)
|
||||||
.from_writer(vec![]);
|
.from_writer(vec![]);
|
||||||
@ -26,15 +44,20 @@ fn from_value_to_delimited_string(
|
|||||||
wtr.write_record(fields).expect("can not write.");
|
wtr.write_record(fields).expect("can not write.");
|
||||||
wtr.write_record(values).expect("can not write.");
|
wtr.write_record(values).expect("can not write.");
|
||||||
|
|
||||||
let v = String::from_utf8(wtr.into_inner().map_err(|_| {
|
writer_to_string(wtr).map_err(|_| make_conversion_error("record", span))
|
||||||
ShellError::CantConvert("record".to_string(), "string".to_string(), *span, None)
|
|
||||||
})?)
|
|
||||||
.map_err(|_| {
|
|
||||||
ShellError::CantConvert("record".to_string(), "string".to_string(), *span, None)
|
|
||||||
})?;
|
|
||||||
Ok(v)
|
|
||||||
}
|
}
|
||||||
Value::List { vals, span } => {
|
|
||||||
|
fn table_to_delimited(
|
||||||
|
vals: &Vec<Value>,
|
||||||
|
span: &Span,
|
||||||
|
separator: char,
|
||||||
|
config: &Config,
|
||||||
|
head: Span,
|
||||||
|
) -> Result<String, ShellError> {
|
||||||
|
if let Some(val) = find_non_record(vals) {
|
||||||
|
return Err(make_unsupported_input_error(val, head, *span));
|
||||||
|
}
|
||||||
|
|
||||||
let mut wtr = WriterBuilder::new()
|
let mut wtr = WriterBuilder::new()
|
||||||
.delimiter(separator as u8)
|
.delimiter(separator as u8)
|
||||||
.from_writer(vec![]);
|
.from_writer(vec![]);
|
||||||
@ -42,15 +65,13 @@ fn from_value_to_delimited_string(
|
|||||||
let merged_descriptors = merge_descriptors(vals);
|
let merged_descriptors = merge_descriptors(vals);
|
||||||
|
|
||||||
if merged_descriptors.is_empty() {
|
if merged_descriptors.is_empty() {
|
||||||
wtr.write_record(
|
let vals = vals
|
||||||
vals.iter()
|
.iter()
|
||||||
.map(|ele| {
|
.map(|ele| {
|
||||||
to_string_tagged_value(ele, config, head, *span)
|
to_string_tagged_value(ele, config, head, *span).unwrap_or_else(|_| String::new())
|
||||||
.unwrap_or_else(|_| String::new())
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>();
|
||||||
)
|
wtr.write_record(vals).expect("can not write");
|
||||||
.expect("can not write");
|
|
||||||
} else {
|
} else {
|
||||||
wtr.write_record(merged_descriptors.iter().map(|item| &item[..]))
|
wtr.write_record(merged_descriptors.iter().map(|item| &item[..]))
|
||||||
.expect("can not write.");
|
.expect("can not write.");
|
||||||
@ -66,18 +87,15 @@ fn from_value_to_delimited_string(
|
|||||||
wtr.write_record(&row).expect("can not write");
|
wtr.write_record(&row).expect("can not write");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let v = String::from_utf8(wtr.into_inner().map_err(|_| {
|
writer_to_string(wtr).map_err(|_| make_conversion_error("table", span))
|
||||||
ShellError::CantConvert("record".to_string(), "string".to_string(), *span, None)
|
|
||||||
})?)
|
|
||||||
.map_err(|_| {
|
|
||||||
ShellError::CantConvert("record".to_string(), "string".to_string(), *span, None)
|
|
||||||
})?;
|
|
||||||
Ok(v)
|
|
||||||
}
|
}
|
||||||
// Propagate errors by explicitly matching them before the final case.
|
|
||||||
Value::Error { error } => Err(error.clone()),
|
fn writer_to_string(writer: Writer<Vec<u8>>) -> Result<String, Box<dyn Error>> {
|
||||||
other => to_string_tagged_value(value, config, other.expect_span(), head),
|
Ok(String::from_utf8(writer.into_inner()?)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_conversion_error(type_from: &str, span: &Span) -> ShellError {
|
||||||
|
ShellError::CantConvert(type_from.to_string(), "string".to_string(), *span, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_string_tagged_value(
|
fn to_string_tagged_value(
|
||||||
@ -95,20 +113,28 @@ fn to_string_tagged_value(
|
|||||||
| Value::CustomValue { .. }
|
| Value::CustomValue { .. }
|
||||||
| Value::Filesize { .. }
|
| Value::Filesize { .. }
|
||||||
| Value::CellPath { .. }
|
| Value::CellPath { .. }
|
||||||
| Value::List { .. }
|
|
||||||
| Value::Record { .. }
|
|
||||||
| Value::Float { .. } => Ok(v.clone().into_abbreviated_string(config)),
|
| Value::Float { .. } => Ok(v.clone().into_abbreviated_string(config)),
|
||||||
Value::Date { val, .. } => Ok(val.to_string()),
|
Value::Date { val, .. } => Ok(val.to_string()),
|
||||||
Value::Nothing { .. } => Ok(String::new()),
|
Value::Nothing { .. } => Ok(String::new()),
|
||||||
// Propagate existing errors
|
// Propagate existing errors
|
||||||
Value::Error { error } => Err(error.clone()),
|
Value::Error { error } => Err(error.clone()),
|
||||||
_ => Err(ShellError::UnsupportedInput(
|
_ => Err(make_unsupported_input_error(v, head, span)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_unsupported_input_error(value: &Value, head: Span, span: Span) -> ShellError {
|
||||||
|
ShellError::UnsupportedInput(
|
||||||
"Unexpected type".to_string(),
|
"Unexpected type".to_string(),
|
||||||
format!("input type: {:?}", v.get_type()),
|
format!("input type: {:?}", value.get_type()),
|
||||||
head,
|
head,
|
||||||
span,
|
span,
|
||||||
)),
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_non_record(values: &[Value]) -> Option<&Value> {
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.find(|val| !matches!(val, Value::Record { .. }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merge_descriptors(values: &[Value]) -> Vec<String> {
|
pub fn merge_descriptors(values: &[Value]) -> Vec<String> {
|
||||||
|
@ -15,7 +15,10 @@ impl Command for ToTsv {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("to tsv")
|
Signature::build("to tsv")
|
||||||
.input_output_types(vec![(Type::Any, Type::String)])
|
.input_output_types(vec![
|
||||||
|
(Type::Record(vec![]), Type::String),
|
||||||
|
(Type::Table(vec![]), Type::String),
|
||||||
|
])
|
||||||
.switch(
|
.switch(
|
||||||
"noheaders",
|
"noheaders",
|
||||||
"do not output the column names as the first row",
|
"do not output the column names as the first row",
|
||||||
@ -29,11 +32,18 @@ impl Command for ToTsv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![
|
||||||
|
Example {
|
||||||
description: "Outputs an TSV string representing the contents of this table",
|
description: "Outputs an TSV string representing the contents of this table",
|
||||||
example: "[[foo bar]; [1 2]] | to tsv",
|
example: "[[foo bar]; [1 2]] | to tsv",
|
||||||
result: Some(Value::test_string("foo\tbar\n1\t2\n")),
|
result: Some(Value::test_string("foo\tbar\n1\t2\n")),
|
||||||
}]
|
},
|
||||||
|
Example {
|
||||||
|
description: "Outputs an TSV string representing the contents of this record",
|
||||||
|
example: "{a: 1 b: 2} | to tsv",
|
||||||
|
result: Some(Value::test_string("a\tb\n1\t2\n")),
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -42,7 +52,7 @@ impl Command for ToTsv {
|
|||||||
_stack: &mut Stack,
|
_stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<nu_protocol::PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let head = call.head;
|
let head = call.head;
|
||||||
let noheaders = call.has_flag("noheaders");
|
let noheaders = call.has_flag("noheaders");
|
||||||
let config = engine_state.get_config();
|
let config = engine_state.get_config();
|
||||||
|
@ -207,3 +207,41 @@ fn from_csv_text_skipping_headers_to_table() {
|
|||||||
assert_eq!(actual.out, "3");
|
assert_eq!(actual.out, "3");
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn table_with_record_error() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
[[a b]; [1 2] [3 {a: 1 b: 2}]]
|
||||||
|
| to csv
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_not_table_error() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
[{a: 1 b: 2} {a: 3 b: 4} 1]
|
||||||
|
| to csv
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn string_to_csv_error() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
'qwe' | to csv
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert"))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user