Remove list of cell path support for select and reject (#11859)

# Description
Requires each of the rest args for `select` and `reject` to be a cell
path instead of the current `oneof(cellpath, list<cellpath>`. This
simplifies the command signatures and code for `select` and `reject`.
Users can now spread lists into the rest arguments instead of providing
them as is.

For example,
```nushell
ls | select [name size]
```
must now be
```nushell
ls | select ...[name size]
```

# User-Facing Changes
Breaking change for the `select` and `reject` command signatures.
This commit is contained in:
Ian Manske 2024-02-15 13:49:48 +00:00 committed by GitHub
parent c68324762d
commit 74d62581b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 122 deletions

View File

@ -29,10 +29,7 @@ impl Command for Reject {
) )
.rest( .rest(
"rest", "rest",
SyntaxShape::OneOf(vec![ SyntaxShape::CellPath,
SyntaxShape::CellPath,
SyntaxShape::List(Box::new(SyntaxShape::CellPath)),
]),
"The names of columns to remove from the table.", "The names of columns to remove from the table.",
) )
.category(Category::Filters) .category(Category::Filters)
@ -65,42 +62,6 @@ impl Command for Reject {
Value::CellPath { val, .. } => { Value::CellPath { val, .. } => {
new_columns.push(val); new_columns.push(val);
} }
Value::List { vals, .. } => {
for value in vals {
let val_span = &value.span();
match value {
Value::String { val, .. } => {
let cv = CellPath {
members: vec![PathMember::String {
val: val.clone(),
span: *val_span,
optional: false,
}],
};
new_columns.push(cv.clone());
}
Value::Int { val, .. } => {
let cv = CellPath {
members: vec![PathMember::Int {
val: val as usize,
span: *val_span,
optional: false,
}],
};
new_columns.push(cv.clone());
}
Value::CellPath { val, .. } => new_columns.push(val),
y => {
return Err(ShellError::CantConvert {
to_type: "cell path".into(),
from_type: y.get_type().to_string(),
span: y.span(),
help: None,
});
}
}
}
}
Value::String { val, .. } => { Value::String { val, .. } => {
let cv = CellPath { let cv = CellPath {
members: vec![PathMember::String { members: vec![PathMember::String {
@ -186,23 +147,25 @@ impl Command for Reject {
})), })),
}, },
Example { Example {
description: "Reject columns by a provided list of columns", description: "Reject multiple rows",
example: "let cols = [size type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject $cols", example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | reject 0 2",
result: None result: None,
}, },
Example { Example {
description: "Reject columns by a list of columns directly", description: "Reject multiple columns",
example: r#"[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject ["size", "type"]"#, example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject type size",
result: Some(Value::test_list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! { "name" => Value::test_string("Cargo.toml") }),
Value::test_record(record! {"name" => Value::test_string("Cargo.toml")}), Value::test_record(record! { "name" => Value::test_string("Cargo.lock") }),
Value::test_record(record! {"name" => Value::test_string("Cargo.lock")})], ])),
)),
}, },
Example { Example {
description: "Reject rows by a provided list of rows", description: "Reject multiple columns by spreading a list",
example: "let rows = [0 2];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | reject $rows", example: "let cols = [type size]; [[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject ...$cols",
result: None 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") }),
])),
}, },
] ]
} }

View File

