diff --git a/CHANGELOG.md b/CHANGELOG.md index 000b259..65f191d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/sdk.md b/docs/sdk.md index 2ae7fb4..7f5c163 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -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 + +## 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 + ## std::var::GetByName ```sh diff --git a/duckscript_sdk/src/sdk/std/var/get_all_var_names/help.md b/duckscript_sdk/src/sdk/std/var/get_all_var_names/help.md new file mode 100644 index 0000000..bae333c --- /dev/null +++ b/duckscript_sdk/src/sdk/std/var/get_all_var_names/help.md @@ -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} +``` diff --git a/duckscript_sdk/src/sdk/std/var/get_all_var_names/mod.rs b/duckscript_sdk/src/sdk/std/var/get_all_var_names/mod.rs new file mode 100755 index 0000000..9519fa2 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/var/get_all_var_names/mod.rs @@ -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 { + vec!["get_all_var_names".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn clone_and_box(&self) -> Box { + Box::new((*self).clone()) + } + + fn requires_context(&self) -> bool { + true + } + + fn run_with_context( + &self, + _arguments: Vec, + state: &mut HashMap, + variables: &mut HashMap, + _output_variable: Option, + _instructions: &Vec, + _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 { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/var/get_all_var_names/mod_test.rs b/duckscript_sdk/src/sdk/std/var/get_all_var_names/mod_test.rs new file mode 100644 index 0000000..6ff144d --- /dev/null +++ b/duckscript_sdk/src/sdk/std/var/get_all_var_names/mod_test.rs @@ -0,0 +1,7 @@ +use super::*; +use crate::test; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} diff --git a/duckscript_sdk/src/sdk/std/var/mod.rs b/duckscript_sdk/src/sdk/std/var/mod.rs index 071930d..d74978e 100755 --- a/duckscript_sdk/src/sdk/std/var/mod.rs +++ b/duckscript_sdk/src/sdk/std/var/mod.rs @@ -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))?; diff --git a/test/std/var/get_all_var_names_test.ds b/test/std/var/get_all_var_names_test.ds new file mode 100644 index 0000000..27c64e2 --- /dev/null +++ b/test/std/var/get_all_var_names_test.ds @@ -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 +