From 6bee80dcd784820c349408bdf1eee3b378b40f79 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Sun, 12 Nov 2023 00:15:11 +0800 Subject: [PATCH] make reject support list input directly (#11024) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 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 │ ╰───┴───┴───╯ ``` --- crates/nu-command/src/filters/reject.rs | 10 ++++++++++ crates/nu-command/src/filters/select.rs | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/nu-command/src/filters/reject.rs b/crates/nu-command/src/filters/reject.rs index 408a0907c..cef085b5a 100644 --- a/crates/nu-command/src/filters/reject.rs +++ b/crates/nu-command/src/filters/reject.rs @@ -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(), @@ -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", diff --git a/crates/nu-command/src/filters/select.rs b/crates/nu-command/src/filters/select.rs index dc6abea58..0dd21db9a 100644 --- a/crates/nu-command/src/filters/select.rs +++ b/crates/nu-command/src/filters/select.rs @@ -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(), @@ -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",