From bf5bd3ff102689a3cae78ea5dfe2e67dfa92f4cd Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Sat, 29 Jul 2023 13:23:31 -0700 Subject: [PATCH] "merging into one dfr into-nu command" (#9858) - fixes #9806 # Description Merges ExprAsNu command and ToNu into one command. # User-Facing Changes As both commands were overloading ```dfr into-nu``` there are no user facing changes --------- Co-authored-by: Jack Wright --- .../src/dataframe/eager/to_nu.rs | 46 +++++++++++-- .../src/dataframe/expressions/alias.rs | 4 +- .../src/dataframe/expressions/as_nu.rs | 67 ------------------- .../src/dataframe/expressions/col.rs | 4 +- .../src/dataframe/expressions/datepart.rs | 4 +- .../src/dataframe/expressions/lit.rs | 4 +- .../src/dataframe/expressions/mod.rs | 3 - .../src/dataframe/expressions/otherwise.rs | 6 +- .../src/dataframe/expressions/when.rs | 6 +- 9 files changed, 53 insertions(+), 91 deletions(-) delete mode 100644 crates/nu-cmd-dataframe/src/dataframe/expressions/as_nu.rs diff --git a/crates/nu-cmd-dataframe/src/dataframe/eager/to_nu.rs b/crates/nu-cmd-dataframe/src/dataframe/eager/to_nu.rs index 40c5f92a0d..1ab8fce211 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/eager/to_nu.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/eager/to_nu.rs @@ -5,6 +5,8 @@ use nu_protocol::{ Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, }; +use crate::dataframe::values::NuExpression; + use super::super::values::NuDataFrame; #[derive(Clone)] @@ -16,7 +18,7 @@ impl Command for ToNu { } fn usage(&self) -> &str { - "Converts a section of the dataframe into nushell Table." + "Converts a dataframe or an expression into into nushell value for access and exploration." } fn signature(&self) -> Signature { @@ -28,7 +30,11 @@ impl Command for ToNu { Some('n'), ) .switch("tail", "shows tail rows", Some('t')) - .input_output_type(Type::Custom("dataframe".into()), Type::Any) + .input_output_types(vec![ + (Type::Custom("expression".into()), Type::Any), + (Type::Custom("dataframe".into()), Type::Table(vec![])), + ]) + //.input_output_type(Type::Any, Type::Any) .category(Category::Custom("dataframe".into())) } @@ -67,6 +73,15 @@ impl Command for ToNu { span: Span::test_data(), }), }, + Example { + description: "Convert a col expression into a nushell value", + example: "dfr col a | dfr into-nu", + result: Some(Value::Record { + cols: vec!["expr".into(), "value".into()], + vals: vec![Value::test_string("column"), Value::test_string("a")], + span: Span::test_data(), + }), + }, ] } @@ -77,20 +92,25 @@ impl Command for ToNu { call: &Call, input: PipelineData, ) -> Result { - command(engine_state, stack, call, input) + let value = input.into_value(call.head); + if NuDataFrame::can_downcast(&value) { + dataframe_command(engine_state, stack, call, value) + } else { + expression_command(call, value) + } } } -fn command( +fn dataframe_command( engine_state: &EngineState, stack: &mut Stack, call: &Call, - input: PipelineData, + input: Value, ) -> Result { let rows: Option = call.get_flag(engine_state, stack, "rows")?; let tail: bool = call.has_flag("tail"); - let df = NuDataFrame::try_from_pipeline(input, call.head)?; + let df = NuDataFrame::try_from_value(input)?; let values = if tail { df.tail(rows, call.head)? @@ -110,14 +130,26 @@ fn command( Ok(PipelineData::Value(value, None)) } +fn expression_command(call: &Call, input: Value) -> Result { + let expr = NuExpression::try_from_value(input)?; + let value = expr.to_value(call.head); + + Ok(PipelineData::Value(value, None)) +} #[cfg(test)] mod test { + use super::super::super::expressions::ExprCol; use super::super::super::test_dataframe::test_dataframe; use super::*; #[test] - fn test_examples() { + fn test_examples_dataframe_input() { test_dataframe(vec![Box::new(ToNu {})]) } + + #[test] + fn test_examples_expression_input() { + test_dataframe(vec![Box::new(ToNu {}), Box::new(ExprCol {})]) + } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/alias.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/alias.rs index c672b18343..c5fa821045 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/alias.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/alias.rs @@ -88,7 +88,7 @@ impl Command for ExprAlias { mod test { use super::super::super::test_dataframe::test_dataframe; use super::*; - use crate::dataframe::expressions::ExprAsNu; + use crate::dataframe::eager::ToNu; use crate::dataframe::expressions::ExprCol; #[test] @@ -96,7 +96,7 @@ mod test { test_dataframe(vec![ Box::new(ExprAlias {}), Box::new(ExprCol {}), - Box::new(ExprAsNu {}), + Box::new(ToNu {}), ]) } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/as_nu.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/as_nu.rs deleted file mode 100644 index 8cd893ae45..0000000000 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/as_nu.rs +++ /dev/null @@ -1,67 +0,0 @@ -use super::super::values::NuExpression; - -use nu_protocol::{ - ast::Call, - engine::{Command, EngineState, Stack}, - Category, Example, PipelineData, ShellError, Signature, Span, Type, Value, -}; - -#[derive(Clone)] -pub struct ExprAsNu; - -impl Command for ExprAsNu { - fn name(&self) -> &str { - "dfr into-nu" - } - - fn usage(&self) -> &str { - "Convert expression into a nu value for access and exploration." - } - - fn signature(&self) -> Signature { - Signature::build(self.name()) - .input_output_type(Type::Custom("expression".into()), Type::Any) - .category(Category::Custom("expression".into())) - } - - fn examples(&self) -> Vec { - vec![Example { - description: "Convert a col expression into a nushell value", - example: "dfr col a | dfr into-nu", - result: Some(Value::Record { - cols: vec!["expr".into(), "value".into()], - vals: vec![Value::test_string("column"), Value::test_string("a")], - span: Span::test_data(), - }), - }] - } - - fn search_terms(&self) -> Vec<&str> { - vec!["convert", "conversion"] - } - - fn run( - &self, - _engine_state: &EngineState, - _stack: &mut Stack, - call: &Call, - input: PipelineData, - ) -> Result { - let expr = NuExpression::try_from_pipeline(input, call.head)?; - let value = expr.to_value(call.head); - - Ok(PipelineData::Value(value, None)) - } -} - -#[cfg(test)] -mod test { - use super::super::super::test_dataframe::test_dataframe; - use super::super::ExprCol; - use super::*; - - #[test] - fn test_examples() { - test_dataframe(vec![Box::new(ExprAsNu {}), Box::new(ExprCol {})]) - } -} diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/col.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/col.rs index 8ff5ec5a74..70110459ef 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/col.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/col.rs @@ -64,10 +64,10 @@ impl Command for ExprCol { mod test { use super::super::super::test_dataframe::test_dataframe; use super::*; - use crate::dataframe::expressions::as_nu::ExprAsNu; + use crate::dataframe::eager::ToNu; #[test] fn test_examples() { - test_dataframe(vec![Box::new(ExprCol {}), Box::new(ExprAsNu {})]) + test_dataframe(vec![Box::new(ExprCol {}), Box::new(ToNu {})]) } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs index 4cc157aaec..1372ad7350 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/datepart.rs @@ -144,9 +144,9 @@ impl Command for ExprDatePart { mod test { use super::super::super::test_dataframe::test_dataframe; use super::*; + use crate::dataframe::eager::ToNu; use crate::dataframe::eager::WithColumn; use crate::dataframe::expressions::ExprAlias; - use crate::dataframe::expressions::ExprAsNu; use crate::dataframe::expressions::ExprCol; use crate::dataframe::series::AsDateTime; @@ -155,7 +155,7 @@ mod test { test_dataframe(vec![ Box::new(ExprDatePart {}), Box::new(ExprCol {}), - Box::new(ExprAsNu {}), + Box::new(ToNu {}), Box::new(AsDateTime {}), Box::new(WithColumn {}), Box::new(ExprAlias {}), diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/lit.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/lit.rs index 2f964e72c3..ed9d8a362f 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/lit.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/lit.rs @@ -66,10 +66,10 @@ impl Command for ExprLit { mod test { use super::super::super::test_dataframe::test_dataframe; use super::*; - use crate::dataframe::expressions::as_nu::ExprAsNu; + use crate::dataframe::eager::ToNu; #[test] fn test_examples() { - test_dataframe(vec![Box::new(ExprLit {}), Box::new(ExprAsNu {})]) + test_dataframe(vec![Box::new(ExprLit {}), Box::new(ToNu {})]) } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/mod.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/mod.rs index 75f9a007f2..941b3e5089 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/mod.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/mod.rs @@ -1,6 +1,5 @@ mod alias; mod arg_where; -mod as_nu; mod col; mod concat_str; mod datepart; @@ -15,7 +14,6 @@ use nu_protocol::engine::StateWorkingSet; pub(crate) use crate::dataframe::expressions::alias::ExprAlias; use crate::dataframe::expressions::arg_where::ExprArgWhere; -use crate::dataframe::expressions::as_nu::ExprAsNu; pub(super) use crate::dataframe::expressions::col::ExprCol; pub(super) use crate::dataframe::expressions::concat_str::ExprConcatStr; pub(crate) use crate::dataframe::expressions::datepart::ExprDatePart; @@ -44,7 +42,6 @@ pub fn add_expressions(working_set: &mut StateWorkingSet) { ExprConcatStr, ExprCount, ExprLit, - ExprAsNu, ExprWhen, ExprOtherwise, ExprQuantile, diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/otherwise.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/otherwise.rs index d1b4479af3..db1d019e0c 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/otherwise.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/otherwise.rs @@ -110,9 +110,9 @@ impl Command for ExprOtherwise { #[cfg(test)] mod test { use super::super::super::test_dataframe::test_dataframe; - use crate::dataframe::eager::WithColumn; + use crate::dataframe::eager::{ToNu, WithColumn}; use crate::dataframe::expressions::when::ExprWhen; - use crate::dataframe::expressions::{ExprAlias, ExprAsNu, ExprCol}; + use crate::dataframe::expressions::{ExprAlias, ExprCol}; use super::*; @@ -124,7 +124,7 @@ mod test { Box::new(ExprAlias {}), Box::new(ExprWhen {}), Box::new(ExprOtherwise {}), - Box::new(ExprAsNu {}), + Box::new(ToNu {}), ]) } } diff --git a/crates/nu-cmd-dataframe/src/dataframe/expressions/when.rs b/crates/nu-cmd-dataframe/src/dataframe/expressions/when.rs index 26f358120f..2fb27951a8 100644 --- a/crates/nu-cmd-dataframe/src/dataframe/expressions/when.rs +++ b/crates/nu-cmd-dataframe/src/dataframe/expressions/when.rs @@ -128,9 +128,9 @@ impl Command for ExprWhen { #[cfg(test)] mod test { use super::super::super::test_dataframe::test_dataframe; - use crate::dataframe::eager::WithColumn; + use crate::dataframe::eager::{ToNu, WithColumn}; use crate::dataframe::expressions::otherwise::ExprOtherwise; - use crate::dataframe::expressions::{ExprAlias, ExprAsNu, ExprCol}; + use crate::dataframe::expressions::{ExprAlias, ExprCol}; use super::*; @@ -142,7 +142,7 @@ mod test { Box::new(ExprAlias {}), Box::new(ExprWhen {}), Box::new(ExprOtherwise {}), - Box::new(ExprAsNu {}), + Box::new(ToNu {}), ]) } }