Simplify empty?, improve default (#4797)

* Simplify empty?, improve default

* improve test
This commit is contained in:
JT 2022-03-09 08:46:28 -05:00 committed by GitHub
parent 0d82d7df60
commit 355b1d9929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 195 deletions

View File

@ -1,7 +1,7 @@
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, Signature, Spanned, SyntaxShape, Value}; use nu_protocol::{Category, Example, PipelineData, Signature, Span, Spanned, SyntaxShape, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct Default; pub struct Default;
@ -13,12 +13,12 @@ impl Command for Default {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("default") Signature::build("default")
.required("column name", SyntaxShape::String, "the name of the column")
.required( .required(
"column value", "default value",
SyntaxShape::Any, SyntaxShape::Any,
"the value of the column to default", "the value to use as a default",
) )
.optional("column name", SyntaxShape::String, "the name of the column")
.category(Category::Filters) .category(Category::Filters)
} }
@ -37,11 +37,26 @@ impl Command for Default {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
description: "Give a default 'target' to all file entries", Example {
example: "ls -la | default target 'nothing'", description: "Give a default 'target' column to all file entries",
example: "ls -la | default 'nothing' target ",
result: None, result: None,
}] },
Example {
description: "Default the `$nothing` value in a list",
example: "[1, 2, $nothing, 4] | default 3",
result: Some(Value::List {
vals: vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
Value::test_int(4),
],
span: Span::test_data(),
}),
},
]
} }
} }
@ -51,11 +66,12 @@ fn default(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let column: Spanned<String> = call.req(engine_state, stack, 0)?; let value: Value = call.req(engine_state, stack, 0)?;
let value: Value = call.req(engine_state, stack, 1)?; let column: Option<Spanned<String>> = call.opt(engine_state, stack, 1)?;
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
if let Some(column) = column {
input.map( input.map(
move |item| match item { move |item| match item {
Value::Record { Value::Record {
@ -87,6 +103,15 @@ fn default(
}, },
ctrlc, ctrlc,
) )
} else {
input.map(
move |item| match item {
Value::Nothing { .. } => value.clone(),
x => x,
},
ctrlc,
)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,8 +1,8 @@
use nu_engine::{eval_block, CallExt}; use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath}; use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, Category, Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -20,12 +20,6 @@ impl Command for Empty {
SyntaxShape::CellPath, SyntaxShape::CellPath,
"the names of the columns to check emptiness", "the names of the columns to check emptiness",
) )
.named(
"block",
SyntaxShape::Block(Some(vec![SyntaxShape::Any])),
"an optional block to replace if empty",
Some('b'),
)
.category(Category::Filters) .category(Category::Filters)
} }
@ -46,7 +40,7 @@ impl Command for Empty {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![ vec![
Example { Example {
description: "Check if a value is empty", description: "Check if a string is empty",
example: "'' | empty?", example: "'' | empty?",
result: Some(Value::Bool { result: Some(Value::Bool {
val: true, val: true,
@ -54,46 +48,21 @@ impl Command for Empty {
}), }),
}, },
Example { Example {
description: "more than one column", description: "Check if a list is empty",
example: "[[meal size]; [arepa small] [taco '']] | empty? meal size", example: "[] | empty?",
result: Some( result: Some(Value::Bool {
Value::List { val: true,
vals: vec![ span: Span::test_data(),
Value::Record{cols: vec!["meal".to_string(), "size".to_string()], vals: vec![ }),
Value::Bool{val: false, span: Span::test_data()},
Value::Bool{val: false, span: Span::test_data()}
], span: Span::test_data()},
Value::Record{cols: vec!["meal".to_string(), "size".to_string()], vals: vec![
Value::Bool{val: false, span: Span::test_data()},
Value::Bool{val: true, span: Span::test_data()}
], span: Span::test_data()}
], span: Span::test_data()
})
}, },
Example { Example {
description: "use a block if setting the empty cell contents is wanted", description: "Check if more than one column are empty",
example: "[[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 -b { |_| [33 37] }", example: "[[meal size]; [arepa small] [taco '']] | empty? meal size",
result: Some( result: Some(Value::Bool {
Value::List { val: false,
vals: vec![ span: Span::test_data(),
Value::Record{ }),
cols: vec!["2020/04/16".to_string(), "2020/07/10".to_string(), "2020/11/16".to_string()], },
vals: vec![
Value::List{vals: vec![
Value::Int{val: 33, span: Span::test_data()},
Value::Int{val: 37, span: Span::test_data()}
], span: Span::test_data()},
Value::List{vals: vec![
Value::Int{val: 27, span: Span::test_data()},
], span: Span::test_data()},
Value::List{vals: vec![
Value::Int{val: 37, span: Span::test_data()},
], span: Span::test_data()},
], span: Span::test_data()}
], span: Span::test_data()
}
)
}
] ]
} }
} }
@ -106,126 +75,77 @@ fn empty(
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head; let head = call.head;
let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?; let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let has_block = call.has_flag("block");
let block = if has_block { if !columns.is_empty() {
let block_expr = call for val in input {
.get_flag_expr("block") for column in &columns {
.expect("internal error: expected block"); let val = val.clone();
match val.follow_cell_path(&column.members) {
let block_id = block_expr Ok(Value::Nothing { .. }) => {}
.as_block() Ok(_) => {
.ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), head))?; return Ok(Value::Bool {
val: false,
let b = engine_state.get_block(block_id);
let evaluated_block = eval_block(
engine_state,
stack,
b,
PipelineData::new(head),
call.redirect_stdout,
call.redirect_stderr,
)?;
Some(evaluated_block.into_value(head))
} else {
None
};
input.map(
move |value| {
let columns = columns.clone();
process_row(value, block.as_ref(), columns, head)
},
engine_state.ctrlc.clone(),
)
}
fn process_row(
input: Value,
default_block: Option<&Value>,
column_paths: Vec<CellPath>,
head: Span,
) -> Value {
match input {
Value::Record {
cols: _,
ref vals,
span,
} => {
if column_paths.is_empty() {
let is_empty = vals.iter().all(|v| v.clone().is_empty());
if default_block.is_some() {
if is_empty {
Value::Bool { val: true, span }
} else {
input.clone()
}
} else {
Value::Bool {
val: is_empty,
span,
}
}
} else {
let mut obj = input.clone();
for column in column_paths {
let path = column.into_string();
let data = input.get_data_by_key(&path);
let is_empty = match data {
Some(x) => x.is_empty(),
None => true,
};
let default = if let Some(x) = default_block {
if is_empty {
x.clone()
} else {
Value::Bool { val: true, span }
}
} else {
Value::Bool {
val: is_empty,
span,
}
};
let r = obj.update_cell_path(&column.members, Box::new(move |_| default));
if let Err(error) = r {
return Value::Error { error };
}
}
obj
}
}
Value::List { vals, .. } if vals.iter().all(|v| v.as_record().is_ok()) => {
{
// we have records
if column_paths.is_empty() {
let is_empty = vals.is_empty() && vals.iter().all(|v| v.clone().is_empty());
Value::Bool {
val: is_empty,
span: head, span: head,
} }
} else { .into_pipeline_data())
Value::Bool { }
Err(err) => return Err(err),
}
}
}
Ok(Value::Bool {
val: true, val: true,
span: head, span: head,
} }
} .into_pipeline_data())
} } else {
} match input {
Value::List { vals, .. } => { PipelineData::ExternalStream { stdout, .. } => match stdout {
let empty = vals.iter().all(|v| v.clone().is_empty()); Some(s) => {
Value::Bool { let bytes = s.into_bytes();
val: empty,
match bytes {
Ok(s) => Ok(Value::Bool {
val: s.item.is_empty(),
span: head, span: head,
} }
.into_pipeline_data()),
Err(err) => Err(err),
} }
other => Value::Bool { }
val: other.is_empty(), None => Ok(Value::Bool {
val: true,
span: head, span: head,
}
.into_pipeline_data()),
}, },
PipelineData::ListStream(s, ..) => Ok(Value::Bool {
val: s.count() == 0,
span: head,
}
.into_pipeline_data()),
PipelineData::Value(value, ..) => {
let answer = is_empty(value);
Ok(Value::Bool {
val: answer,
span: head,
}
.into_pipeline_data())
}
}
}
}
pub fn is_empty(value: Value) -> bool {
match value {
Value::List { vals, .. } => vals.is_empty(),
Value::String { val, .. } => val.is_empty(),
Value::Binary { val, .. } => val.is_empty(),
Value::Nothing { .. } => true,
Value::Record { cols, .. } => cols.is_empty(),
_ => false,
} }
} }

View File

@ -50,7 +50,7 @@ fn keeps_remaining_original_names_given_less_new_names_than_total_original_names
open los_cuatro_mosqueteros.txt open los_cuatro_mosqueteros.txt
| lines | lines
| wrap name | wrap name
| default hit "arepa!" | default "arepa!" hit
| rename mosqueteros | rename mosqueteros
| get hit | get hit
| length | length

View File

@ -235,7 +235,7 @@ fn length_for_rows() -> TestResult {
#[test] #[test]
fn length_defaulted_columns() -> TestResult { fn length_defaulted_columns() -> TestResult {
run_test( run_test(
r#"echo [[name, age]; [test, 10]] | default age 11 | get 0 | columns | length"#, r#"echo [[name, age]; [test, 10]] | default 11 age | get 0 | columns | length"#,
"2", "2",
) )
} }