add in a new command called columns (#519)

This commit is contained in:
Michael Angerman 2021-12-18 10:14:28 -08:00 committed by GitHub
parent 8933dde324
commit 00bb203756
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 124 additions and 0 deletions

View file

@ -49,6 +49,7 @@ pub fn create_default_context() -> EngineState {
Any,
Append,
Collect,
Columns,
Drop,
DropColumn,
DropNth,

View file

@ -0,0 +1,108 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
Signature, Span, Value,
};
#[derive(Clone)]
pub struct Columns;
impl Command for Columns {
fn name(&self) -> &str {
"columns"
}
fn signature(&self) -> Signature {
Signature::build(self.name()).category(Category::Filters)
}
fn usage(&self) -> &str {
"Show the columns in the input."
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "[[name,age,grade]; [bill,20,a]] | columns",
description: "Get the columns from the table",
result: None,
},
Example {
example: "[[name,age,grade]; [bill,20,a]] | columns | first",
description: "Get the first column from the table",
result: None,
},
Example {
example: "[[name,age,grade]; [bill,20,a]] | columns | nth 1",
description: "Get the second column from the table",
result: None,
},
]
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
getcol(engine_state, span, input)
}
}
fn getcol(
engine_state: &EngineState,
span: Span,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
match input {
PipelineData::Value(
Value::List {
vals: input_vals,
span: _,
},
..,
) => {
let input_cols = get_input_cols(input_vals);
Ok(input_cols
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
}
PipelineData::Stream(stream, ..) => {
let v: Vec<_> = stream.into_iter().collect();
let input_cols = get_input_cols(v);
Ok(input_cols
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
}
PipelineData::Value(_v, ..) => {
let cols = vec![];
let vals = vec![];
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
}
}
}
fn get_input_cols(input: Vec<Value>) -> Vec<String> {
let rec = input.first();
match rec {
Some(Value::Record { cols, vals: _, .. }) => cols.to_vec(),
_ => vec!["".to_string()],
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(Columns {})
}
}

View file

@ -2,6 +2,7 @@ mod all;
mod any;
mod append;
mod collect;
mod columns;
mod drop;
mod each;
mod first;
@ -30,6 +31,7 @@ pub use all::All;
pub use any::Any;
pub use append::Append;
pub use collect::Collect;
pub use columns::Columns;
pub use drop::*;
pub use each::Each;
pub use first::First;

View file

@ -1287,3 +1287,16 @@ fn flatten_table_column_get_last() -> TestResult {
"0.22",
)
}
#[test]
fn get_table_columns_1() -> TestResult {
run_test(
"[[name, age, grade]; [paul,21,a]] | columns | first",
"name",
)
}
#[test]
fn get_table_columns_2() -> TestResult {
run_test("[[name, age, grade]; [paul,21,a]] | columns | nth 1", "age")
}