New array_pop command

This commit is contained in:
sagie gur ari 2020-01-22 22:06:40 +00:00
parent 09fe913f22
commit 883994f253
7 changed files with 220 additions and 0 deletions

View file

@ -2,6 +2,7 @@
### v0.1.8
* New array_pop command
* Commands created from duckscript now support help text and automatic scope clearing #69
* New clear_scope command #71
* New set_error command #68

View file

@ -17,6 +17,7 @@
* [std::collections::Array (array)](#std__collections__Array)
* [std::collections::ArrayIsEmpty (array_is_empty)](#std__collections__ArrayIsEmpty)
* [std::collections::ArrayLength (array_length, arrlen)](#std__collections__ArrayLength)
* [std::collections::ArrayPop (array_pop)](#std__collections__ArrayPop)
* [std::collections::IsArray (is_array)](#std__collections__IsArray)
* [std::collections::Range (range)](#std__collections__Range)
* [std::collections::ReadProperties (read_properties)](#std__collections__ReadProperties)
@ -802,6 +803,34 @@ echo Array length: ${len} released: ${released}
#### Aliases:
array_length, arrlen
<a name="std__collections__ArrayPop"></a>
## std::collections::ArrayPop
```sh
var = array_pop handle
```
Returns the last element of the array or none if the array is empty.
#### Parameters
The array handle.
#### Return Value
The last element of the array or none if the array is empty.
#### Examples
```sh
handle = array 1 2 3
last_element = array_pop ${handle}
assert_eq ${last_element} 3
```
#### Aliases:
array_pop
<a name="std__collections__IsArray"></a>
## std::collections::IsArray
```sh

View file

@ -0,0 +1,21 @@
```sh
var = array_pop handle
```
Returns the last element of the array or none if the array is empty.
#### Parameters
The array handle.
#### Return Value
The last element of the array or none if the array is empty.
#### Examples
```sh
handle = array 1 2 3
last_element = array_pop ${handle}
assert_eq ${last_element} 3
```

View file

@ -0,0 +1,105 @@
use crate::utils::pckg;
use crate::utils::state::get_handles_sub_state;
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;
struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "ArrayPop")
}
fn aliases(&self) -> Vec<String> {
vec!["array_pop".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
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.is_empty() {
CommandResult::Error("Array handle not provided.".to_string())
} else {
let state = get_handles_sub_state(state);
let key = &arguments[0];
match state.remove(key) {
Some(state_value) => match state_value {
StateValue::List(mut list) => {
let list_item = list.pop();
state.insert(key.to_string(), StateValue::List(list));
match list_item {
Some(list_item_value) => match list_item_value {
StateValue::Boolean(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::Number(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::UnsignedNumber(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::Number32Bit(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::UnsignedNumber32Bit(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::Number64Bit(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::UnsignedNumber64Bit(value) => {
CommandResult::Continue(Some(value.to_string()))
}
StateValue::String(value) => CommandResult::Continue(Some(value)),
StateValue::List(_) => {
CommandResult::Error("Unsupported array element.".to_string())
}
StateValue::SubState(_) => {
CommandResult::Error("Unsupported array element.".to_string())
}
},
None => CommandResult::Continue(None),
}
}
_ => CommandResult::Error("Invalid handle provided.".to_string()),
},
None => CommandResult::Error(
format!("Array for handle: {} not found.", key).to_string(),
),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,31 @@
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_pop", "out");
}
#[test]
fn run_not_found() {
test::run_script_and_error(vec![create("")], "out = array_pop bad_handle", "out");
}
#[test]
fn run_found() {
test::run_script_and_validate(
vec![create(""), array::create("")],
r#"
handle = array a b c "d e"
out = array_pop ${handle}
"#,
CommandValidation::Match("out".to_string(), "d e".to_string()),
);
}

View file

@ -1,6 +1,7 @@
pub(crate) mod array;
mod array_is_empty;
pub(crate) mod array_length;
mod array_pop;
mod is_array;
mod range;
mod read_properties;
@ -18,6 +19,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(array::create(&package))?;
commands.set(array_is_empty::create(&package))?;
commands.set(array_length::create(&package))?;
commands.set(array_pop::create(&package))?;
commands.set(is_array::create(&package))?;
commands.set(range::create(&package))?;
commands.set(read_properties::create(&package))?;

View file

@ -0,0 +1,31 @@
function test_array_with_data
arr = array 1 2 3
last_element = array_pop ${arr}
assert_eq ${last_element} 3
last_element = array_pop ${arr}
assert_eq ${last_element} 2
last_element = array_pop ${arr}
assert_eq ${last_element} 1
last_element = array_pop ${arr}
defined = is_defined last_element
assert_false ${defined}
released = release ${arr}
assert ${released}
end
function test_array_no_data
arr = array
last_element = array_pop ${arr}
defined = is_defined last_element
assert_false ${defined}
released = release ${arr}
assert ${released}
end