New get_all_var_names command #100

This commit is contained in:
sagie gur ari 2020-05-03 19:36:09 +00:00
parent 1350385843
commit 170e1e4a90
7 changed files with 153 additions and 1 deletions

View file

@ -2,9 +2,10 @@
### v0.3.4
* \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands #103
* New get_all_var_names command #100
* New get_by_name command.
* New set_by_name command.
* \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands.
### v0.3.3 (2020-04-15)

View file

@ -130,6 +130,7 @@
* [std::test::TestDirectory (test_directory)](#std__test__TestDirectory)
* [std::thread::Sleep (sleep)](#std__thread__Sleep)
* [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::Set (set)](#std__var__Set)
* [std::var::SetByName (set_by_name)](#std__var__SetByName)
@ -4752,6 +4753,35 @@ echo ${result}
#### Aliases:
current_time
<a name="std__var__GetAllVarNames"></a>
## std::var::GetAllVarNames
```sh
handle = get_all_var_names
```
Creates an array holding all currently known variable names and returns the array handle.
#### Parameters
None
#### Return Value
A handle to the array.
#### Examples
```sh
handle = get_all_var_names
# once done we should release the handle
release ${handle}
```
#### Aliases:
get_all_var_names
<a name="std__var__GetByName"></a>
## std::var::GetByName
```sh

View file

@ -0,0 +1,22 @@
```sh
handle = get_all_var_names
```
Creates an array holding all currently known variable names and returns the array handle.
#### Parameters
None
#### Return Value
A handle to the array.
#### Examples
```sh
handle = get_all_var_names
# once done we should release the handle
release ${handle}
```

View file

@ -0,0 +1,64 @@
use crate::utils::pckg;
use crate::utils::state::put_handle;
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, "GetAllVarNames")
}
fn aliases(&self) -> Vec<String> {
vec!["get_all_var_names".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 {
let mut array = vec![];
for key in variables.keys() {
array.push(StateValue::String(key.to_string()));
}
let key = put_handle(state, StateValue::List(array));
CommandResult::Continue(Some(key))
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,7 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}

View file

@ -1,3 +1,4 @@
mod get_all_var_names;
mod get_by_name;
pub(crate) mod set;
mod set_by_name;
@ -11,6 +12,7 @@ static PACKAGE: &str = "var";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(get_all_var_names::create(&package))?;
commands.set(get_by_name::create(&package))?;
commands.set(set::create(&package))?;
commands.set(set_by_name::create(&package))?;

View file

@ -0,0 +1,26 @@
fn test_with_values
test1 = set 1
test2 = set 2
map_handle = map
map_put ${map_handle} test1 1
map_put ${map_handle} test2 1
map_put ${map_handle} map_handle 1
handle = get_all_var_names
size = array_length ${handle}
assert_eq 3 ${size}
for name in ${handle}
map_remove ${map_handle} ${name}
end
size = map_size ${map_handle}
assert_eq 0 ${size}
release ${map_handle}
release ${handle}
end