@ -30,10 +30,7 @@ impl Command for Select {
) )
.rest( .rest(
"rest", "rest",
SyntaxShape::OneOf(vec![ SyntaxShape::CellPath,
SyntaxShape::CellPath,
SyntaxShape::List(Box::new(SyntaxShape::CellPath)),
]),
"The columns to select from the table.", "The columns to select from the table.",
) )
.allow_variants_without_examples(true) .allow_variants_without_examples(true)
@ -69,44 +66,6 @@ produce a table, a list will produce a list, and a record will produce a record.
Value::CellPath { val, .. } => { Value::CellPath { val, .. } => {
new_columns.push(val); new_columns.push(val);
} }
Value::List { vals, .. } => {
for value in vals {
let val_span = &value.span();
match value {
Value::String { val, .. } => {
let cv = CellPath {
members: vec![PathMember::String {
val: val.clone(),
span: *val_span,
optional: false,
}],
};
new_columns.push(cv.clone());
}
Value::Int { val, .. } => {
let cv = CellPath {
members: vec![PathMember::Int {
val: val as usize,
span: *val_span,
optional: false,
}],
};
new_columns.push(cv.clone());
}
Value::CellPath { val, .. } => {
new_columns.push(val);
}
y => {
return Err(ShellError::CantConvert {
to_type: "cell path".into(),
from_type: y.get_type().to_string(),
span: y.span(),
help: None,
});
}
}
}
}
Value::String { val, .. } => { Value::String { val, .. } => {
let cv = CellPath { let cv = CellPath {
members: vec![PathMember::String { members: vec![PathMember::String {
@ -178,23 +137,32 @@ produce a table, a list will produce a list, and a record will produce a record.
result: None, result: None,
}, },
Example { Example {
description: "Select columns by a provided list of columns", description: "Select multiple columns",
example: "let cols = [name type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select $cols", example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select name type",
result: None 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 columns by a provided list of columns", description: "Select multiple columns by spreading a list",
example: r#"[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select ["name", "type"]"#, example: r#"let cols = [name type]; [[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select ...$cols"#,
result: Some(Value::test_list( result: Some(Value::test_list(vec![
vec![ Value::test_record(record! {
Value::test_record(record! {"name" => Value::test_string("Cargo.toml"), "type" => Value::test_string("toml")}), "name" => Value::test_string("Cargo.toml"),
Value::test_record(record! {"name" => Value::test_string("Cargo.lock"), "type" => Value::test_string("toml")})], "type" => Value::test_string("toml")
)) }),
}, Value::test_record(record! {
Example { "name" => Value::test_string("Cargo.lock"),
description: "Select rows by a provided list of rows", "type" => Value::test_string("toml")
example: "let rows = [0 2];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | select $rows", }),
result: None ]))
}, },
] ]
} }

View File

@ -128,14 +128,14 @@ fn reject_optional_row() {
} }
#[test] #[test]
fn reject_list_columns() { fn reject_columns_with_list_spread() {
let actual = nu!("let arg = [type size]; [[name type size];[Cargo.toml file 10mb] [Cargo.lock file 10mb] [src dir 100mb]] | reject $arg | to nuon"); let actual = nu!("let arg = [type size]; [[name type size];[Cargo.toml file 10mb] [Cargo.lock file 10mb] [src dir 100mb]] | reject ...$arg | to nuon");
assert_eq!(actual.out, "[[name]; [Cargo.toml], [Cargo.lock], [src]]"); assert_eq!(actual.out, "[[name]; [Cargo.toml], [Cargo.lock], [src]]");
} }
#[test] #[test]
fn reject_list_rows() { fn reject_rows_with_list_spread() {
let actual = nu!("let arg = [2 0]; [[name type size];[Cargo.toml file 10mb] [Cargo.lock file 10mb] [src dir 100mb]] | reject $arg | to nuon"); let actual = nu!("let arg = [2 0]; [[name type size];[Cargo.toml file 10mb] [Cargo.lock file 10mb] [src dir 100mb]] | reject ...$arg | to nuon");
assert_eq!( assert_eq!(
actual.out, actual.out,
"[[name, type, size]; [Cargo.lock, file, 10000000b]]" "[[name, type, size]; [Cargo.lock, file, 10000000b]]"
@ -143,8 +143,8 @@ fn reject_list_rows() {
} }
#[test] #[test]
fn rject_list_mixed() { fn reject_mixed_with_list_spread() {
let actual = nu!("let arg = [ type 2]; [[name type size];[Cargp.toml file 10mb] [ Cargo.lock file 10mb] [src dir 100mb]] | reject $arg | to nuon"); let actual = nu!("let arg = [type 2]; [[name type size];[Cargp.toml file 10mb] [ Cargo.lock file 10mb] [src dir 100mb]] | reject ...$arg | to nuon");
assert_eq!( assert_eq!(
actual.out, actual.out,
"[[name, size]; [Cargp.toml, 10000000b], [Cargo.lock, 10000000b]]" "[[name, size]; [Cargp.toml, 10000000b], [Cargo.lock, 10000000b]]"
@ -172,6 +172,6 @@ fn test_ignore_errors_flag() {
#[test] #[test]
fn test_ignore_errors_flag_var() { fn test_ignore_errors_flag_var() {
let actual = let actual =
nu!("let arg = [5 c]; [[a, b]; [1, 2], [3, 4], [5, 6]] | reject $arg -i | to nuon"); nu!("let arg = [5 c]; [[a, b]; [1, 2], [3, 4], [5, 6]] | reject ...$arg -i | to nuon");
assert_eq!(actual.out, "[[a, b]; [1, 2], [3, 4], [5, 6]]"); assert_eq!(actual.out, "[[a, b]; [1, 2], [3, 4], [5, 6]]");
} }

View File

@ -249,20 +249,20 @@ fn select_on_empty_list_returns_empty_list() {
} }
#[test] #[test]
fn select_columns_with_variable_list() { fn select_columns_with_list_spread() {
let actual = nu!(r#" let actual = nu!(r#"
let columns = [a c]; let columns = [a c];
echo [[a b c]; [1 2 3]] | select $columns | to nuon echo [[a b c]; [1 2 3]] | select ...$columns | to nuon
"#); "#);
assert_eq!(actual.out, "[[a, c]; [1, 3]]"); assert_eq!(actual.out, "[[a, c]; [1, 3]]");
} }
#[test] #[test]
fn select_rows_with_variable_list() { fn select_rows_with_list_spread() {
let actual = nu!(r#" let actual = nu!(r#"
let rows = [0 2]; let rows = [0 2];
echo [[a b c]; [1 2 3] [4 5 6] [7 8 9]] | select $rows | to nuon echo [[a b c]; [1 2 3] [4 5 6] [7 8 9]] | select ...$rows | to nuon
"#); "#);
assert_eq!(actual.out, "[[a, b, c]; [1, 2, 3], [7, 8, 9]]"); assert_eq!(actual.out, "[[a, b, c]; [1, 2, 3], [7, 8, 9]]");
@ -270,7 +270,7 @@ fn select_rows_with_variable_list() {
#[test] #[test]
fn select_single_row_with_variable() { fn select_single_row_with_variable() {
let actual = nu!("let idx = 2;[{a: 1, b: 2} {a: 3, b: 5} {a: 3}] | select $idx | to nuon"); let actual = nu!("let idx = 2; [{a: 1, b: 2} {a: 3, b: 5} {a: 3}] | select $idx | to nuon");
assert_eq!(actual.out, "[[a]; [3]]".to_string()); assert_eq!(actual.out, "[[a]; [3]]".to_string());
assert!(actual.err.is_empty()); assert!(actual.err.is_empty());