Add select command which opens cell

This commit is contained in:
Jonathan Turner 2019-05-29 16:02:36 +12:00
parent 2afa785e0e
commit 8e00cd43a8
4 changed files with 38 additions and 1 deletions

View File

@ -54,6 +54,7 @@ pub async fn cli() -> Result<(), Box<Error>> {
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),

View File

@ -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;

36
src/commands/select.rs Normal file
View File

@ -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<OutputStream, ShellError> {
if args.args.is_empty() {
return Err(ShellError::string("select requires a field"));
}
let fields: Result<Vec<String>, _> = 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())
}

View File

@ -17,7 +17,6 @@ impl Scope {
crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, ShellError> {
use ast::*;
match expr {
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),