diff --git a/crates/nu-command/tests/commands/insert.rs b/crates/nu-command/tests/commands/insert.rs index 3aed2f92c..57ef27cda 100644 --- a/crates/nu-command/tests/commands/insert.rs +++ b/crates/nu-command/tests/commands/insert.rs @@ -95,3 +95,10 @@ fn insert_uses_enumerate_index() { assert_eq!(actual.out, "[[index, a, b]; [0, 7, 8], [1, 6, 8]]"); } + +#[test] +fn insert_support_lazy_record() { + let actual = + nu!(r#"let x = (lazy make -c ["h"] -g {|a| $a | str upcase}); $x | insert a 10 | get a"#); + assert_eq!(actual.out, "10"); +} diff --git a/crates/nu-command/tests/commands/update.rs b/crates/nu-command/tests/commands/update.rs index e8783313b..866897c51 100644 --- a/crates/nu-command/tests/commands/update.rs +++ b/crates/nu-command/tests/commands/update.rs @@ -105,3 +105,10 @@ fn update_uses_enumerate_index() { assert_eq!(actual.out, "[[index, a]; [0, 8], [1, 8]]"); } + +#[test] +fn update_support_lazy_record() { + let actual = + nu!(r#"let x = (lazy make -c ["h"] -g {|a| $a | str upcase}); $x | update h 10 | get h"#); + assert_eq!(actual.out, "10"); +} diff --git a/crates/nu-command/tests/commands/upsert.rs b/crates/nu-command/tests/commands/upsert.rs index f6319bb65..e757fe69c 100644 --- a/crates/nu-command/tests/commands/upsert.rs +++ b/crates/nu-command/tests/commands/upsert.rs @@ -95,3 +95,14 @@ fn upsert_empty() { assert!(actual.err.contains("index too large (max: 0)")); } + +#[test] +fn upsert_support_lazy_record() { + let actual = + nu!(r#"let x = (lazy make -c ["h"] -g {|a| $a | str upcase}); $x | upsert h 10 | get h"#); + assert_eq!(actual.out, "10"); + + let actual = + nu!(r#"let x = (lazy make -c ["h"] -g {|a| $a | str upcase}); $x | upsert aa 10 | get aa"#); + assert_eq!(actual.out, "10"); +} diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index c50846d98..7b2d3b880 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -1050,6 +1050,12 @@ impl Value { } } } + Value::LazyRecord { val, .. } => { + // convert to Record first. + let mut record = val.collect()?; + record.upsert_data_at_cell_path(cell_path, new_val)?; + *self = record + } Value::Error { error } => return Err(*error.to_owned()), v => { return Err(ShellError::CantFindColumn { @@ -1181,6 +1187,12 @@ impl Value { }); } } + Value::LazyRecord { val, .. } => { + // convert to Record first. + let mut record = val.collect()?; + record.update_data_at_cell_path(cell_path, new_val)?; + *self = record + } Value::Error { error } => return Err(*error.to_owned()), v => { return Err(ShellError::CantFindColumn { @@ -1293,6 +1305,13 @@ impl Value { } Ok(()) } + Value::LazyRecord { val, .. } => { + // convert to Record first. + let mut record = val.collect()?; + record.remove_data_at_cell_path(cell_path)?; + *self = record; + Ok(()) + } v => Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, @@ -1391,6 +1410,13 @@ impl Value { } Ok(()) } + Value::LazyRecord { val, .. } => { + // convert to Record first. + let mut record = val.collect()?; + record.remove_data_at_cell_path(cell_path)?; + *self = record; + Ok(()) + } v => Err(ShellError::CantFindColumn { col_name: col_name.to_string(), span: *span, @@ -1507,6 +1533,12 @@ impl Value { cols.push(col_name.clone()); vals.push(new_val); } + Value::LazyRecord { val, span } => { + // convert to Record first. + let mut record = val.collect()?; + record.insert_data_at_cell_path(cell_path, new_val, *span)?; + *self = record + } other => { return Err(ShellError::UnsupportedInput( "table or record".into(),