Skip to content

Commit

Permalink
make reject support list input directly (nushell#11024)
Browse files Browse the repository at this point in the history
# Description
Fixes: nushell#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 │
╰───┴───┴───╯
```
  • Loading branch information
WindSoilder authored and dmatos2012 committed Feb 20, 2024
1 parent 4f498d3 commit cbc4670
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/nu-command/src/filters/reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl Command for Reject {
};
new_columns.push(cv.clone());
}
Value::CellPath { val, .. } => new_columns.push(val),
y => {
return Err(ShellError::CantConvert {
to_type: "cell path".into(),
Expand Down Expand Up @@ -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",
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 {
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",
Expand Down
12 changes: 12 additions & 0 deletions crates/nu-command/src/filters/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Value::CellPath { val, .. } => {
new_columns.push(val);
}
y => {
return Err(ShellError::CantConvert {
to_type: "cell path".into(),
Expand Down Expand Up @@ -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",
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 {
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",
Expand Down

0 comments on commit cbc4670

Please sign in to comment.