nushell/crates/nu-command/src/filters/select.rs

188 lines
5.7 KiB
Rust
Raw Normal View History

2021-10-02 06:55:05 +02:00
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
2021-10-25 06:01:02 +02:00
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
2021-10-02 06:55:05 +02:00
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-02 06:55:05 +02:00
pub struct Select;
impl Command for Select {
fn name(&self) -> &str {
"select"
}
fn signature(&self) -> Signature {
Signature::build("select").rest(
"rest",
SyntaxShape::CellPath,
"the columns to select from the table",
)
}
fn usage(&self) -> &str {
"Down-select table to only these columns."
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-10-02 06:55:05 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-10-25 08:31:39 +02:00
let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
2021-10-02 06:55:05 +02:00
let span = call.head;
select(span, columns, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Select just the name column",
example: "ls | select name",
result: None,
},
Example {
description: "Select the name and size columns",
example: "ls | select name size",
result: None,
},
]
}
}
2021-10-25 06:01:02 +02:00
fn select(
span: Span,
columns: Vec<CellPath>,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-02 06:55:05 +02:00
if columns.is_empty() {
2021-10-25 06:01:02 +02:00
return Err(ShellError::CantFindColumn(span, span)); //FIXME?
2021-10-02 06:55:05 +02:00
}
match input {
2021-10-25 06:01:02 +02:00
PipelineData::Value(Value::List {
2021-10-02 06:55:05 +02:00
vals: input_vals,
span,
2021-10-25 06:01:02 +02:00
}) => {
2021-10-02 06:55:05 +02:00
let mut output = vec![];
for input_val in input_vals {
let mut cols = vec![];
let mut vals = vec![];
for path in &columns {
//FIXME: improve implementation to not clone
let fetcher = input_val.clone().follow_cell_path(&path.members)?;
cols.push(path.into_string());
vals.push(fetcher);
}
output.push(Value::Record { cols, vals, span })
}
2021-10-25 06:01:02 +02:00
Ok(output.into_iter().into_pipeline_data())
2021-10-02 06:55:05 +02:00
}
2021-10-25 06:01:02 +02:00
PipelineData::Stream(stream) => Ok(stream
.map(move |x| {
let mut cols = vec![];
let mut vals = vec![];
for path in &columns {
//FIXME: improve implementation to not clone
match x.clone().follow_cell_path(&path.members) {
Ok(value) => {
cols.push(path.into_string());
vals.push(value);
}
Err(error) => {
cols.push(path.into_string());
vals.push(Value::Error { error });
2021-10-02 06:55:05 +02:00
}
}
2021-10-25 06:01:02 +02:00
}
2021-10-02 06:55:05 +02:00
2021-10-25 06:01:02 +02:00
Value::Record { cols, vals, span }
})
.into_pipeline_data()),
PipelineData::Value(v) => {
2021-10-02 06:55:05 +02:00
let mut cols = vec![];
let mut vals = vec![];
for cell_path in columns {
// FIXME: remove clone
let result = v.clone().follow_cell_path(&cell_path.members)?;
cols.push(cell_path.into_string());
vals.push(result);
}
2021-10-25 06:01:02 +02:00
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
2021-10-02 06:55:05 +02:00
}
}
}
// #[cfg(test)]
// mod tests {
// use nu_protocol::ColumnPath;
// use nu_source::Span;
// use nu_source::SpannedItem;
// use nu_source::Tag;
// use nu_stream::InputStream;
// use nu_test_support::value::nothing;
// use nu_test_support::value::row;
// use nu_test_support::value::string;
// use super::select;
// use super::Command;
// use super::ShellError;
// #[test]
// fn examples_work_as_expected() -> Result<(), ShellError> {
// use crate::examples::test as test_examples;
// test_examples(Command {})
// }
// #[test]
// fn select_using_sparse_table() {
// // Create a sparse table with 3 rows:
// // col_foo | col_bar
// // -----------------
// // foo |
// // | bar
// // foo |
// let input = vec![
// row(indexmap! {"col_foo".into() => string("foo")}),
// row(indexmap! {"col_bar".into() => string("bar")}),
// row(indexmap! {"col_foo".into() => string("foo")}),
// ];
// let expected = vec![
// row(
// indexmap! {"col_none".into() => nothing(), "col_foo".into() => string("foo"), "col_bar".into() => nothing()},
// ),
// row(
// indexmap! {"col_none".into() => nothing(), "col_foo".into() => nothing(), "col_bar".into() => string("bar")},
// ),
// row(
// indexmap! {"col_none".into() => nothing(), "col_foo".into() => string("foo"), "col_bar".into() => nothing()},
// ),
// ];
// let actual = select(
// Tag::unknown(),
// vec![
// ColumnPath::build(&"col_none".to_string().spanned(Span::unknown())),
// ColumnPath::build(&"col_foo".to_string().spanned(Span::unknown())),
// ColumnPath::build(&"col_bar".to_string().spanned(Span::unknown())),
// ],
// input.into(),
// );
// assert_eq!(Ok(expected), actual.map(InputStream::into_vec));
// }
// }