New set_by_name command

This commit is contained in:
sagie gur ari 2020-05-02 12:13:27 +00:00
parent 02e99a7a82
commit 898e2c4799
7 changed files with 170 additions and 0 deletions

View file

@ -3,6 +3,7 @@
### v0.3.4
* New get_by_name command.
* New set_by_name command.
### v0.3.3 (2020-04-15)

View file

@ -132,6 +132,7 @@
* [std::time::CurrentTimeMillies (current_time)](#std__time__CurrentTimeMillies)
* [std::var::GetByName (get_by_name)](#std__var__GetByName)
* [std::var::Set (set)](#std__var__Set)
* [std::var::SetByName (set_by_name)](#std__var__SetByName)
<a name="std__Echo"></a>
@ -4831,6 +4832,43 @@ assert_eq ${value} FALSE
#### Aliases:
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
Developed by Sagie Gur-Ari and licensed under the
[Apache 2](https://github.com/sagiegurari/duckscript/blob/master/LICENSE) open source license.

View file

@ -1,5 +1,6 @@
mod get_by_name;
pub(crate) mod set;
mod set_by_name;
use crate::utils::pckg;
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(set::create(&package))?;
commands.set(set_by_name::create(&package))?;
Ok(())
}

View 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
```

View 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(),
})
}

View 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");
}

View 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