Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify empty?, improve default #4797

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Simplify empty?, improve default
  • Loading branch information
sophiajt committed Mar 9, 2022
commit 0d81db9839d53cf4f42d505ff08f70d0a4cacbbc
99 changes: 62 additions & 37 deletions crates/nu-command/src/filters/default.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
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)]
pub struct Default;
Expand All @@ -13,12 +13,12 @@ impl Command for Default {

fn signature(&self) -> Signature {
Signature::build("default")
.required("column name", SyntaxShape::String, "the name of the column")
.required(
"column value",
"default value",
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)
}

Expand All @@ -37,11 +37,26 @@ impl Command for Default {
}

fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Give a default 'target' to all file entries",
example: "ls -la | default target 'nothing'",
result: None,
}]
vec![
Example {
description: "Give a default 'target' column to all file entries",
example: "ls -la | default 'nothing' target ",
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(),
}),
},
]
}
}

Expand All @@ -51,42 +66,52 @@ fn default(
call: &Call,
input: PipelineData,
) -> 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, 1)?;
let value: Value = call.req(engine_state, stack, 0)?;
let column: Option<Spanned<String>> = call.opt(engine_state, stack, 1)?;

let ctrlc = engine_state.ctrlc.clone();

input.map(
move |item| match item {
Value::Record {
mut cols,
mut vals,
span,
} => {
let mut idx = 0;
let mut found = false;
if let Some(column) = column {
input.map(
move |item| match item {
Value::Record {
mut cols,
mut vals,
span,
} => {
let mut idx = 0;
let mut found = false;

while idx < cols.len() {
if cols[idx] == column.item {
found = true;
if matches!(vals[idx], Value::Nothing { .. }) {
vals[idx] = value.clone();
while idx < cols.len() {
if cols[idx] == column.item {
found = true;
if matches!(vals[idx], Value::Nothing { .. }) {
vals[idx] = value.clone();
}
}
idx += 1;
}
idx += 1;
}

if !found {
cols.push(column.item.clone());
vals.push(value.clone());
}
if !found {
cols.push(column.item.clone());
vals.push(value.clone());
}

Value::Record { cols, vals, span }
}
_ => item,
},
ctrlc,
)
Value::Record { cols, vals, span }
}
_ => item,
},
ctrlc,
)
} else {
input.map(
move |item| match item {
Value::Nothing { .. } => value.clone(),
x => x,
},
ctrlc,
)
}
}

#[cfg(test)]
Expand Down
220 changes: 66 additions & 154 deletions crates/nu-command/src/filters/empty.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use nu_engine::{eval_block, CallExt};
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Category, Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value,
};

#[derive(Clone)]
Expand All @@ -20,12 +20,6 @@ impl Command for Empty {
SyntaxShape::CellPath,
"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)
}

Expand Down Expand Up @@ -54,46 +48,13 @@ impl Command for Empty {
}),
},
Example {
description: "more than one column",
description: "Check if more than one column are empty",
example: "[[meal size]; [arepa small] [taco '']] | empty? meal size",
result: Some(
Value::List {
vals: vec![
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()
})
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
},
Example {
description: "use a block if setting the empty cell contents is wanted",
example: "[[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 -b { |_| [33 37] }",
result: Some(
Value::List {
vals: vec![
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()
}
)
}
]
}
}
Expand All @@ -106,126 +67,77 @@ fn empty(
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let has_block = call.has_flag("block");

let block = if has_block {
let block_expr = call
.get_flag_expr("block")
.expect("internal error: expected block");

let block_id = block_expr
.as_block()
.ok_or_else(|| ShellError::TypeMismatch("expected row condition".to_owned(), head))?;

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 }
if !columns.is_empty() {
for val in input {
for column in &columns {
let val = val.clone();
match val.follow_cell_path(&column.members) {
Ok(Value::Nothing { .. }) => {}
Ok(_) => {
return Ok(Value::Bool {
val: false,
span: head,
}
} 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 };
.into_pipeline_data())
}
Err(err) => return Err(err),
}
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,
}
} else {
Value::Bool {
val: true,
span: head,
Ok(Value::Bool {
val: true,
span: head,
}
.into_pipeline_data())
} else {
match input {
PipelineData::ExternalStream { stdout, .. } => match stdout {
Some(s) => {
let bytes = s.into_bytes();

match bytes {
Ok(s) => Ok(Value::Bool {
val: s.item.is_empty(),
span: head,
}
.into_pipeline_data()),
Err(err) => Err(err),
}
}
}
}
Value::List { vals, .. } => {
let empty = vals.iter().all(|v| v.clone().is_empty());
Value::Bool {
val: empty,
None => Ok(Value::Bool {
val: true,
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())
}
}
other => Value::Bool {
val: other.is_empty(),
span: head,
},
}
}

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,
}
}

Expand Down
Loading