make reject support list input directly (#11024)

# Description
Fixes: #10895 

It's because `reject` and `select` command can't handle list of CellPath
input directly.
After this pr, the following should be ok:
```nushell
❯ [{'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'b': 2, 'c': 3}] | reject ['a', 'b']
╭───┬───╮
│ # │ c │
├───┼───┤
│ 0 │ 3 │
│ 1 │ 3 │
╰───┴───╯
❯ [{'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'b': 2, 'c': 3}] | select ['a', 'b']
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 2 │
│ 1 │ 1 │ 2 │
╰───┴───┴───╯
```
This commit is contained in:
WindSoilder 2023-11-12 00:15:11 +08:00 committed by GitHub
parent 588a078872
commit 6bee80dcd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View file

@ -89,6 +89,7 @@ impl Command for Reject {
}; };
new_columns.push(cv.clone()); new_columns.push(cv.clone());
} }
Value::CellPath { val, .. } => new_columns.push(val),
y => { y => {
return Err(ShellError::CantConvert { return Err(ShellError::CantConvert {
to_type: "cell path".into(), to_type: "cell path".into(),
@ -189,6 +190,15 @@ impl Command for Reject {
example: "let cols = [size type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject $cols", example: "let cols = [size type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject $cols",
result: None result: None
}, },
Example {
description: "Reject columns by a list of columns directly",
example: r#"[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject ["size", "type"]"#,
result: Some(Value::test_list(
vec![
Value::test_record(record! {"name" => Value::test_string("Cargo.toml")}),
Value::test_record(record! {"name" => Value::test_string("Cargo.lock")})],
)),
},
Example { Example {
description: "Reject rows by a provided list of rows", description: "Reject rows by a provided list of rows",
example: "let rows = [0 2];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | reject $rows", example: "let rows = [0 2];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | reject $rows",

View file

@ -93,6 +93,9 @@ produce a table, a list will produce a list, and a record will produce a record.
}; };
new_columns.push(cv.clone()); new_columns.push(cv.clone());
} }
Value::CellPath { val, .. } => {
new_columns.push(val);
}
y => { y => {
return Err(ShellError::CantConvert { return Err(ShellError::CantConvert {
to_type: "cell path".into(), to_type: "cell path".into(),
@ -179,6 +182,15 @@ produce a table, a list will produce a list, and a record will produce a record.
example: "let cols = [name type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select $cols", example: "let cols = [name type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select $cols",
result: None result: None
}, },
Example {
description: "Select columns by a provided list of columns",
example: r#"[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select ["name", "type"]"#,
result: Some(Value::test_list(
vec![
Value::test_record(record! {"name" => Value::test_string("Cargo.toml"), "type" => Value::test_string("toml")}),
Value::test_record(record! {"name" => Value::test_string("Cargo.lock"), "type" => Value::test_string("toml")})],
))
},
Example { Example {
description: "Select rows by a provided list of rows", description: "Select rows by a provided list of rows",
example: "let rows = [0 2];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | select $rows", example: "let rows = [0 2];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | select $rows",