Fix replacement closures for update, insert, and upsert (#11258)

# Description
This PR addresses #11204 which points out that using a closure for the
replacement value with `update`, `insert`, or `upsert` does not work for
lists.

# User-Facing Changes
- Replacement closures should now work for lists in `upsert`, `insert`,
and `update`. E.g., `[0] | update 0 {|i| $i + 1 }` now gives `[1]`
instead of an unhelpful error.
- `[1 2] | insert 4 20` no longer works. Before, this would give `[1, 2,
null, null, 20]`, but now it gives an error. This was done to match the
intended behavior in `Value::insert_data_at_cell_path`, whereas the
behavior before was probably unintentional. Following
`Value::insert_data_at_cell_path`, inserting at the end of a list is
also fine, so the valid indices for `upsert` and `insert` are
`0..=length` just like `Vec::insert` or list inserts in other languages.

# Tests + Formatting
Added tests for `upsert`, `insert`, and `update`:
- Replacement closures for lists, list streams, records, and tables
- Other list stream tests
This commit is contained in:
Ian Manske 2023-12-09 21:22:45 +00:00 committed by GitHub
parent 94b27267fd
commit fa5d7babb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1116 additions and 383 deletions

View File

@ -1,9 +1,9 @@
use nu_engine::{eval_block, CallExt}; use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Block, Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -57,49 +57,64 @@ impl Command for Insert {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
description: "Insert a new entry into a single record", Example {
example: "{'name': 'nu', 'stars': 5} | insert alias 'Nushell'", description: "Insert a new entry into a single record",
result: Some(Value::test_record(record! { example: "{'name': 'nu', 'stars': 5} | insert alias 'Nushell'",
"name" => Value::test_string("nu"), result: Some(Value::test_record(record! {
"stars" => Value::test_int(5), "name" => Value::test_string("nu"),
"alias" => Value::test_string("Nushell"), "stars" => Value::test_int(5),
})), "alias" => Value::test_string("Nushell"),
}, })),
Example { },
description: "Insert a new column into a table, populating all rows", Example {
example: "[[project, lang]; ['Nushell', 'Rust']] | insert type 'shell'", description: "Insert a new column into a table, populating all rows",
result: Some(Value::test_list ( example: "[[project, lang]; ['Nushell', 'Rust']] | insert type 'shell'",
vec![Value::test_record(record! { result: Some(Value::test_list(vec![Value::test_record(record! {
"project" => Value::test_string("Nushell"), "project" => Value::test_string("Nushell"),
"lang" => Value::test_string("Rust"), "lang" => Value::test_string("Rust"),
"type" => Value::test_string("shell"), "type" => Value::test_string("shell"),
})], })])),
)), },
}, Example {
Example { description: "Insert a new column with values computed based off the other columns",
description: "Insert a column with values equal to their row index, plus the value of 'foo' in each row", example: "[[foo]; [7] [8] [9]] | insert bar {|row| $row.foo * 2 }",
example: "[[foo]; [7] [8] [9]] | enumerate | insert bar {|e| $e.item.foo + $e.index } | flatten", result: Some(Value::test_list(vec![
result: Some(Value::test_list (
vec![
Value::test_record(record! { Value::test_record(record! {
"index" => Value::test_int(0), "foo" => Value::test_int(7),
"foo" => Value::test_int(7), "bar" => Value::test_int(14),
"bar" => Value::test_int(7),
}), }),
Value::test_record(record! { Value::test_record(record! {
"index" => Value::test_int(1), "foo" => Value::test_int(8),
"foo" => Value::test_int(8), "bar" => Value::test_int(16),
"bar" => Value::test_int(9),
}), }),
Value::test_record(record! { Value::test_record(record! {
"index" => Value::test_int(2), "foo" => Value::test_int(9),
"foo" => Value::test_int(9), "bar" => Value::test_int(18),
"bar" => Value::test_int(11),
}), }),
], ])),
)), },
}] Example {
description: "Insert a new value into a list at an index",
example: "[1 2 4] | insert 2 3",
result: Some(Value::test_list(vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
Value::test_int(4),
])),
},
Example {
description: "Insert a new value at the end of a list",
example: "[1 2 3] | insert 3 4",
result: Some(Value::test_list(vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
Value::test_int(4),
])),
},
]
} }
} }
@ -109,7 +124,6 @@ fn insert(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let span = call.head; let span = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?; let cell_path: CellPath = call.req(engine_state, stack, 0)?;
@ -118,99 +132,263 @@ fn insert(
let redirect_stdout = call.redirect_stdout; let redirect_stdout = call.redirect_stdout;
let redirect_stderr = call.redirect_stderr; let redirect_stderr = call.redirect_stderr;
let engine_state = engine_state.clone();
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
// Replace is a block, so set it up and run it instead of using it as the replacement match input {
if replacement.as_block().is_ok() { PipelineData::Value(mut value, metadata) => {
let capture_block = Closure::from_value(replacement)?; if replacement.as_block().is_ok() {
let block = engine_state.get_block(capture_block.block_id).clone(); match (cell_path.members.first(), &mut value) {
(Some(PathMember::String { .. }), Value::List { vals, .. }) => {
let mut stack = stack.captures_to_stack(capture_block.captures); let span = replacement.span();
let orig_env_vars = stack.env_vars.clone(); let capture_block = Closure::from_value(replacement)?;
let orig_env_hidden = stack.env_hidden.clone(); let block = engine_state.get_block(capture_block.block_id);
let stack = stack.captures_to_stack(capture_block.captures.clone());
input for val in vals {
.map( let mut stack = stack.clone();
move |mut input| { insert_value_by_closure(
// with_env() is used here to ensure that each iteration uses val,
// a different set of environment variables. span,
// Hence, a 'cd' in the first loop won't affect the next loop. engine_state,
stack.with_env(&orig_env_vars, &orig_env_hidden); &mut stack,
redirect_stdout,
// Element argument redirect_stderr,
if let Some(var) = block.signature.get_positional(0) { block,
if let Some(var_id) = &var.var_id { &cell_path.members,
stack.add_var(*var_id, input.clone()) false,
)?;
} }
} }
(first, _) => {
insert_single_value_by_closure(
&mut value,
replacement,
engine_state,
stack,
redirect_stdout,
redirect_stderr,
&cell_path.members,
matches!(first, Some(PathMember::Int { .. })),
)?;
}
}
} else {
value.insert_data_at_cell_path(&cell_path.members, replacement, span)?;
}
Ok(value.into_pipeline_data_with_metadata(metadata))
}
PipelineData::ListStream(mut stream, metadata) => {
if let Some((
&PathMember::Int {
val,
span: path_span,
..
},
path,
)) = cell_path.members.split_first()
{
let mut pre_elems = vec![];
let output = eval_block( for idx in 0..val {
&engine_state, if let Some(v) = stream.next() {
&mut stack, pre_elems.push(v);
&block, } else {
input.clone().into_pipeline_data(), return Err(ShellError::InsertAfterNextFreeIndex {
redirect_stdout, available_idx: idx,
redirect_stderr, span: path_span,
); });
}
}
match output { if path.is_empty() {
Ok(pd) => { if replacement.as_block().is_ok() {
let span = pd.span().unwrap_or(span); let span = replacement.span();
if let Err(e) = input.insert_data_at_cell_path( let value = stream.next();
&cell_path.members, let end_of_stream = value.is_none();
pd.into_value(span), let value = value.unwrap_or(Value::nothing(span));
span, let capture_block = Closure::from_value(replacement)?;
) { let block = engine_state.get_block(capture_block.block_id);
return Value::error(e, span); let mut stack = stack.captures_to_stack(capture_block.captures);
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value.clone())
} }
}
let output = eval_block(
engine_state,
&mut stack,
block,
value.clone().into_pipeline_data(),
redirect_stdout,
redirect_stderr,
)?;
pre_elems.push(output.into_value(span));
if !end_of_stream {
pre_elems.push(value);
}
} else {
pre_elems.push(replacement);
}
} else if let Some(mut value) = stream.next() {
if replacement.as_block().is_ok() {
insert_single_value_by_closure(
&mut value,
replacement,
engine_state,
stack,
redirect_stdout,
redirect_stderr,
path,
true,
)?;
} else {
value.insert_data_at_cell_path(path, replacement, span)?;
}
pre_elems.push(value)
} else {
return Err(ShellError::AccessBeyondEnd {
max_idx: pre_elems.len() - 1,
span: path_span,
});
}
Ok(pre_elems
.into_iter()
.chain(stream)
.into_pipeline_data_with_metadata(metadata, ctrlc))
} else if replacement.as_block().is_ok() {
let engine_state = engine_state.clone();
let replacement_span = replacement.span();
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let stack = stack.captures_to_stack(capture_block.captures.clone());
Ok(stream
.map(move |mut input| {
// Recreate the stack for each iteration to
// isolate environment variable changes, etc.
let mut stack = stack.clone();
let err = insert_value_by_closure(
&mut input,
replacement_span,
&engine_state,
&mut stack,
redirect_stdout,
redirect_stderr,
&block,
&cell_path.members,
false,
);
if let Err(e) = err {
Value::error(e, span)
} else {
input input
} }
Err(e) => Value::error(e, span), })
} .into_pipeline_data_with_metadata(metadata, ctrlc))
}, } else {
ctrlc, Ok(stream
) .map(move |mut input| {
.map(|x| x.set_metadata(metadata)) if let Err(e) = input.insert_data_at_cell_path(
} else { &cell_path.members,
if let Some(PathMember::Int { val, .. }) = cell_path.members.first() { replacement.clone(),
let mut input = input.into_iter(); span,
let mut pre_elems = vec![]; ) {
Value::error(e, span)
for _ in 0..*val { } else {
if let Some(v) = input.next() { input
pre_elems.push(v); }
} else { })
pre_elems.push(Value::nothing(span)) .into_pipeline_data_with_metadata(metadata, ctrlc))
}
} }
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data_with_metadata(metadata, ctrlc));
} }
input PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
.map( type_name: "empty pipeline".to_string(),
move |mut input| { span,
let replacement = replacement.clone(); }),
PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess {
if let Err(e) = type_name: "external stream".to_string(),
input.insert_data_at_cell_path(&cell_path.members, replacement, span) span,
{ }),
return Value::error(e, span);
}
input
},
ctrlc,
)
.map(|x| x.set_metadata(metadata))
} }
} }
#[allow(clippy::too_many_arguments)]
fn insert_value_by_closure(
value: &mut Value,
span: Span,
engine_state: &EngineState,
stack: &mut Stack,
redirect_stdout: bool,
redirect_stderr: bool,
block: &Block,
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let input_at_path = value.clone().follow_cell_path(cell_path, false);
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(
*var_id,
if first_path_member_int {
input_at_path.clone().unwrap_or(Value::nothing(span))
} else {
value.clone()
},
)
}
}
let input_at_path = input_at_path
.map(IntoPipelineData::into_pipeline_data)
.unwrap_or(PipelineData::Empty);
let output = eval_block(
engine_state,
stack,
block,
input_at_path,
redirect_stdout,
redirect_stderr,
)?;
value.insert_data_at_cell_path(cell_path, output.into_value(span), span)
}
#[allow(clippy::too_many_arguments)]
fn insert_single_value_by_closure(
value: &mut Value,
replacement: Value,
engine_state: &EngineState,
stack: &mut Stack,
redirect_stdout: bool,
redirect_stderr: bool,
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let span = replacement.span();
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
insert_value_by_closure(
value,
span,
engine_state,
&mut stack,
redirect_stdout,
redirect_stderr,
block,
cell_path,
first_path_member_int,
)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -1,9 +1,9 @@
use nu_engine::{eval_block, CallExt}; use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Block, Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -63,18 +63,8 @@ impl Command for Update {
})), })),
}, },
Example { Example {
description: "Use in closure form for more involved updating logic", description: "Use a closure to alter each value in the 'authors' column to a single string",
example: "[[count fruit]; [1 'apple']] | enumerate | update item.count {|e| ($e.item.fruit | str length) + $e.index } | get item", example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|row| $row.authors | str join ',' }",
result: Some(Value::test_list(
vec![Value::test_record(record! {
"count" => Value::test_int(5),
"fruit" => Value::test_string("apple"),
})],
)),
},
Example {
description: "Alter each value in the 'authors' column to use a single string instead of a list",
example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|row| $row.authors | str join ','}",
result: Some(Value::test_list( result: Some(Value::test_list(
vec![Value::test_record(record! { vec![Value::test_record(record! {
"project" => Value::test_string("nu"), "project" => Value::test_string("nu"),
@ -84,14 +74,28 @@ impl Command for Update {
}, },
Example { Example {
description: "You can also use a simple command to update 'authors' to a single string", description: "You can also use a simple command to update 'authors' to a single string",
example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|| str join ','}", example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors { str join ',' }",
result: Some(Value::test_list( result: Some(Value::test_list(
vec![Value::test_record(record! { vec![Value::test_record(record! {
"project" => Value::test_string("nu"), "project" => Value::test_string("nu"),
"authors" => Value::test_string("Andrés,JT,Yehuda"), "authors" => Value::test_string("Andrés,JT,Yehuda"),
})], })],
)), )),
} },
Example {
description: "Update a value at an index in a list",
example: "[1 2 3] | update 1 4",
result: Some(Value::test_list(
vec![Value::test_int(1), Value::test_int(4), Value::test_int(3)]
)),
},
Example {
description: "Use a closure to compute a new value at an index",
example: "[1 2 3] | update 1 {|i| $i + 2 }",
result: Some(Value::test_list(
vec![Value::test_int(1), Value::test_int(4), Value::test_int(3)]
)),
},
] ]
} }
} }
@ -110,111 +114,222 @@ fn update(
let redirect_stdout = call.redirect_stdout; let redirect_stdout = call.redirect_stdout;
let redirect_stderr = call.redirect_stderr; let redirect_stderr = call.redirect_stderr;
let engine_state = engine_state.clone();
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
// Let's capture the metadata for ls_colors match input {
let metadata = input.metadata(); PipelineData::Value(mut value, metadata) => {
let mdclone = metadata.clone(); if replacement.as_block().is_ok() {
match (cell_path.members.first(), &mut value) {
// Replace is a block, so set it up and run it instead of using it as the replacement (Some(PathMember::String { .. }), Value::List { vals, .. }) => {
if replacement.as_block().is_ok() { let span = replacement.span();
let capture_block = Closure::from_value(replacement)?; let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id).clone(); let block = engine_state.get_block(capture_block.block_id);
let stack = stack.captures_to_stack(capture_block.captures.clone());
let mut stack = stack.captures_to_stack(capture_block.captures); for val in vals {
let orig_env_vars = stack.env_vars.clone(); let mut stack = stack.clone();
let orig_env_hidden = stack.env_hidden.clone(); update_value_by_closure(
val,
Ok(input span,
.map( engine_state,
move |mut input| { &mut stack,
// with_env() is used here to ensure that each iteration uses redirect_stdout,
// a different set of environment variables. redirect_stderr,
// Hence, a 'cd' in the first loop won't affect the next loop. block,
stack.with_env(&orig_env_vars, &orig_env_hidden); &cell_path.members,
false,
if let Some(var) = block.signature.get_positional(0) { )?;
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, input.clone())
} }
} }
(first, _) => {
update_single_value_by_closure(
&mut value,
replacement,
engine_state,
stack,
redirect_stdout,
redirect_stderr,
&cell_path.members,
matches!(first, Some(PathMember::Int { .. })),
)?;
}
}
} else {
value.update_data_at_cell_path(&cell_path.members, replacement)?;
}
Ok(value.into_pipeline_data_with_metadata(metadata))
}
PipelineData::ListStream(mut stream, metadata) => {
if let Some((
&PathMember::Int {
val,
span: path_span,
..
},
path,
)) = cell_path.members.split_first()
{
let mut pre_elems = vec![];
let input_at_path = for idx in 0..=val {
match input.clone().follow_cell_path(&cell_path.members, false) { if let Some(v) = stream.next() {
Err(e) => return Value::error(e, span), pre_elems.push(v);
Ok(v) => v, } else if idx == 0 {
}; return Err(ShellError::AccessEmptyContent { span: path_span });
let output = eval_block( } else {
&engine_state, return Err(ShellError::AccessBeyondEnd {
&mut stack, max_idx: idx - 1,
&block, span: path_span,
input_at_path.into_pipeline_data_with_metadata(metadata.clone()), });
}
}
// cannot fail since loop above does at least one iteration or returns an error
let value = pre_elems.last_mut().expect("one element");
if replacement.as_block().is_ok() {
update_single_value_by_closure(
value,
replacement,
engine_state,
stack,
redirect_stdout, redirect_stdout,
redirect_stderr, redirect_stderr,
); path,
true,
)?;
} else {
value.update_data_at_cell_path(path, replacement)?;
}
match output { Ok(pre_elems
Ok(pd) => { .into_iter()
if let Err(e) = input .chain(stream)
.update_data_at_cell_path(&cell_path.members, pd.into_value(span)) .into_pipeline_data_with_metadata(metadata, ctrlc))
{ } else if replacement.as_block().is_ok() {
return Value::error(e, span); let replacement_span = replacement.span();
} let engine_state = engine_state.clone();
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let stack = stack.captures_to_stack(capture_block.captures.clone());
Ok(stream
.map(move |mut input| {
// Recreate the stack for each iteration to
// isolate environment variable changes, etc.
let mut stack = stack.clone();
let err = update_value_by_closure(
&mut input,
replacement_span,
&engine_state,
&mut stack,
redirect_stdout,
redirect_stderr,
&block,
&cell_path.members,
false,
);
if let Err(e) = err {
Value::error(e, span)
} else {
input input
} }
Err(e) => Value::error(e, span), })
} .into_pipeline_data_with_metadata(metadata, ctrlc))
}, } else {
ctrlc, Ok(stream
)? .map(move |mut input| {
.set_metadata(mdclone)) if let Err(e) =
} else { input.update_data_at_cell_path(&cell_path.members, replacement.clone())
if let Some(PathMember::Int { val, span, .. }) = cell_path.members.first() { {
let mut input = input.into_iter(); Value::error(e, span)
let mut pre_elems = vec![]; } else {
input
for idx in 0..*val { }
if let Some(v) = input.next() { })
pre_elems.push(v); .into_pipeline_data_with_metadata(metadata, ctrlc))
} else if idx == 0 {
return Err(ShellError::AccessEmptyContent { span: *span });
} else {
return Err(ShellError::AccessBeyondEnd {
max_idx: idx - 1,
span: *span,
});
}
} }
// Skip over the replaced value
let _ = input.next();
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data_with_metadata(metadata, ctrlc));
} }
Ok(input PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
.map( type_name: "empty pipeline".to_string(),
move |mut input| { span,
let replacement = replacement.clone(); }),
PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess {
if let Err(e) = input.update_data_at_cell_path(&cell_path.members, replacement) type_name: "external stream".to_string(),
{ span,
return Value::error(e, span); }),
}
input
},
ctrlc,
)?
.set_metadata(metadata))
} }
} }
#[allow(clippy::too_many_arguments)]
fn update_value_by_closure(
value: &mut Value,
span: Span,
engine_state: &EngineState,
stack: &mut Stack,
redirect_stdout: bool,
redirect_stderr: bool,
block: &Block,
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let input_at_path = value.clone().follow_cell_path(cell_path, false)?;
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(
*var_id,
if first_path_member_int {
input_at_path.clone()
} else {
value.clone()
},
)
}
}
let output = eval_block(
engine_state,
stack,
block,
input_at_path.into_pipeline_data(),
redirect_stdout,
redirect_stderr,
)?;
value.update_data_at_cell_path(cell_path, output.into_value(span))
}
#[allow(clippy::too_many_arguments)]
fn update_single_value_by_closure(
value: &mut Value,
replacement: Value,
engine_state: &EngineState,
stack: &mut Stack,
redirect_stdout: bool,
redirect_stderr: bool,
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let span = replacement.span();
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
update_value_by_closure(
value,
span,
engine_state,
&mut stack,
redirect_stdout,
redirect_stderr,
block,
cell_path,
first_path_member_int,
)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -1,9 +1,9 @@
use nu_engine::{eval_block, CallExt}; use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath, PathMember}; use nu_protocol::ast::{Block, Call, CellPath, PathMember};
use nu_protocol::engine::{Closure, Command, EngineState, Stack}; use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, record, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, SyntaxShape, Type, Value, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -57,19 +57,28 @@ impl Command for Upsert {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
description: "Update a record's value", Example {
example: "{'name': 'nu', 'stars': 5} | upsert name 'Nushell'", description: "Update a record's value",
result: Some(Value::test_record(record! { example: "{'name': 'nu', 'stars': 5} | upsert name 'Nushell'",
"name" => Value::test_string("Nushell"), result: Some(Value::test_record(record! {
"stars" => Value::test_int(5), "name" => Value::test_string("Nushell"),
})), "stars" => Value::test_int(5),
}, })),
Example { },
description: "Update each row of a table", Example {
example: "[[name lang]; [Nushell ''] [Reedline '']] | upsert lang 'Rust'", description: "Insert a new entry into a record",
result: Some(Value::test_list( example: "{'name': 'nu', 'stars': 5} | upsert language 'Rust'",
vec![ result: Some(Value::test_record(record! {
"name" => Value::test_string("nu"),
"stars" => Value::test_int(5),
"language" => Value::test_string("Rust"),
})),
},
Example {
description: "Update each row of a table",
example: "[[name lang]; [Nushell ''] [Reedline '']] | upsert lang 'Rust'",
result: Some(Value::test_list(vec![
Value::test_record(record! { Value::test_record(record! {
"name" => Value::test_string("Nushell"), "name" => Value::test_string("Nushell"),
"lang" => Value::test_string("Rust"), "lang" => Value::test_string("Rust"),
@ -78,46 +87,45 @@ impl Command for Upsert {
"name" => Value::test_string("Reedline"), "name" => Value::test_string("Reedline"),
"lang" => Value::test_string("Rust"), "lang" => Value::test_string("Rust"),
}), }),
], ])),
)), },
}, Example {
Example { description: "Insert a new column with values computed based off the other columns",
description: "Insert a new entry into a single record", example: "[[foo]; [7] [8] [9]] | upsert bar {|row| $row.foo * 2 }",
example: "{'name': 'nu', 'stars': 5} | upsert language 'Rust'", result: Some(Value::test_list(vec![
result: Some(Value::test_record(record! { Value::test_record(record! {
"name" => Value::test_string("nu"), "foo" => Value::test_int(7),
"stars" => Value::test_int(5), "bar" => Value::test_int(14),
"language" => Value::test_string("Rust"), }),
})), Value::test_record(record! {
}, Example { "foo" => Value::test_int(8),
description: "Use in closure form for more involved updating logic", "bar" => Value::test_int(16),
example: "[[count fruit]; [1 'apple']] | enumerate | upsert item.count {|e| ($e.item.fruit | str length) + $e.index } | get item", }),
result: Some(Value::test_list( Value::test_record(record! {
vec![Value::test_record(record! { "foo" => Value::test_int(9),
"count" => Value::test_int(5), "bar" => Value::test_int(18),
"fruit" => Value::test_string("apple"), }),
})], ])),
)), },
}, Example {
Example { description: "Upsert into a list, updating an existing value at an index",
description: "Upsert an int into a list, updating an existing value based on the index", example: "[1 2 3] | upsert 0 2",
example: "[1 2 3] | upsert 0 2", result: Some(Value::test_list(vec![
result: Some(Value::test_list( Value::test_int(2),
vec![Value::test_int(2), Value::test_int(2), Value::test_int(3)], Value::test_int(2),
)), Value::test_int(3),
}, ])),
Example { },
description: "Upsert an int into a list, inserting a new value based on the index", Example {
example: "[1 2 3] | upsert 3 4", description: "Upsert into a list, inserting a new value at the end",
result: Some(Value::test_list( example: "[1 2 3] | upsert 3 4",
vec![ result: Some(Value::test_list(vec![
Value::test_int(1), Value::test_int(1),
Value::test_int(2), Value::test_int(2),
Value::test_int(3), Value::test_int(3),
Value::test_int(4), Value::test_int(4),
], ])),
)), },
},
] ]
} }
} }
@ -128,7 +136,6 @@ fn upsert(
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let span = call.head; let span = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?; let cell_path: CellPath = call.req(engine_state, stack, 0)?;
@ -137,101 +144,256 @@ fn upsert(
let redirect_stdout = call.redirect_stdout; let redirect_stdout = call.redirect_stdout;
let redirect_stderr = call.redirect_stderr; let redirect_stderr = call.redirect_stderr;
let engine_state = engine_state.clone();
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
// Replace is a block, so set it up and run it instead of using it as the replacement match input {
if replacement.as_block().is_ok() { PipelineData::Value(mut value, metadata) => {
let capture_block = Closure::from_value(replacement)?; if replacement.as_block().is_ok() {
let block = engine_state.get_block(capture_block.block_id).clone(); match (cell_path.members.first(), &mut value) {
(Some(PathMember::String { .. }), Value::List { vals, .. }) => {
let mut stack = stack.captures_to_stack(capture_block.captures); let span = replacement.span();
let orig_env_vars = stack.env_vars.clone(); let capture_block = Closure::from_value(replacement)?;
let orig_env_hidden = stack.env_hidden.clone(); let block = engine_state.get_block(capture_block.block_id);
let stack = stack.captures_to_stack(capture_block.captures.clone());
input for val in vals {
.map( let mut stack = stack.clone();
move |mut input| { upsert_value_by_closure(
// with_env() is used here to ensure that each iteration uses val,
// a different set of environment variables. span,
// Hence, a 'cd' in the first loop won't affect the next loop. engine_state,
stack.with_env(&orig_env_vars, &orig_env_hidden); &mut stack,
redirect_stdout,
if let Some(var) = block.signature.get_positional(0) { redirect_stderr,
if let Some(var_id) = &var.var_id { block,
stack.add_var(*var_id, input.clone()) &cell_path.members,
false,
)?;
} }
} }
(first, _) => {
let output = eval_block( upsert_single_value_by_closure(
&engine_state, &mut value,
&mut stack, replacement,
&block, engine_state,
input.clone().into_pipeline_data(), stack,
redirect_stdout, redirect_stdout,
redirect_stderr, redirect_stderr,
); &cell_path.members,
matches!(first, Some(PathMember::Int { .. })),
match output { )?;
Ok(pd) => {
if let Err(e) = input
.upsert_data_at_cell_path(&cell_path.members, pd.into_value(span))
{
return Value::error(e, span);
}
input
}
Err(e) => Value::error(e, span),
} }
}
} else {
value.upsert_data_at_cell_path(&cell_path.members, replacement)?;
}
Ok(value.into_pipeline_data_with_metadata(metadata))
}
PipelineData::ListStream(mut stream, metadata) => {
if let Some((
&PathMember::Int {
val,
span: path_span,
..
}, },
ctrlc, path,
) )) = cell_path.members.split_first()
.map(|x| x.set_metadata(metadata)) {
} else { let mut pre_elems = vec![];
if let Some(PathMember::Int { val, span, .. }) = cell_path.members.first() {
let mut input = input.into_iter();
let mut pre_elems = vec![];
for idx in 0..*val { for idx in 0..val {
if let Some(v) = input.next() { if let Some(v) = stream.next() {
pre_elems.push(v); pre_elems.push(v);
} else {
return Err(ShellError::InsertAfterNextFreeIndex {
available_idx: idx,
span: path_span,
});
}
}
if path.is_empty() {
let span = replacement.span();
let value = stream.next().unwrap_or(Value::nothing(span));
if replacement.as_block().is_ok() {
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(*var_id, value.clone())
}
}
let output = eval_block(
engine_state,
&mut stack,
block,
value.clone().into_pipeline_data(),
redirect_stdout,
redirect_stderr,
)?;
pre_elems.push(output.into_value(span));
} else {
pre_elems.push(replacement);
}
} else if let Some(mut value) = stream.next() {
if replacement.as_block().is_ok() {
upsert_single_value_by_closure(
&mut value,
replacement,
engine_state,
stack,
redirect_stdout,
redirect_stderr,
path,
true,
)?;
} else {
value.upsert_data_at_cell_path(path, replacement)?;
}
pre_elems.push(value)
} else { } else {
return Err(ShellError::AccessBeyondEnd { return Err(ShellError::AccessBeyondEnd {
max_idx: idx, max_idx: pre_elems.len() - 1,
span: *span, span: path_span,
}); });
} }
Ok(pre_elems
.into_iter()
.chain(stream)
.into_pipeline_data_with_metadata(metadata, ctrlc))
} else if replacement.as_block().is_ok() {
let engine_state = engine_state.clone();
let replacement_span = replacement.span();
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id).clone();
let stack = stack.captures_to_stack(capture_block.captures.clone());
Ok(stream
.map(move |mut input| {
// Recreate the stack for each iteration to
// isolate environment variable changes, etc.
let mut stack = stack.clone();
let err = upsert_value_by_closure(
&mut input,
replacement_span,
&engine_state,
&mut stack,
redirect_stdout,
redirect_stderr,
&block,
&cell_path.members,
false,
);
if let Err(e) = err {
Value::error(e, span)
} else {
input
}
})
.into_pipeline_data_with_metadata(metadata, ctrlc))
} else {
Ok(stream
.map(move |mut input| {
if let Err(e) =
input.upsert_data_at_cell_path(&cell_path.members, replacement.clone())
{
Value::error(e, span)
} else {
input
}
})
.into_pipeline_data_with_metadata(metadata, ctrlc))
} }
// Skip over the replaced value
let _ = input.next();
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data_with_metadata(metadata, ctrlc));
} }
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
input type_name: "empty pipeline".to_string(),
.map( span,
move |mut input| { }),
let replacement = replacement.clone(); PipelineData::ExternalStream { .. } => Err(ShellError::IncompatiblePathAccess {
type_name: "external stream".to_string(),
if let Err(e) = input.upsert_data_at_cell_path(&cell_path.members, replacement) span,
{ }),
return Value::error(e, span);
}
input
},
ctrlc,
)
.map(|x| x.set_metadata(metadata))
} }
} }
#[allow(clippy::too_many_arguments)]
fn upsert_value_by_closure(
value: &mut Value,
span: Span,
engine_state: &EngineState,
stack: &mut Stack,
redirect_stdout: bool,
redirect_stderr: bool,
block: &Block,
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let input_at_path = value.clone().follow_cell_path(cell_path, false);
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
stack.add_var(
*var_id,
if first_path_member_int {
input_at_path.clone().unwrap_or(Value::nothing(span))
} else {
value.clone()
},
)
}
}
let input_at_path = input_at_path
.map(IntoPipelineData::into_pipeline_data)
.unwrap_or(PipelineData::Empty);
let output = eval_block(
engine_state,
stack,
block,
input_at_path,
redirect_stdout,
redirect_stderr,
)?;
value.upsert_data_at_cell_path(cell_path, output.into_value(span))
}
#[allow(clippy::too_many_arguments)]
fn upsert_single_value_by_closure(
value: &mut Value,
replacement: Value,
engine_state: &EngineState,
stack: &mut Stack,
redirect_stdout: bool,
redirect_stderr: bool,
cell_path: &[PathMember],
first_path_member_int: bool,
) -> Result<(), ShellError> {
let span = replacement.span();
let capture_block = Closure::from_value(replacement)?;
let block = engine_state.get_block(capture_block.block_id);
let mut stack = stack.captures_to_stack(capture_block.captures);
upsert_value_by_closure(
value,
span,
engine_state,
&mut stack,
redirect_stdout,
redirect_stderr,
block,
cell_path,
first_path_member_int,
)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -44,24 +44,49 @@ fn insert_into_list() {
} }
#[test] #[test]
fn insert_into_list_begin() { fn insert_at_start_of_list() {
let actual = nu!("[1, 2, 3] | insert 0 abc | to json -r"); let actual = nu!("[1, 2, 3] | insert 0 abc | to json -r");
assert_eq!(actual.out, r#"["abc",1,2,3]"#); assert_eq!(actual.out, r#"["abc",1,2,3]"#);
} }
#[test] #[test]
fn insert_into_list_end() { fn insert_at_end_of_list() {
let actual = nu!("[1, 2, 3] | insert 3 abc | to json -r"); let actual = nu!("[1, 2, 3] | insert 3 abc | to json -r");
assert_eq!(actual.out, r#"[1,2,3,"abc"]"#); assert_eq!(actual.out, r#"[1,2,3,"abc"]"#);
} }
#[test] #[test]
fn insert_past_end_list() { fn insert_past_end_of_list() {
let actual = nu!("[1, 2, 3] | insert 5 abc | to json -r"); let actual = nu!("[1, 2, 3] | insert 5 abc");
assert_eq!(actual.out, r#"[1,2,3,null,null,"abc"]"#); assert!(actual
.err
.contains("can't insert at index (the next available index is 3)"));
}
#[test]
fn insert_into_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | insert 1 abc | to json -r");
assert_eq!(actual.out, r#"[1,"abc",2,3]"#);
}
#[test]
fn insert_at_end_of_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | insert 3 abc | to json -r");
assert_eq!(actual.out, r#"[1,2,3,"abc"]"#);
}
#[test]
fn insert_past_end_of_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | insert 5 abc");
assert!(actual
.err
.contains("can't insert at index (the next available index is 3)"));
} }
#[test] #[test]
@ -90,14 +115,82 @@ fn lazy_record_test_values() {
#[test] #[test]
fn deep_cell_path_creates_all_nested_records() { fn deep_cell_path_creates_all_nested_records() {
let actual = nu!(r#"{a: {}} | insert a.b.c 0 | get a.b.c"#); let actual = nu!("{a: {}} | insert a.b.c 0 | get a.b.c");
assert_eq!(actual.out, "0"); assert_eq!(actual.out, "0");
} }
#[test] #[test]
fn inserts_all_rows_in_table_in_record() { fn inserts_all_rows_in_table_in_record() {
let actual = nu!( let actual = nu!(
r#"{table: [[col]; [{a: 1}], [{a: 1}]]} | insert table.col.b 2 | get table.col.b | to nuon"# "{table: [[col]; [{a: 1}], [{a: 1}]]} | insert table.col.b 2 | get table.col.b | to nuon"
); );
assert_eq!(actual.out, "[2, 2]"); assert_eq!(actual.out, "[2, 2]");
} }
#[test]
fn list_replacement_closure() {
let actual = nu!("[1, 2] | insert 1 {|i| $i + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3, 2]");
let actual = nu!("[1, 2] | insert 1 { $in + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3, 2]");
let actual = nu!("[1, 2] | insert 2 {|i| if $i == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
let actual = nu!("[1, 2] | insert 2 { if $in == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
}
#[test]
fn record_replacement_closure() {
let actual = nu!("{ a: text } | insert b {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "{a: text, b: TEXT}");
let actual = nu!("{ a: text } | insert b { default TEXT } | to nuon");
assert_eq!(actual.out, "{a: text, b: TEXT}");
let actual = nu!("{ a: { b: 1 } } | insert a.c {|r| $r.a.b } | to nuon");
assert_eq!(actual.out, "{a: {b: 1, c: 1}}");
let actual = nu!("{ a: { b: 1 } } | insert a.c { default 0 } | to nuon");
assert_eq!(actual.out, "{a: {b: 1, c: 0}}");
}
#[test]
fn table_replacement_closure() {
let actual = nu!("[[a]; [text]] | insert b {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
let actual = nu!("[[a]; [text]] | insert b { default TEXT } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
let actual = nu!("[[b]; [1]] | wrap a | insert a.c {|r| $r.a.b } | to nuon");
assert_eq!(actual.out, "[[a]; [{b: 1, c: 1}]]");
let actual = nu!("[[b]; [1]] | wrap a | insert a.c { default 0 } | to nuon");
assert_eq!(actual.out, "[[a]; [{b: 1, c: 0}]]");
}
#[test]
fn list_stream_replacement_closure() {
let actual = nu!("[1, 2] | every 1 | insert 1 {|i| $i + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3, 2]");
let actual = nu!("[1, 2] | every 1 | insert 1 { $in + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3, 2]");
let actual =
nu!("[1, 2] | every 1 | insert 2 {|i| if $i == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
let actual =
nu!("[1, 2] | every 1 | insert 2 { if $in == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
let actual = nu!("[[a]; [text]] | every 1 | insert b {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
let actual = nu!("[[a]; [text]] | every 1 | insert b { default TEXT } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
}

View File

@ -71,11 +71,23 @@ fn update_list() {
} }
#[test] #[test]
fn update_past_end_list() { fn update_past_end_of_list() {
let actual = nu!("[1, 2, 3] | update 5 abc | to json -r"); let actual = nu!("[1, 2, 3] | update 5 abc | to json -r");
assert!(actual.err.contains("too large")); assert!(actual.err.contains("too large"));
} }
#[test]
fn update_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | update 1 abc | to json -r");
assert_eq!(actual.out, r#"[1,"abc",3]"#);
}
#[test]
fn update_past_end_of_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | update 5 abc | to json -r");
assert!(actual.err.contains("too large"));
}
#[test] #[test]
fn update_nonexistent_column() { fn update_nonexistent_column() {
let actual = nu!("{a:1} | update b 2"); let actual = nu!("{a:1} | update b 2");
@ -85,7 +97,7 @@ fn update_nonexistent_column() {
#[test] #[test]
fn update_uses_enumerate_index() { fn update_uses_enumerate_index() {
let actual = nu!( let actual = nu!(
r#"[[a]; [7] [6]] | enumerate | update item.a {|el| $el.index + 1 + $el.item.a } | flatten | to nuon"# "[[a]; [7] [6]] | enumerate | update item.a {|el| $el.index + 1 + $el.item.a } | flatten | to nuon"
); );
assert_eq!(actual.out, "[[index, a]; [0, 8], [1, 8]]"); assert_eq!(actual.out, "[[index, a]; [0, 8], [1, 8]]");
@ -97,3 +109,45 @@ fn update_support_lazy_record() {
nu!(r#"let x = (lazy make -c ["h"] -g {|a| $a | str upcase}); $x | update h 10 | get h"#); nu!(r#"let x = (lazy make -c ["h"] -g {|a| $a | str upcase}); $x | update h 10 | get h"#);
assert_eq!(actual.out, "10"); assert_eq!(actual.out, "10");
} }
#[test]
fn list_replacement_closure() {
let actual = nu!("[1, 2] | update 1 {|i| $i + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual = nu!("[1, 2] | update 1 { $in + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
}
#[test]
fn record_replacement_closure() {
let actual = nu!("{ a: text } | update a {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "{a: TEXT}");
let actual = nu!("{ a: text } | update a { str upcase } | to nuon");
assert_eq!(actual.out, "{a: TEXT}");
}
#[test]
fn table_replacement_closure() {
let actual = nu!("[[a]; [text]] | update a {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
let actual = nu!("[[a]; [text]] | update a { str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
}
#[test]
fn list_stream_replacement_closure() {
let actual = nu!("[1, 2] | every 1 | update 1 {|i| $i + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual = nu!("[1, 2] | every 1 | update 1 { $in + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual = nu!("[[a]; [text]] | every 1 | update a {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
let actual = nu!("[[a]; [text]] | every 1 | update a { str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
}

View File

@ -67,17 +67,49 @@ fn upsert_uses_enumerate_index_updating() {
} }
#[test] #[test]
fn index_does_not_exist() { fn upsert_into_list() {
let actual = nu!("[1,2,3] | upsert 4 4"); let actual = nu!("[1, 2, 3] | upsert 1 abc | to json -r");
assert!(actual.err.contains("index too large (max: 3)")); assert_eq!(actual.out, r#"[1,"abc",3]"#);
} }
#[test] #[test]
fn upsert_empty() { fn upsert_at_end_of_list() {
let actual = nu!("[] | upsert 1 1"); let actual = nu!("[1, 2, 3] | upsert 3 abc | to json -r");
assert!(actual.err.contains("index too large (max: 0)")); assert_eq!(actual.out, r#"[1,2,3,"abc"]"#);
}
#[test]
fn upsert_past_end_of_list() {
let actual = nu!("[1, 2, 3] | upsert 5 abc");
assert!(actual
.err
.contains("can't insert at index (the next available index is 3)"));
}
#[test]
fn upsert_into_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | upsert 1 abc | to json -r");
assert_eq!(actual.out, r#"[1,"abc",3]"#);
}
#[test]
fn upsert_at_end_of_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | upsert 3 abc | to json -r");
assert_eq!(actual.out, r#"[1,2,3,"abc"]"#);
}
#[test]
fn upsert_past_end_of_list_stream() {
let actual = nu!("[1, 2, 3] | every 1 | upsert 5 abc");
assert!(actual
.err
.contains("can't insert at index (the next available index is 3)"));
} }
#[test] #[test]
@ -93,14 +125,100 @@ fn upsert_support_lazy_record() {
#[test] #[test]
fn deep_cell_path_creates_all_nested_records() { fn deep_cell_path_creates_all_nested_records() {
let actual = nu!(r#"{a: {}} | insert a.b.c 0 | get a.b.c"#); let actual = nu!("{a: {}} | upsert a.b.c 0 | get a.b.c");
assert_eq!(actual.out, "0"); assert_eq!(actual.out, "0");
} }
#[test] #[test]
fn upserts_all_rows_in_table_in_record() { fn upserts_all_rows_in_table_in_record() {
let actual = nu!( let actual = nu!(
r#"{table: [[col]; [{a: 1}], [{a: 1}]]} | insert table.col.b 2 | get table.col.b | to nuon"# "{table: [[col]; [{a: 1}], [{a: 1}]]} | upsert table.col.b 2 | get table.col.b | to nuon"
); );
assert_eq!(actual.out, "[2, 2]"); assert_eq!(actual.out, "[2, 2]");
} }
#[test]
fn list_replacement_closure() {
let actual = nu!("[1, 2] | upsert 1 {|i| $i + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual = nu!("[1, 2] | upsert 1 { $in + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual = nu!("[1, 2] | upsert 2 {|i| if $i == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
let actual = nu!("[1, 2] | upsert 2 { if $in == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
}
#[test]
fn record_replacement_closure() {
let actual = nu!("{ a: text } | upsert a {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "{a: TEXT}");
let actual = nu!("{ a: text } | upsert a { str upcase } | to nuon");
assert_eq!(actual.out, "{a: TEXT}");
let actual = nu!("{ a: text } | upsert b {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "{a: text, b: TEXT}");
let actual = nu!("{ a: text } | upsert b { default TEXT } | to nuon");
assert_eq!(actual.out, "{a: text, b: TEXT}");
let actual = nu!("{ a: { b: 1 } } | upsert a.c {|r| $r.a.b } | to nuon");
assert_eq!(actual.out, "{a: {b: 1, c: 1}}");
let actual = nu!("{ a: { b: 1 } } | upsert a.c { default 0 } | to nuon");
assert_eq!(actual.out, "{a: {b: 1, c: 0}}");
}
#[test]
fn table_replacement_closure() {
let actual = nu!("[[a]; [text]] | upsert a {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
let actual = nu!("[[a]; [text]] | upsert a { str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
let actual = nu!("[[a]; [text]] | upsert b {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
let actual = nu!("[[a]; [text]] | upsert b { default TEXT } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
let actual = nu!("[[b]; [1]] | wrap a | upsert a.c {|r| $r.a.b } | to nuon");
assert_eq!(actual.out, "[[a]; [{b: 1, c: 1}]]");
let actual = nu!("[[b]; [1]] | wrap a | upsert a.c { default 0 } | to nuon");
assert_eq!(actual.out, "[[a]; [{b: 1, c: 0}]]");
}
#[test]
fn list_stream_replacement_closure() {
let actual = nu!("[1, 2] | every 1 | upsert 1 {|i| $i + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual = nu!("[1, 2] | every 1 | upsert 1 { $in + 1 } | to nuon");
assert_eq!(actual.out, "[1, 3]");
let actual =
nu!("[1, 2] | every 1 | upsert 2 {|i| if $i == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
let actual =
nu!("[1, 2] | every 1 | upsert 2 { if $in == null { 0 } else { $in + 1 } } | to nuon");
assert_eq!(actual.out, "[1, 2, 0]");
let actual = nu!("[[a]; [text]] | every 1 | upsert a {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
let actual = nu!("[[a]; [text]] | every 1 | upsert a { str upcase } | to nuon");
assert_eq!(actual.out, "[[a]; [TEXT]]");
let actual = nu!("[[a]; [text]] | every 1 | upsert b {|r| $r.a | str upcase } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
let actual = nu!("[[a]; [text]] | every 1 | upsert b { default TEXT } | to nuon");
assert_eq!(actual.out, "[[a, b]; [text, TEXT]]");
}

View File

@ -1143,6 +1143,7 @@ impl Value {
cell_path: &[PathMember], cell_path: &[PathMember],
new_val: Value, new_val: Value,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let v_span = self.span();
if let Some((member, path)) = cell_path.split_first() { if let Some((member, path)) = cell_path.split_first() {
match member { match member {
PathMember::String { PathMember::String {
@ -1215,22 +1216,26 @@ impl Value {
Value::List { vals, .. } => { Value::List { vals, .. } => {
if let Some(v) = vals.get_mut(*row_num) { if let Some(v) = vals.get_mut(*row_num) {
v.upsert_data_at_cell_path(path, new_val)?; v.upsert_data_at_cell_path(path, new_val)?;
} else if vals.len() == *row_num && path.is_empty() { } else if vals.len() != *row_num {
// If the upsert is at 1 + the end of the list, it's OK.
// Otherwise, it's prohibited.
vals.push(new_val);
} else {
return Err(ShellError::InsertAfterNextFreeIndex { return Err(ShellError::InsertAfterNextFreeIndex {
available_idx: vals.len(), available_idx: vals.len(),
span: *span, span: *span,
}); });
} else if !path.is_empty() {
return Err(ShellError::AccessBeyondEnd {
max_idx: vals.len() - 1,
span: *span,
});
} else {
// If the upsert is at 1 + the end of the list, it's OK.
vals.push(new_val);
} }
} }
Value::Error { error, .. } => return Err(*error.clone()), Value::Error { error, .. } => return Err(*error.clone()),
v => { _ => {
return Err(ShellError::NotAList { return Err(ShellError::NotAList {
dst_span: *span, dst_span: *span,
src_span: v.span(), src_span: v_span,
}); });
} }
}, },
@ -1632,22 +1637,30 @@ impl Value {
} => match self { } => match self {
Value::List { vals, .. } => { Value::List { vals, .. } => {
if let Some(v) = vals.get_mut(*row_num) { if let Some(v) = vals.get_mut(*row_num) {
v.insert_data_at_cell_path(path, new_val, head_span)?; if path.is_empty() {
} else if vals.len() == *row_num && path.is_empty() { vals.insert(*row_num, new_val);
// If the insert is at 1 + the end of the list, it's OK. } else {
// Otherwise, it's prohibited. v.insert_data_at_cell_path(path, new_val, head_span)?;
vals.push(new_val); }
} else { } else if vals.len() != *row_num {
return Err(ShellError::InsertAfterNextFreeIndex { return Err(ShellError::InsertAfterNextFreeIndex {
available_idx: vals.len(), available_idx: vals.len(),
span: *span, span: *span,
}); });
} else if !path.is_empty() {
return Err(ShellError::AccessBeyondEnd {
max_idx: vals.len() - 1,
span: *span,
});
} else {
// If the insert is at 1 + the end of the list, it's OK.
vals.push(new_val);
} }
} }
v => { _ => {
return Err(ShellError::NotAList { return Err(ShellError::NotAList {
dst_span: *span, dst_span: *span,
src_span: v.span(), src_span: v_span,
}); });
} }
}, },