From 8e00cd43a80307d4b6b0e04c2dd3d8ce89b043c4 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 29 May 2019 16:02:36 +1200 Subject: [PATCH] Add select command which opens cell --- src/cli.rs | 1 + src/commands.rs | 1 + src/commands/select.rs | 36 ++++++++++++++++++++++++++++++++++++ src/evaluate/evaluator.rs | 1 - 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/commands/select.rs diff --git a/src/cli.rs b/src/cli.rs index 23f6db7abd..18f25885df 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -54,6 +54,7 @@ pub async fn cli() -> Result<(), Box> { command("column", column::column), command("split", split::split), command("reject", reject::reject), + command("select", select::select), command("to-array", to_array::to_array), command("to-json", to_json::to_json), Arc::new(Where), diff --git a/src/commands.rs b/src/commands.rs index 4b020a7638..06829b48a8 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -8,6 +8,7 @@ crate mod ls; crate mod open; crate mod ps; crate mod reject; +crate mod select; crate mod size; crate mod skip; crate mod sort_by; diff --git a/src/commands/select.rs b/src/commands/select.rs new file mode 100644 index 0000000000..fc84f9736e --- /dev/null +++ b/src/commands/select.rs @@ -0,0 +1,36 @@ +use crate::errors::ShellError; +use crate::object::Value; +use crate::object::base::select_fields; +use crate::prelude::*; + +pub fn select(args: CommandArgs) -> Result { + if args.args.is_empty() { + return Err(ShellError::string("select requires a field")); + } + + let fields: Result, _> = args.args.iter().map(|a| a.as_string()).collect(); + let fields = fields?; + + let stream = args + .input + .map(move |item| { + let mut result = VecDeque::new(); + let column = select_fields(&item, &fields); + for field in &fields { + match column.get_data_by_key(&field) { + Some(Value::List(l)) => { + for item in l { + result.push_back(ReturnValue::Value(item.copy())); + } + } + Some(x) => result.push_back(ReturnValue::Value(x.copy())), + None => {} + } + } + + result + }) + .flatten(); + + Ok(stream.boxed()) +} diff --git a/src/evaluate/evaluator.rs b/src/evaluate/evaluator.rs index f5c681fa84..02a4cbe86a 100644 --- a/src/evaluate/evaluator.rs +++ b/src/evaluate/evaluator.rs @@ -17,7 +17,6 @@ impl Scope { crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result { use ast::*; - match expr { Expression::Leaf(l) => Ok(evaluate_leaf(l)), Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),