New array_remove command

This commit is contained in:
sagie gur ari 2020-05-05 17:39:54 +00:00
parent 1e35a39c23
commit f090237e9d
7 changed files with 234 additions and 2 deletions

View file

@ -2,6 +2,7 @@
### v0.3.4
* New array_remove command.
* New set_new command.
* New is_set command.
* New unset command.

View file

@ -16,6 +16,7 @@
* [std::collections::ArrayLength (array_length, arrlen)](#std__collections__ArrayLength)
* [std::collections::ArrayPop (array_pop)](#std__collections__ArrayPop)
* [std::collections::ArrayPush (array_push, array_add, array_put)](#std__collections__ArrayPush)
* [std::collections::ArrayRemove (array_remove)](#std__collections__ArrayRemove)
* [std::collections::ArraySet (array_set)](#std__collections__ArraySet)
* [std::collections::IsArray (is_array)](#std__collections__IsArray)
* [std::collections::IsMap (is_map)](#std__collections__IsMap)
@ -815,6 +816,44 @@ assert_eq ${last_element} 4
#### Aliases:
array_push, array_add, array_put
<a name="std__collections__ArrayRemove"></a>
## std::collections::ArrayRemove
```sh
result = array_remove handle index
```
Removes the item from the array at the given index.<br>
If the array is not found or the index is greater than the array size, this command will return false.<br>
Otherwise it will return true.
#### Parameters
* The array handle.
* The element index.
#### Return Value
True if successful.
#### Examples
```sh
arr = array old
element = array_get ${arr} 0
assert_eq ${element} old
result = array_remove ${arr} 0
assert ${result}
empty = array_is_empty ${arr}
assert ${empty}
```
#### Aliases:
array_remove
<a name="std__collections__ArraySet"></a>
## std::collections::ArraySet
```sh

View file

@ -0,0 +1,31 @@
```sh
result = array_remove handle index
```
Removes the item from the array at the given index.<br>
If the array is not found or the index is greater than the array size, this command will return false.<br>
Otherwise it will return true.
#### Parameters
* The array handle.
* The element index.
#### Return Value
True if successful.
#### Examples
```sh
arr = array old
element = array_get ${arr} 0
assert_eq ${element} old
result = array_remove ${arr} 0
assert ${result}
empty = array_is_empty ${arr}
assert ${empty}
```

View file

@ -0,0 +1,90 @@
use crate::utils::pckg;
use crate::utils::state::{get_handles_sub_state, mutate_list};
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use std::collections::HashMap;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "ArrayRemove")
}
fn aliases(&self) -> Vec<String> {
vec!["array_remove".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn requires_context(&self) -> bool {
true
}
fn run_with_context(
&self,
arguments: Vec<String>,
state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Invalid input provided.".to_string())
} else {
let state = get_handles_sub_state(state);
let key = arguments[0].clone();
let index: usize = match arguments[1].parse() {
Ok(value) => value,
Err(_) => {
return CommandResult::Error(
format!("Non numeric value: {} provided.", &arguments[1]).to_string(),
);
}
};
let result = mutate_list(key, state, |list| {
let list_length = list.len();
if list_length > index {
list.remove(index);
Ok(Some("true".to_string()))
} else {
Err(format!(
"Index: {} is greater than list size: {}",
index, list_length
))
}
});
match result {
Ok(value) => CommandResult::Continue(value),
Err(error) => CommandResult::Error(error),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,48 @@
use super::*;
use crate::sdk::std::collections::array;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = array_remove", "out");
}
#[test]
fn run_only_handle() {
test::run_script_and_error(vec![create("")], "out = array_remove handle", "out");
}
#[test]
fn run_not_found() {
test::run_script_and_error(vec![create("")], "out = array_remove bad_handle 2", "out");
}
#[test]
fn run_found() {
test::run_script_and_validate(
vec![create(""), array::create("")],
r#"
handle = array a b c "d e"
out = array_remove ${handle} 3
"#,
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_found_out_of_bounds() {
test::run_script_and_error(
vec![create(""), array::create("")],
r#"
handle = array a b c "d e"
out = array_remove ${handle} 20
"#,
"out",
);
}

View file

@ -8,6 +8,7 @@ mod array_join;
pub(crate) mod array_length;
pub(crate) mod array_pop;
mod array_push;
mod array_remove;
mod array_set;
mod is_array;
mod is_map;
@ -43,12 +44,13 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(array_concat::create(&package)?)?;
commands.set(array_contains::create(&package)?)?;
commands.set(array_get::create(&package))?;
commands.set(array_push::create(&package))?;
commands.set(array_set::create(&package))?;
commands.set(array_is_empty::create(&package)?)?;
commands.set(array_join::create(&package)?)?;
commands.set(array_length::create(&package))?;
commands.set(array_pop::create(&package))?;
commands.set(array_push::create(&package))?;
commands.set(array_remove::create(&package))?;
commands.set(array_set::create(&package))?;
commands.set(is_array::create(&package))?;
commands.set(is_map::create(&package))?;
commands.set(is_set::create(&package))?;

View file

@ -0,0 +1,21 @@
fn test_array_with_data
arr = array 1 2 3
element = array_get ${arr} 0
assert_eq ${element} 1
result = array_remove ${arr} 0
assert ${result}
element = array_get ${arr} 0
assert_eq ${element} 2
result = array_remove ${arr} 1
assert ${result}
result = array_remove ${arr} 0
assert ${result}
empty = array_is_empty ${arr}
assert ${empty}
end