mirror of
https://github.com/sagiegurari/duckscript
synced 2024-09-06 00:34:07 +00:00
New unset command
This commit is contained in:
parent
4bd6bf1ec7
commit
a5457e636a
|
@ -3,6 +3,7 @@
|
||||||
### v0.3.4
|
### v0.3.4
|
||||||
|
|
||||||
* \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands #103
|
* \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands #103
|
||||||
|
* New unset command.
|
||||||
* New array_contains command.
|
* New array_contains command.
|
||||||
* New map_contains_value command.
|
* New map_contains_value command.
|
||||||
* New map_contains_key command.
|
* New map_contains_key command.
|
||||||
|
|
48
docs/sdk.md
48
docs/sdk.md
|
@ -137,6 +137,7 @@
|
||||||
* [std::var::GetByName (get_by_name)](#std__var__GetByName)
|
* [std::var::GetByName (get_by_name)](#std__var__GetByName)
|
||||||
* [std::var::Set (set)](#std__var__Set)
|
* [std::var::Set (set)](#std__var__Set)
|
||||||
* [std::var::SetByName (set_by_name)](#std__var__SetByName)
|
* [std::var::SetByName (set_by_name)](#std__var__SetByName)
|
||||||
|
* [std::var::Unset (unset)](#std__var__Unset)
|
||||||
|
|
||||||
|
|
||||||
<a name="std__Echo"></a>
|
<a name="std__Echo"></a>
|
||||||
|
@ -5019,7 +5020,7 @@ set
|
||||||
<a name="std__var__SetByName"></a>
|
<a name="std__var__SetByName"></a>
|
||||||
## std::var::SetByName
|
## std::var::SetByName
|
||||||
```sh
|
```sh
|
||||||
var = set_by_name name value
|
var = set_by_name name [value]
|
||||||
```
|
```
|
||||||
|
|
||||||
This command sets the variable value based on the variable name.<br>
|
This command sets the variable value based on the variable name.<br>
|
||||||
|
@ -5032,7 +5033,7 @@ However, it allows for a dynamic variable name.
|
||||||
#### Parameters
|
#### Parameters
|
||||||
|
|
||||||
* The variable name.
|
* The variable name.
|
||||||
* The new variable value.
|
* The new variable value, if not provided, the variable will be unset.
|
||||||
|
|
||||||
#### Return Value
|
#### Return Value
|
||||||
|
|
||||||
|
@ -5053,6 +5054,49 @@ assert_eq ${value} test
|
||||||
#### Aliases:
|
#### Aliases:
|
||||||
set_by_name
|
set_by_name
|
||||||
|
|
||||||
|
<a name="std__var__Unset"></a>
|
||||||
|
## std::var::Unset
|
||||||
|
|
||||||
|
```sh
|
||||||
|
unset [names]*
|
||||||
|
```
|
||||||
|
|
||||||
|
Undefines all the variable names provided.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
A list of variable names to undefine.
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
var = set 1
|
||||||
|
defined = is_defined var
|
||||||
|
assert ${defined}
|
||||||
|
unset var
|
||||||
|
defined = is_defined var
|
||||||
|
assert_false ${defined}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Source:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
|
||||||
|
for scope::unset::name in ${scope::unset::arguments}
|
||||||
|
set_by_name ${scope::unset::name}
|
||||||
|
end
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Aliases:
|
||||||
|
unset
|
||||||
|
|
||||||
### License
|
### License
|
||||||
Developed by Sagie Gur-Ari and licensed under the
|
Developed by Sagie Gur-Ari and licensed under the
|
||||||
[Apache 2](https://github.com/sagiegurari/duckscript/blob/master/LICENSE) open source license.
|
[Apache 2](https://github.com/sagiegurari/duckscript/blob/master/LICENSE) open source license.
|
||||||
|
|
|
@ -2,6 +2,7 @@ mod get_all_var_names;
|
||||||
mod get_by_name;
|
mod get_by_name;
|
||||||
pub(crate) mod set;
|
pub(crate) mod set;
|
||||||
mod set_by_name;
|
mod set_by_name;
|
||||||
|
mod unset;
|
||||||
|
|
||||||
use crate::utils::pckg;
|
use crate::utils::pckg;
|
||||||
use duckscript::types::command::Commands;
|
use duckscript::types::command::Commands;
|
||||||
|
@ -16,6 +17,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
||||||
commands.set(get_by_name::create(&package))?;
|
commands.set(get_by_name::create(&package))?;
|
||||||
commands.set(set::create(&package))?;
|
commands.set(set::create(&package))?;
|
||||||
commands.set(set_by_name::create(&package))?;
|
commands.set(set_by_name::create(&package))?;
|
||||||
|
commands.set(unset::create(&package)?)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
```sh
|
```sh
|
||||||
var = set_by_name name value
|
var = set_by_name name [value]
|
||||||
```
|
```
|
||||||
|
|
||||||
This command sets the variable value based on the variable name.<br>
|
This command sets the variable value based on the variable name.<br>
|
||||||
|
@ -12,7 +12,7 @@ However, it allows for a dynamic variable name.
|
||||||
#### Parameters
|
#### Parameters
|
||||||
|
|
||||||
* The variable name.
|
* The variable name.
|
||||||
* The new variable value.
|
* The new variable value, if not provided, the variable will be unset.
|
||||||
|
|
||||||
#### Return Value
|
#### Return Value
|
||||||
|
|
||||||
|
|
|
@ -43,12 +43,18 @@ impl Command for CommandImpl {
|
||||||
_commands: &mut Commands,
|
_commands: &mut Commands,
|
||||||
_line: usize,
|
_line: usize,
|
||||||
) -> CommandResult {
|
) -> CommandResult {
|
||||||
if arguments.len() < 2 {
|
if arguments.is_empty() {
|
||||||
CommandResult::Error("Missing variable name and value.".to_string())
|
CommandResult::Error("Missing variable name.".to_string())
|
||||||
} else {
|
} else {
|
||||||
variables.insert(arguments[0].clone(), arguments[1].clone());
|
let output = if arguments.len() > 1 {
|
||||||
|
variables.insert(arguments[0].clone(), arguments[1].clone());
|
||||||
|
Some(arguments[1].clone())
|
||||||
|
} else {
|
||||||
|
variables.remove(&arguments[0]);
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
CommandResult::Continue(Some(arguments[1].clone()))
|
CommandResult::Continue(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::sdk::std::var::set;
|
||||||
use crate::test;
|
use crate::test;
|
||||||
use crate::test::CommandValidation;
|
use crate::test::CommandValidation;
|
||||||
|
|
||||||
|
@ -14,11 +15,18 @@ fn run_no_arguments() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn run_only_name() {
|
fn run_only_name() {
|
||||||
test::run_script_and_error(vec![create("")], "out = set_by_name test", "out");
|
test::run_script_and_validate(
|
||||||
|
vec![create(""), set::create("")],
|
||||||
|
r#"
|
||||||
|
out = set test
|
||||||
|
set_by_name out
|
||||||
|
"#,
|
||||||
|
CommandValidation::None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn run_valid() {
|
fn run_name_and_value() {
|
||||||
let context = test::run_script_and_validate(
|
let context = test::run_script_and_validate(
|
||||||
vec![create("")],
|
vec![create("")],
|
||||||
"out = set_by_name test value",
|
"out = set_by_name test value",
|
||||||
|
|
24
duckscript_sdk/src/sdk/std/var/unset/help.md
Normal file
24
duckscript_sdk/src/sdk/std/var/unset/help.md
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
```sh
|
||||||
|
unset [names]*
|
||||||
|
```
|
||||||
|
|
||||||
|
Undefines all the variable names provided.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
A list of variable names to undefine.
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
var = set 1
|
||||||
|
defined = is_defined var
|
||||||
|
assert ${defined}
|
||||||
|
unset var
|
||||||
|
defined = is_defined var
|
||||||
|
assert_false ${defined}
|
||||||
|
```
|
22
duckscript_sdk/src/sdk/std/var/unset/mod.rs
Executable file
22
duckscript_sdk/src/sdk/std/var/unset/mod.rs
Executable file
|
@ -0,0 +1,22 @@
|
||||||
|
use crate::types::command::create_alias_command;
|
||||||
|
use crate::utils::pckg;
|
||||||
|
use duckscript::types::command::Command;
|
||||||
|
use duckscript::types::error::ScriptError;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "./mod_test.rs"]
|
||||||
|
mod mod_test;
|
||||||
|
|
||||||
|
pub(crate) fn create(package: &str) -> Result<Box<dyn Command>, ScriptError> {
|
||||||
|
let name = pckg::concat(package, "Unset");
|
||||||
|
let command = create_alias_command(
|
||||||
|
name,
|
||||||
|
vec!["unset".to_string()],
|
||||||
|
include_str!("help.md").to_string(),
|
||||||
|
"unset".to_string(),
|
||||||
|
include_str!("script.ds").to_string(),
|
||||||
|
0,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(Box::new(command))
|
||||||
|
}
|
7
duckscript_sdk/src/sdk/std/var/unset/mod_test.rs
Normal file
7
duckscript_sdk/src/sdk/std/var/unset/mod_test.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
use super::*;
|
||||||
|
use crate::test;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn common_functions() {
|
||||||
|
test::test_common_command_functions(create("").unwrap());
|
||||||
|
}
|
4
duckscript_sdk/src/sdk/std/var/unset/script.ds
Normal file
4
duckscript_sdk/src/sdk/std/var/unset/script.ds
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
for scope::unset::name in ${scope::unset::arguments}
|
||||||
|
set_by_name ${scope::unset::name}
|
||||||
|
end
|
|
@ -201,7 +201,7 @@ impl Command for AliasCommand {
|
||||||
clear(&self.scope_name, variables);
|
clear(&self.scope_name, variables);
|
||||||
|
|
||||||
let end_count = variables.len();
|
let end_count = variables.len();
|
||||||
if start_count != end_count {
|
if start_count < end_count {
|
||||||
CommandResult::Crash(
|
CommandResult::Crash(
|
||||||
format!(
|
format!(
|
||||||
"Memory leak detected, delta variables count: {}",
|
"Memory leak detected, delta variables count: {}",
|
||||||
|
|
|
@ -1,5 +1,16 @@
|
||||||
|
|
||||||
fn test_valid
|
fn test_name_only
|
||||||
|
name = set test
|
||||||
|
assert_eq ${name} test
|
||||||
|
|
||||||
|
value = set_by_name name
|
||||||
|
defined = is_defined value
|
||||||
|
assert_false ${defined}
|
||||||
|
defined = is_defined name
|
||||||
|
assert_false ${defined}
|
||||||
|
end
|
||||||
|
|
||||||
|
fn test_name_and_value
|
||||||
name = set test
|
name = set test
|
||||||
assert_eq ${name} test
|
assert_eq ${name} test
|
||||||
|
|
||||||
|
|
42
test/std/var/unset_test.ds
Normal file
42
test/std/var/unset_test.ds
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
|
||||||
|
fn test_empty
|
||||||
|
unset
|
||||||
|
end
|
||||||
|
|
||||||
|
fn test_single
|
||||||
|
name = set test
|
||||||
|
assert_eq ${name} test
|
||||||
|
|
||||||
|
value = unset name
|
||||||
|
defined = is_defined value
|
||||||
|
assert_false ${defined}
|
||||||
|
defined = is_defined name
|
||||||
|
assert_false ${defined}
|
||||||
|
end
|
||||||
|
|
||||||
|
fn test_multiple
|
||||||
|
name1 = set test1
|
||||||
|
name2 = set test2
|
||||||
|
|
||||||
|
value = unset name1 name2
|
||||||
|
defined = is_defined value
|
||||||
|
assert_false ${defined}
|
||||||
|
defined = is_defined name1
|
||||||
|
assert_false ${defined}
|
||||||
|
defined = is_defined name2
|
||||||
|
assert_false ${defined}
|
||||||
|
end
|
||||||
|
|
||||||
|
fn test_not_found_mixed
|
||||||
|
name1 = set test1
|
||||||
|
name2 = set test2
|
||||||
|
|
||||||
|
value = unset name1 name2 name3 name4 name1
|
||||||
|
defined = is_defined value
|
||||||
|
assert_false ${defined}
|
||||||
|
defined = is_defined name1
|
||||||
|
assert_false ${defined}
|
||||||
|
defined = is_defined name2
|
||||||
|
assert_false ${defined}
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue