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
4 changed files with 53 additions and 122 deletions

View File

@ -29,10 +29,7 @@ impl Command for Reject {
)
.rest(
"rest",
SyntaxShape::OneOf(vec![
SyntaxShape::CellPath,
SyntaxShape::List(Box::new(SyntaxShape::CellPath)),
]),
SyntaxShape::CellPath,
"The names of columns to remove from the table.",
)
.category(Category::Filters)
@ -65,42 +62,6 @@ impl Command for Reject {
Value::CellPath { 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, .. } => {
let cv = CellPath {
members: vec![PathMember::String {
@ -186,23 +147,25 @@ impl Command for Reject {
})),
},
Example {
description: "Reject columns by a provided list of columns",
example: "let cols = [size type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject $cols",
result: None
description: "Reject multiple rows",
example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb] [file.json json 3kb]] | reject 0 2",
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")})],
)),
description: "Reject multiple columns",
example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject type size",
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",
result: None
description: "Reject multiple columns by spreading a list",
example: "let cols = [type size]; [[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | reject ...$cols",
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",
SyntaxShape::OneOf(vec![
SyntaxShape::CellPath,
SyntaxShape::List(Box::new(SyntaxShape::CellPath)),
]),
SyntaxShape::CellPath,
"The columns to select from the table.",
)
.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, .. } => {
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, .. } => {
let cv = CellPath {
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,
},
Example {
description: "Select columns by a provided list of columns",
example: "let cols = [name type];[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select $cols",
result: None
description: "Select multiple columns",
example: "[[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 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",
result: None
description: "Select multiple columns by spreading a list",
example: r#"let cols = [name type]; [[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select ...$cols"#,
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")
}),
]))
},
]
}