Add insert/update to lists (#4873)

This commit is contained in:
JT 2022-03-19 10:12:54 +13:00 committed by GitHub
parent 983d115bc0
commit b293282e9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 8 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}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoPipelineData, PipelineData, ShellError, Signature, Span, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
SyntaxShape, Value, ShellError, Signature, Span, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -121,6 +121,24 @@ fn insert(
ctrlc, ctrlc,
) )
} else { } else {
if let Some(PathMember::Int { val, .. }) = cell_path.members.get(0) {
let mut input = input.into_iter();
let mut pre_elems = vec![];
for _ in 0..*val {
if let Some(v) = input.next() {
pre_elems.push(v);
} else {
pre_elems.push(Value::Nothing { span })
}
}
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data(ctrlc));
}
input.map( input.map(
move |mut input| { move |mut input| {
let replacement = replacement.clone(); let replacement = replacement.clone();

View File

@ -1,9 +1,9 @@
use nu_engine::{eval_block, CallExt}; use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::{Call, CellPath}; use nu_protocol::ast::{Call, CellPath, PathMember};
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{ use nu_protocol::{
Category, Example, FromValue, IntoPipelineData, PipelineData, ShellError, Signature, Span, Category, Example, FromValue, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
SyntaxShape, Value, ShellError, Signature, Span, SyntaxShape, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -131,6 +131,27 @@ fn upsert(
ctrlc, ctrlc,
) )
} else { } else {
if let Some(PathMember::Int { val, span }) = cell_path.members.get(0) {
let mut input = input.into_iter();
let mut pre_elems = vec![];
for idx in 0..*val {
if let Some(v) = input.next() {
pre_elems.push(v);
} else {
return Err(ShellError::AccessBeyondEnd(idx - 1, *span));
}
}
// Skip over the replaced value
let _ = input.next();
return Ok(pre_elems
.into_iter()
.chain(vec![replacement])
.chain(input)
.into_pipeline_data(ctrlc));
}
input.map( input.map(
move |mut input| { move |mut input| {
let replacement = replacement.clone(); let replacement = replacement.clone();

View File

@ -26,3 +26,51 @@ fn insert_the_column_conflict() {
assert!(actual.err.contains("column already exists")); assert!(actual.err.contains("column already exists"));
} }
#[test]
fn insert_into_list() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3] | insert 1 abc | to json -r
"#
));
assert_eq!(actual.out, r#"[1,"abc",2,3]"#);
}
#[test]
fn insert_into_list_begin() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3] | insert 0 abc | to json -r
"#
));
assert_eq!(actual.out, r#"["abc",1,2,3]"#);
}
#[test]
fn insert_into_list_end() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3] | insert 3 abc | to json -r
"#
));
assert_eq!(actual.out, r#"[1,2,3,"abc"]"#);
}
#[test]
fn insert_past_end_list() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3] | insert 5 abc | to json -r
"#
));
assert_eq!(actual.out, r#"[1,2,3,null,null,"abc"]"#);
}

View File

@ -71,3 +71,27 @@ fn upsert_column_missing() {
assert!(actual.err.contains("cannot find column")); assert!(actual.err.contains("cannot find column"));
} }
#[test]
fn update_list() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3] | update 1 abc | to json -r
"#
));
assert_eq!(actual.out, r#"[1,"abc",3]"#);
}
#[test]
fn update_past_end_list() {
let actual = nu!(
cwd: ".", pipeline(
r#"
[1, 2, 3] | update 5 abc | to json -r
"#
));
assert!(actual.err.contains("too large"));
}

View File

@ -133,11 +133,11 @@ Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVE
#[error("Row number too large (max: {0}).")] #[error("Row number too large (max: {0}).")]
#[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))] #[diagnostic(code(nu::shell::access_beyond_end), url(docsrs))]
AccessBeyondEnd(usize, #[label = "too large"] Span), AccessBeyondEnd(usize, #[label = "index too large (max: {0})"] Span),
#[error("Row number too large.")] #[error("Row number too large.")]
#[diagnostic(code(nu::shell::access_beyond_end_of_stream), url(docsrs))] #[diagnostic(code(nu::shell::access_beyond_end_of_stream), url(docsrs))]
AccessBeyondEndOfStream(#[label = "too large"] Span), AccessBeyondEndOfStream(#[label = "index too large"] Span),
#[error("Data cannot be accessed with a cell path")] #[error("Data cannot be accessed with a cell path")]
#[diagnostic(code(nu::shell::incompatible_path_access), url(docsrs))] #[diagnostic(code(nu::shell::incompatible_path_access), url(docsrs))]