Fix release with recursive flag to support set data structures as well

This commit is contained in:
sagie gur ari 2021-06-26 10:57:58 +00:00
parent 8879af3f05
commit fda0966424
8 changed files with 99 additions and 59 deletions

View file

@ -1,5 +1,9 @@
## CHANGELOG
### v0.8.3
* Fix release with recursive flag to support set data structures as well.
### v0.8.2 (2021-06-04)
* Upgrade dependencies #179

32
Cargo.lock generated
View file

@ -335,9 +335,9 @@ checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "globset"
version = "0.4.6"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c152169ef1e421390738366d2f796655fec62621dabbd0fd476f905934061e4a"
checksum = "f0fc1b9fa0e64ffb1aa5b95daa0f0f167734fd528b7c02eabc581d9d843649b1"
dependencies = [
"aho-corasick 0.7.18",
"bstr",
@ -388,9 +388,9 @@ dependencies = [
[[package]]
name = "ignore"
version = "0.4.17"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b287fb45c60bb826a0dc68ff08742b9d88a2fea13d6e0c286b3172065aaf878c"
checksum = "713f1b139373f96a2e0ce3ac931cd01ee973c3c5dd7c40c0c2efe96ad2b6751d"
dependencies = [
"crossbeam-utils",
"globset",
@ -454,9 +454,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.95"
version = "0.2.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36"
checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
[[package]]
name = "log"
@ -585,9 +585,9 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.7.2"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
[[package]]
name = "openssl"
@ -672,9 +672,9 @@ dependencies = [
[[package]]
name = "rand_chacha"
version = "0.3.0"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
@ -779,9 +779,9 @@ dependencies = [
[[package]]
name = "security-framework"
version = "2.2.0"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3670b1d2fdf6084d192bc71ead7aabe6c06aa2ea3fbd9cc3ac111fa5c2b1bd84"
checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467"
dependencies = [
"bitflags",
"core-foundation",
@ -792,9 +792,9 @@ dependencies = [
[[package]]
name = "security-framework-sys"
version = "2.2.0"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3676258fd3cfe2c9a0ec99ce3038798d847ce3e4bb17746373eb9f0f1ac16339"
checksum = "7e4effb91b4b8b6fb7732e670b6cee160278ff8e6bf485c7805d9e319d76e284"
dependencies = [
"core-foundation-sys",
"libc",
@ -825,9 +825,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.72"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82"
checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7"
dependencies = [
"proc-macro2",
"quote",

View file

@ -172,6 +172,7 @@
* [std::time::CurrentTimeMillies (current_time)](#std__time__CurrentTimeMillies)
* [std::var::GetAllVarNames (get_all_var_names)](#std__var__GetAllVarNames)
* [std::var::GetByName (get_by_name)](#std__var__GetByName)
* [std::var::IsDefined (is_defined)](#std__var__IsDefined)
* [std::var::Set (set)](#std__var__Set)
* [std::var::SetByName (set_by_name)](#std__var__SetByName)
* [std::var::Unset (unset)](#std__var__Unset)
@ -410,8 +411,8 @@ release [-r|--recursive] handle
Releases an internal handle stored in the runtime memory.<br>
Certain commands (such as **array**) will create a handle and the variable will only hold a reference to that handle.<br>
In order to release those handles once they are no longer needed, the release command should be used.<br>
By providing the recursive flag, it will also go over the data values (array items, map values, ...) and release each one of them as well
if they are handles to other arrays/maps/...
By providing the recursive flag, it will also go over the data values (array items, map values, set keys, ...) and release each one of them as well
if they are handles to other arrays/maps/sets/...
#### Parameters
@ -6338,6 +6339,33 @@ assert_eq ${value} test
#### Aliases:
get_by_name
<a name="std__var__IsDefined"></a>
## std::var::IsDefined
```sh
var = is_defined key
```
Returns true if the provided variable name (not value) exists.
#### Parameters
The variable name.
#### Return Value
True if the variable is defined.
#### Examples
```sh
key = set "hello world"
exists = is_defined key
```
#### Aliases:
is_defined
<a name="std__var__Set"></a>
## std::var::Set
```sh

View file

@ -5,8 +5,8 @@ release [-r|--recursive] handle
Releases an internal handle stored in the runtime memory.<br>
Certain commands (such as **array**) will create a handle and the variable will only hold a reference to that handle.<br>
In order to release those handles once they are no longer needed, the release command should be used.<br>
By providing the recursive flag, it will also go over the data values (array items, map values, ...) and release each one of them as well
if they are handles to other arrays/maps/...
By providing the recursive flag, it will also go over the data values (array items, map values, set keys, ...) and release each one of them as well
if they are handles to other arrays/maps/sets/...
#### Parameters

View file

@ -1,5 +1,5 @@
use crate::utils::pckg;
use crate::utils::state::remove_handle;
use crate::utils::state::{remove_handle, remove_handle_recursive};
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
@ -9,41 +9,6 @@ use std::collections::HashMap;
#[path = "./mod_test.rs"]
mod mod_test;
fn remove(state: &mut HashMap<String, StateValue>, key: &str, recursive: bool) -> bool {
match remove_handle(state, key.to_string()) {
Some(state_value) => {
if recursive {
match state_value {
StateValue::SubState(map) => {
for (_, map_value) in map {
match map_value {
StateValue::String(value) => remove(state, &value, recursive),
_ => true,
};
}
true
}
StateValue::List(list) => {
for value in list {
match value {
StateValue::String(value) => remove(state, &value, recursive),
_ => true,
};
}
true
}
_ => true,
}
} else {
true
}
}
None => false,
}
}
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
@ -90,9 +55,13 @@ impl Command for CommandImpl {
(arguments[0].to_string(), false)
};
let removed = remove(state, &key, recursive);
if recursive {
remove_handle_recursive(state, key);
} else {
remove_handle(state, key);
}
CommandResult::Continue(Some(removed.to_string()))
CommandResult::Continue(Some(true.to_string()))
}
}
}

View file

@ -17,7 +17,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(get_all_var_names::create(&package))?;
commands.set(get_by_name::create(&package))?;
commands.set(is_defined::create(PACKAGE))?;
commands.set(is_defined::create(&package))?;
commands.set(set::create(&package))?;
commands.set(set_by_name::create(&package))?;
commands.set(unset::create(&package)?)?;

View file

@ -68,6 +68,33 @@ pub(crate) fn remove_handle(
handle_state.remove(&key)
}
pub(crate) fn remove_handle_recursive(state: &mut HashMap<String, StateValue>, key: String) {
if let Some(state_value) = remove_handle(state, key.to_string()) {
match state_value {
StateValue::List(list) => {
for value in list {
if let StateValue::String(value) = value {
remove_handle_recursive(state, value)
};
}
}
StateValue::Set(set) => {
for value in set {
remove_handle_recursive(state, value)
}
}
StateValue::SubState(map) => {
for (_, map_value) in map {
if let StateValue::String(value) = map_value {
remove_handle_recursive(state, value)
};
}
}
_ => (),
}
}
}
pub(crate) fn return_handle(
state: &mut HashMap<String, StateValue>,
key: String,

View file

@ -20,6 +20,10 @@ fn test_release_recursive
sub_array_handle2 = array
array_handle = array ${sub_array_handle2}
sub_set_handle = set_new
set_handle = set_new ${sub_set_handle}
array_push ${array_handle} ${set_handle}
handle = array ${array_handle} ${map_handle}
exists = is_array ${handle}
@ -32,6 +36,10 @@ fn test_release_recursive
assert ${exists}
exists = is_array ${sub_array_handle2}
assert ${exists}
exists = is_set ${set_handle}
assert ${exists}
exists = is_set ${sub_set_handle}
assert ${exists}
release -r ${handle}
@ -45,4 +53,8 @@ fn test_release_recursive
assert_false ${exists}
exists = is_array ${sub_array_handle2}
assert_false ${exists}
exists = is_set ${set_handle}
assert_false ${exists}
exists = is_set ${sub_set_handle}
assert_false ${exists}
end