* Fix #3582

* Fix empty?

* Fix parsing types
This commit is contained in:
JT
2021-06-09 18:07:54 +12:00
committed by GitHub
parent 25ba6ea459
commit 440e12abc4
9 changed files with 47 additions and 165 deletions

View File

@ -7,7 +7,6 @@ use nu_protocol::{
SyntaxShape, UntaggedValue, Value,
};
use crate::utils::arguments::arguments;
use nu_value_ext::{as_string, ValueExt};
pub struct Command;
@ -18,10 +17,17 @@ impl WholeStreamCommand for Command {
}
fn signature(&self) -> Signature {
Signature::build("empty?").rest(
SyntaxShape::Any,
"the names of the columns to check emptiness. Pass an optional block to replace if empty",
)
Signature::build("empty?")
.rest(
SyntaxShape::ColumnPath,
"the names of the columns to check emptiness",
)
.named(
"block",
SyntaxShape::Block,
"an optional block to replace if empty",
Some('b'),
)
}
fn usage(&self) -> &str {
@ -58,7 +64,7 @@ impl WholeStreamCommand for Command {
),
},Example {
description: "use a block if setting the empty cell contents is wanted",
example: "echo [[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 { [33 37] }",
example: "echo [[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 -b { [33 37] }",
result: Some(
vec![
UntaggedValue::row(indexmap! {
@ -79,11 +85,10 @@ fn is_empty(args: CommandArgs) -> Result<ActionStream, ShellError> {
let name_tag = Arc::new(args.call_info.name_tag.clone());
let context = Arc::new(EvaluationContext::from_args(&args));
let args = args.evaluate_once()?;
let mut rest = args.rest(0)?;
let (columns, default_block): (Vec<ColumnPath>, Option<Box<CapturedBlock>>) =
arguments(&mut rest)?;
let block: Option<CapturedBlock> = args.get_flag("block")?;
let columns: Vec<ColumnPath> = args.rest(0)?;
let input = args.input;
let default_block = Arc::new(default_block);
if input.is_empty() {
let stream = vec![UntaggedValue::Primitive(Primitive::Nothing).into_value(tag)].into_iter();
@ -92,10 +97,9 @@ fn is_empty(args: CommandArgs) -> Result<ActionStream, ShellError> {
.map(move |input| {
let tag = name_tag.clone();
let context = context.clone();
let block = default_block.clone();
let columns = vec![];
match process_row(context, input, block, columns, tag) {
match process_row(context, input, &block, columns, tag) {
Ok(s) => s,
Err(e) => ActionStream::one(Err(e)),
}
@ -108,10 +112,9 @@ fn is_empty(args: CommandArgs) -> Result<ActionStream, ShellError> {
.map(move |input| {
let tag = name_tag.clone();
let context = context.clone();
let block = default_block.clone();
let columns = columns.clone();
match process_row(context, input, block, columns, tag) {
match process_row(context, input, &block, columns, tag) {
Ok(s) => s,
Err(e) => ActionStream::one(Err(e)),
}
@ -123,7 +126,7 @@ fn is_empty(args: CommandArgs) -> Result<ActionStream, ShellError> {
fn process_row(
context: Arc<EvaluationContext>,
input: Value,
default_block: Arc<Option<Box<CapturedBlock>>>,
default_block: &Option<CapturedBlock>,
column_paths: Vec<ColumnPath>,
tag: Arc<Tag>,
) -> Result<ActionStream, ShellError> {

View File

@ -1,5 +1,4 @@
use crate::prelude::*;
use crate::utils::arguments::arguments;
use indexmap::set::IndexSet;
use log::trace;
use nu_engine::WholeStreamCommand;
@ -20,7 +19,7 @@ impl WholeStreamCommand for Command {
fn signature(&self) -> Signature {
Signature::build("get").rest(
SyntaxShape::Any,
SyntaxShape::ColumnPath,
"optionally return additional data by path",
)
}
@ -51,11 +50,9 @@ impl WholeStreamCommand for Command {
pub fn get(args: CommandArgs) -> Result<ActionStream, ShellError> {
let args = args.evaluate_once()?;
let mut rest: Vec<Value> = args.rest(0)?;
let column_paths: Vec<ColumnPath> = args.rest(0)?;
let mut input = args.input;
let (column_paths, _) = arguments(&mut rest)?;
if column_paths.is_empty() {
let vec = input.drain_vec();

View File

@ -1,10 +1,9 @@
use crate::prelude::*;
use crate::utils::arguments::arguments;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{
PathMember, Primitive, Signature, SyntaxShape, TaggedDictBuilder, UnspannedPathMember,
UntaggedValue, Value,
ColumnPath, PathMember, Primitive, Signature, SyntaxShape, TaggedDictBuilder,
UnspannedPathMember, UntaggedValue, Value,
};
use nu_value_ext::{as_string, get_data_by_column_path};
@ -16,7 +15,10 @@ impl WholeStreamCommand for Command {
}
fn signature(&self) -> Signature {
Signature::build("select").rest(SyntaxShape::Any, "the columns to select from the table")
Signature::build("select").rest(
SyntaxShape::ColumnPath,
"the columns to select from the table",
)
}
fn usage(&self) -> &str {
@ -46,9 +48,8 @@ impl WholeStreamCommand for Command {
fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let args = args.evaluate_once()?;
let mut rest = args.rest(0)?;
let columns: Vec<ColumnPath> = args.rest(0)?;
let input = args.input;
let (columns, _) = arguments(&mut rest)?;
if columns.is_empty() {
return Err(ShellError::labeled_error(

View File

@ -108,7 +108,7 @@ fn sort_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let mut args = args.evaluate_once()?;
let rest = args.rest(0)?;
let rest: Vec<Tagged<String>> = args.rest(0)?;
let insensitive = args.has_flag("insensitive");
let reverse = args.has_flag("reverse");
let mut vec = args.input.drain_vec();

View File

@ -1,5 +1,4 @@
use crate::prelude::*;
use crate::utils::arguments::arguments;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{
@ -127,16 +126,14 @@ impl WholeStreamCommand for SubCommand {
struct DatetimeFormat(String);
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (options, input) = args.extract(|params| {
let (column_paths, _) = arguments(&mut params.rest(0)?)?;
Ok(Arguments {
timezone: params.get_flag("timezone")?,
offset: params.get_flag("offset")?,
format: params.get_flag("format")?,
column_paths,
})
})?;
let args = args.evaluate_once()?;
let options = Arguments {
timezone: args.get_flag("timezone")?,
offset: args.get_flag("offset")?,
format: args.get_flag("format")?,
column_paths: args.rest(0)?,
};
let input = args.input;
// if zone-offset is specified, then zone will be neglected
let zone_options = if let Some(Tagged {

View File

@ -1,5 +1,4 @@
use crate::prelude::*;
use crate::utils::arguments::arguments;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::ShellTypeName;
@ -25,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("str to-int")
.named("radix", SyntaxShape::Number, "radix of integer", Some('r'))
.rest(
SyntaxShape::Any,
SyntaxShape::ColumnPath,
"optionally convert text into integer by column paths",
)
}
@ -65,14 +64,12 @@ impl WholeStreamCommand for SubCommand {
}
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (options, input) = args.extract(|params| {
let (column_paths, _) = arguments(&mut params.rest(0)?)?;
Ok(Arguments {
radix: params.get_flag("radix")?,
column_paths,
})
})?;
let args = args.evaluate_once()?;
let options = Arguments {
radix: args.get_flag("radix")?,
column_paths: args.rest(0)?,
};
let input = args.input;
let radix = options.radix.as_ref().map(|r| r.item).unwrap_or(10);