mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 20:02:32 +00:00
New set_by_name command
This commit is contained in:
parent
02e99a7a82
commit
898e2c4799
|
@ -3,6 +3,7 @@
|
||||||
### v0.3.4
|
### v0.3.4
|
||||||
|
|
||||||
* New get_by_name command.
|
* New get_by_name command.
|
||||||
|
* New set_by_name command.
|
||||||
|
|
||||||
### v0.3.3 (2020-04-15)
|
### v0.3.3 (2020-04-15)
|
||||||
|
|
||||||
|
|
38
docs/sdk.md
38
docs/sdk.md
|
@ -132,6 +132,7 @@
|
||||||
* [std::time::CurrentTimeMillies (current_time)](#std__time__CurrentTimeMillies)
|
* [std::time::CurrentTimeMillies (current_time)](#std__time__CurrentTimeMillies)
|
||||||
* [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)
|
||||||
|
|
||||||
|
|
||||||
<a name="std__Echo"></a>
|
<a name="std__Echo"></a>
|
||||||
|
@ -4831,6 +4832,43 @@ assert_eq ${value} FALSE
|
||||||
#### Aliases:
|
#### Aliases:
|
||||||
set
|
set
|
||||||
|
|
||||||
|
<a name="std__var__SetByName"></a>
|
||||||
|
## std::var::SetByName
|
||||||
|
```sh
|
||||||
|
var = set_by_name name value
|
||||||
|
```
|
||||||
|
|
||||||
|
This command sets the variable value based on the variable name.<br>
|
||||||
|
It is similar to
|
||||||
|
```sh
|
||||||
|
name = set ${value}
|
||||||
|
```
|
||||||
|
However, it allows for a dynamic variable name.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
* The variable name.
|
||||||
|
* The new variable value.
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
The new variable value.
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
var = set test
|
||||||
|
value = get_by_name var
|
||||||
|
defined = is_defined value
|
||||||
|
|
||||||
|
assert ${defined}
|
||||||
|
assert_eq ${value} test
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Aliases:
|
||||||
|
set_by_name
|
||||||
|
|
||||||
### 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.
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
mod get_by_name;
|
mod get_by_name;
|
||||||
pub(crate) mod set;
|
pub(crate) mod set;
|
||||||
|
mod set_by_name;
|
||||||
|
|
||||||
use crate::utils::pckg;
|
use crate::utils::pckg;
|
||||||
use duckscript::types::command::Commands;
|
use duckscript::types::command::Commands;
|
||||||
|
@ -12,6 +13,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))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
30
duckscript_sdk/src/sdk/std/var/set_by_name/help.md
Normal file
30
duckscript_sdk/src/sdk/std/var/set_by_name/help.md
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
```sh
|
||||||
|
var = set_by_name name value
|
||||||
|
```
|
||||||
|
|
||||||
|
This command sets the variable value based on the variable name.<br>
|
||||||
|
It is similar to
|
||||||
|
```sh
|
||||||
|
name = set ${value}
|
||||||
|
```
|
||||||
|
However, it allows for a dynamic variable name.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
* The variable name.
|
||||||
|
* The new variable value.
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
The new variable value.
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
var = set test
|
||||||
|
value = get_by_name var
|
||||||
|
defined = is_defined value
|
||||||
|
|
||||||
|
assert ${defined}
|
||||||
|
assert_eq ${value} test
|
||||||
|
```
|
60
duckscript_sdk/src/sdk/std/var/set_by_name/mod.rs
Executable file
60
duckscript_sdk/src/sdk/std/var/set_by_name/mod.rs
Executable file
|
@ -0,0 +1,60 @@
|
||||||
|
use crate::utils::pckg;
|
||||||
|
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, "SetByName")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aliases(&self) -> Vec<String> {
|
||||||
|
vec!["set_by_name".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("Missing variable name and value.".to_string())
|
||||||
|
} else {
|
||||||
|
variables.insert(arguments[0].clone(), arguments[1].clone());
|
||||||
|
|
||||||
|
CommandResult::Continue(Some(arguments[1].clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||||
|
Box::new(CommandImpl {
|
||||||
|
package: package.to_string(),
|
||||||
|
})
|
||||||
|
}
|
29
duckscript_sdk/src/sdk/std/var/set_by_name/mod_test.rs
Normal file
29
duckscript_sdk/src/sdk/std/var/set_by_name/mod_test.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use super::*;
|
||||||
|
use crate::test;
|
||||||
|
use crate::test::CommandValidation;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn common_functions() {
|
||||||
|
test::test_common_command_functions(create(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_no_arguments() {
|
||||||
|
test::run_script_and_error(vec![create("")], "out = set_by_name", "out");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_only_name() {
|
||||||
|
test::run_script_and_error(vec![create("")], "out = set_by_name test", "out");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_valid() {
|
||||||
|
let context = test::run_script_and_validate(
|
||||||
|
vec![create("")],
|
||||||
|
"out = set_by_name test value",
|
||||||
|
CommandValidation::Match("out".to_string(), "value".to_string()),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(context.variables.get("test").unwrap(), "value");
|
||||||
|
}
|
10
test/std/var/set_by_name_test.ds
Normal file
10
test/std/var/set_by_name_test.ds
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
fn test_valid
|
||||||
|
name = set test
|
||||||
|
assert_eq ${name} test
|
||||||
|
|
||||||
|
value = set_by_name name new_value
|
||||||
|
assert_eq ${name} new_value
|
||||||
|
assert_eq ${value} new_value
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue