Update description and error types for split-by (#10865)

# Description

`split-by` only works on a `Record`, the error type was updated to
match, and now uses a more-specific type. (Two type fixes for the price
of one!)

The `usage` was updated to say "record" as well

# User-Facing Changes

* Providing the wrong type to `split-by` now gives an error messages
with the correct required input type

Previously:

```
❯ ls | get name | split-by type
Error:   × unsupported input
   ╭─[entry #267:1:1]
 1 │ ls | get name | split-by type
   ·      ─┬─
   ·       ╰── requires a table with one row for splitting
   ╰────
```

With this PR:

```
❯ ls | get name | split-by type
Error: nu:🐚:type_mismatch

  × Type mismatch.
   ╭─[entry #1:1:1]
 1 │ ls | get name | split-by type
   ·      ─┬─
   ·       ╰── requires a record to split
   ╰────
```

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

Only generated commands need to be updated

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
Eric Hodel 2023-11-07 07:27:10 -08:00 committed by GitHub
parent 9b202d560d
commit c039e4b3d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 28 deletions

View File

@ -23,7 +23,7 @@ impl Command for SplitBy {
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Create a new table split." "Split a record into groups"
} }
fn run( fn run(
@ -176,7 +176,7 @@ fn data_group(
pub fn data_split( pub fn data_split(
value: PipelineData, value: PipelineData,
splitter: Option<&dyn Fn(usize, &Value) -> Result<String, ShellError>>, splitter: Option<&dyn Fn(usize, &Value) -> Result<String, ShellError>>,
span: Span, dst_span: Span,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let mut splits = indexmap::IndexMap::new(); let mut splits = indexmap::IndexMap::new();
@ -202,33 +202,31 @@ pub fn data_split(
} }
} }
_ => { _ => {
return Err(ShellError::GenericError( return Err(ShellError::OnlySupportsThisInputType {
"unsupported input".into(), exp_input_type: "Record".into(),
"requires a table with one row for splitting".into(), wrong_type: v.get_type().to_string(),
Some(span), dst_span,
None, src_span: v.span(),
Vec::new(), })
))
} }
} }
} }
PipelineData::Empty => return Err(ShellError::PipelineEmpty { dst_span }),
_ => { _ => {
return Err(ShellError::GenericError( return Err(ShellError::PipelineMismatch {
"unsupported input".into(), exp_input_type: "record".into(),
"requires a table with one row for splitting".into(), dst_span,
Some(span), src_span: value.span().unwrap_or(Span::unknown()),
None, })
Vec::new(),
))
} }
} }
let record = splits let record = splits
.into_iter() .into_iter()
.map(|(k, rows)| (k, Value::record(rows.into_iter().collect(), span))) .map(|(k, rows)| (k, Value::record(rows.into_iter().collect(), dst_span)))
.collect(); .collect();
Ok(PipelineData::Value(Value::record(record, span), None)) Ok(PipelineData::Value(Value::record(record, dst_span), None))
} }
#[cfg(test)] #[cfg(test)]

View File

@ -31,7 +31,16 @@ fn splits() {
} }
#[test] #[test]
fn errors_if_no_table_given_as_input() { fn errors_if_no_input() {
Playground::setup("split_by_no_input", |dirs, _sandbox| {
let actual = nu!(cwd: dirs.test(), pipeline("split-by type"));
assert!(actual.err.contains("no input value was piped in"));
})
}
#[test]
fn errors_if_non_record_input() {
Playground::setup("split_by_test_2", |dirs, sandbox| { Playground::setup("split_by_test_2", |dirs, sandbox| {
sandbox.with_files(vec![ sandbox.with_files(vec![
EmptyFile("los.txt"), EmptyFile("los.txt"),
@ -40,7 +49,11 @@ fn errors_if_no_table_given_as_input() {
EmptyFile("arepas.clu"), EmptyFile("arepas.clu"),
]); ]);
let actual = nu!( let input_mismatch = nu!(cwd: dirs.test(), pipeline("5 | split-by type"));
assert!(input_mismatch.err.contains("doesn't support int input"));
let only_supports = nu!(
cwd: dirs.test(), pipeline( cwd: dirs.test(), pipeline(
" "
ls ls
@ -49,6 +62,8 @@ fn errors_if_no_table_given_as_input() {
" "
)); ));
assert!(actual.err.contains("requires a table")); assert!(only_supports
.err
.contains("only Record input data is supported"));
}) })
} }