mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-06 08:02:06 +00:00
New map_remove command
This commit is contained in:
parent
effb4ff498
commit
3f6d509bcb
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.9
|
||||
|
||||
* New map_remove command.
|
||||
* New is_map command.
|
||||
* New map_get command.
|
||||
* New map_put command.
|
||||
|
|
36
docs/sdk.md
36
docs/sdk.md
|
@ -25,6 +25,7 @@
|
|||
* [std::collections::Map (map)](#std__collections__Map)
|
||||
* [std::collections::MapGet (map_get)](#std__collections__MapGet)
|
||||
* [std::collections::MapPut (map_put)](#std__collections__MapPut)
|
||||
* [std::collections::MapRemove (map_remove)](#std__collections__MapRemove)
|
||||
* [std::collections::Range (range)](#std__collections__Range)
|
||||
* [std::collections::ReadProperties (read_properties)](#std__collections__ReadProperties)
|
||||
* [std::collections::WriteProperties (write_properties)](#std__collections__WriteProperties)
|
||||
|
@ -1172,6 +1173,41 @@ release ${handle}
|
|||
#### Aliases:
|
||||
map_put
|
||||
|
||||
<a name="std__collections__MapRemove"></a>
|
||||
## std::collections::MapRemove
|
||||
```sh
|
||||
value = map_remove handle key
|
||||
```
|
||||
|
||||
Removes a the value corresponding to the key from the map and returns it.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The map handle.
|
||||
* The key.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The value corresponding to the key from the map.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = map
|
||||
|
||||
result = map_put ${handle} key value
|
||||
assert_eq ${result} true
|
||||
|
||||
value = map_remove ${handle} key
|
||||
assert_eq ${value} value
|
||||
|
||||
release ${handle}
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
map_remove
|
||||
|
||||
<a name="std__collections__Range"></a>
|
||||
## std::collections::Range
|
||||
```sh
|
||||
|
|
28
duckscript_sdk/src/sdk/std/collections/map_remove/help.md
Normal file
28
duckscript_sdk/src/sdk/std/collections/map_remove/help.md
Normal file
|
@ -0,0 +1,28 @@
|
|||
```sh
|
||||
value = map_remove handle key
|
||||
```
|
||||
|
||||
Removes a the value corresponding to the key from the map and returns it.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The map handle.
|
||||
* The key.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The value corresponding to the key from the map.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = map
|
||||
|
||||
result = map_put ${handle} key value
|
||||
assert_eq ${result} true
|
||||
|
||||
value = map_remove ${handle} key
|
||||
assert_eq ${value} value
|
||||
|
||||
release ${handle}
|
||||
```
|
156
duckscript_sdk/src/sdk/std/collections/map_remove/mod.rs
Executable file
156
duckscript_sdk/src/sdk/std/collections/map_remove/mod.rs
Executable file
|
@ -0,0 +1,156 @@
|
|||
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;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "MapRemove")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["map_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.is_empty() {
|
||||
CommandResult::Error("Map handle not provided.".to_string())
|
||||
} else if arguments.len() < 2 {
|
||||
CommandResult::Error("Key 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::SubState(mut map) => {
|
||||
let item = map.remove(&arguments[1]);
|
||||
|
||||
state.insert(key.to_string(), StateValue::SubState(map));
|
||||
|
||||
match item {
|
||||
Some(map_item_value) => match map_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.to_string()))
|
||||
}
|
||||
StateValue::ByteArray(_) => {
|
||||
CommandResult::Error("Unsupported map element.".to_string())
|
||||
}
|
||||
StateValue::List(_) => {
|
||||
CommandResult::Error("Unsupported map element.".to_string())
|
||||
}
|
||||
StateValue::SubState(_) => {
|
||||
CommandResult::Error("Unsupported map element.".to_string())
|
||||
}
|
||||
},
|
||||
None => CommandResult::Continue(None),
|
||||
}
|
||||
}
|
||||
StateValue::Boolean(value) => {
|
||||
state.insert(key.to_string(), StateValue::Boolean(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::Number(value) => {
|
||||
state.insert(key.to_string(), StateValue::Number(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::UnsignedNumber(value) => {
|
||||
state.insert(key.to_string(), StateValue::UnsignedNumber(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::Number32Bit(value) => {
|
||||
state.insert(key.to_string(), StateValue::Number32Bit(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::UnsignedNumber32Bit(value) => {
|
||||
state.insert(key.to_string(), StateValue::UnsignedNumber32Bit(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::Number64Bit(value) => {
|
||||
state.insert(key.to_string(), StateValue::Number64Bit(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::UnsignedNumber64Bit(value) => {
|
||||
state.insert(key.to_string(), StateValue::UnsignedNumber64Bit(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::String(value) => {
|
||||
state.insert(key.to_string(), StateValue::String(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::ByteArray(value) => {
|
||||
state.insert(key.to_string(), StateValue::ByteArray(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
StateValue::List(value) => {
|
||||
state.insert(key.to_string(), StateValue::List(value));
|
||||
CommandResult::Error("Invalid handle provided.".to_string())
|
||||
}
|
||||
},
|
||||
None => {
|
||||
CommandResult::Error(format!("Map for handle: {} not found.", key).to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
use super::*;
|
||||
use crate::sdk::std::collections::{map, map_put};
|
||||
use crate::sdk::std::set;
|
||||
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 = map_remove", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_missing_key() {
|
||||
test::run_script_and_error(vec![create("")], "out = map_remove handle", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_not_found() {
|
||||
test::run_script_and_error(vec![create("")], "out = map_remove bad_handle key", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_found() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), map::create(""), map_put::create("")],
|
||||
r#"
|
||||
handle = map
|
||||
map_put ${handle} key value
|
||||
out = map_remove ${handle} key
|
||||
"#,
|
||||
CommandValidation::Match("out".to_string(), "value".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_twice() {
|
||||
test::run_script_and_validate(
|
||||
vec![
|
||||
create(""),
|
||||
map::create(""),
|
||||
map_put::create(""),
|
||||
set::create(""),
|
||||
],
|
||||
r#"
|
||||
handle = map
|
||||
map_put ${handle} key value
|
||||
out = map_remove ${handle} key
|
||||
out = map_remove ${handle} key
|
||||
handle = set
|
||||
"#,
|
||||
CommandValidation::None,
|
||||
);
|
||||
}
|
|
@ -10,6 +10,7 @@ mod is_map;
|
|||
mod map;
|
||||
mod map_get;
|
||||
mod map_put;
|
||||
mod map_remove;
|
||||
mod range;
|
||||
mod read_properties;
|
||||
mod write_properties;
|
||||
|
@ -35,6 +36,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(map::create(&package))?;
|
||||
commands.set(map_get::create(&package))?;
|
||||
commands.set(map_put::create(&package))?;
|
||||
commands.set(map_remove::create(&package))?;
|
||||
commands.set(range::create(&package))?;
|
||||
commands.set(read_properties::create(&package))?;
|
||||
commands.set(write_properties::create(&package))?;
|
||||
|
|
|
@ -16,7 +16,7 @@ mod process;
|
|||
mod read;
|
||||
pub(crate) mod release;
|
||||
pub(crate) mod scope;
|
||||
mod set;
|
||||
pub(crate) mod set;
|
||||
pub(crate) mod string;
|
||||
mod test;
|
||||
mod thread;
|
||||
|
|
27
test/std/collections/map_remove_test.ds
Normal file
27
test/std/collections/map_remove_test.ds
Normal file
|
@ -0,0 +1,27 @@
|
|||
|
||||
fn test_remove
|
||||
handle = map
|
||||
|
||||
result = map_put ${handle} key value
|
||||
assert_eq ${result} true
|
||||
|
||||
value = map_remove ${handle} key
|
||||
assert_eq ${value} value
|
||||
|
||||
release ${handle}
|
||||
end
|
||||
|
||||
fn test_remove_twice
|
||||
handle = map
|
||||
|
||||
result = map_put ${handle} key value
|
||||
assert_eq ${result} true
|
||||
|
||||
value = map_remove ${handle} key
|
||||
assert_eq ${value} value
|
||||
value = map_remove ${handle} key
|
||||
defined = is_defined value
|
||||
assert_false ${defined}
|
||||
|
||||
release ${handle}
|
||||
end
|
Loading…
Reference in a new issue