From 898e2c4799d413b45957edc5d53a219ae514a0a7 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Sat, 2 May 2020 12:13:27 +0000 Subject: [PATCH] New set_by_name command --- CHANGELOG.md | 1 + docs/sdk.md | 38 ++++++++++++ duckscript_sdk/src/sdk/std/var/mod.rs | 2 + .../src/sdk/std/var/set_by_name/help.md | 30 ++++++++++ .../src/sdk/std/var/set_by_name/mod.rs | 60 +++++++++++++++++++ .../src/sdk/std/var/set_by_name/mod_test.rs | 29 +++++++++ test/std/var/set_by_name_test.ds | 10 ++++ 7 files changed, 170 insertions(+) create mode 100644 duckscript_sdk/src/sdk/std/var/set_by_name/help.md create mode 100755 duckscript_sdk/src/sdk/std/var/set_by_name/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/var/set_by_name/mod_test.rs create mode 100644 test/std/var/set_by_name_test.ds diff --git a/CHANGELOG.md b/CHANGELOG.md index 53a78ef..089f539 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### v0.3.4 * New get_by_name command. +* New set_by_name command. ### v0.3.3 (2020-04-15) diff --git a/docs/sdk.md b/docs/sdk.md index 43a5c2d..2ae7fb4 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -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) @@ -4831,6 +4832,43 @@ assert_eq ${value} FALSE #### Aliases: set + +## std::var::SetByName +```sh +var = set_by_name name value +``` + +This command sets the variable value based on the variable name.
+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. diff --git a/duckscript_sdk/src/sdk/std/var/mod.rs b/duckscript_sdk/src/sdk/std/var/mod.rs index 5d930e1..071930d 100755 --- a/duckscript_sdk/src/sdk/std/var/mod.rs +++ b/duckscript_sdk/src/sdk/std/var/mod.rs @@ -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(()) } diff --git a/duckscript_sdk/src/sdk/std/var/set_by_name/help.md b/duckscript_sdk/src/sdk/std/var/set_by_name/help.md new file mode 100644 index 0000000..50f6c89 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/var/set_by_name/help.md @@ -0,0 +1,30 @@ +```sh +var = set_by_name name value +``` + +This command sets the variable value based on the variable name.
+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 +``` diff --git a/duckscript_sdk/src/sdk/std/var/set_by_name/mod.rs b/duckscript_sdk/src/sdk/std/var/set_by_name/mod.rs new file mode 100755 index 0000000..b7643de --- /dev/null +++ b/duckscript_sdk/src/sdk/std/var/set_by_name/mod.rs @@ -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 { + vec!["set_by_name".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 { + 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 { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/var/set_by_name/mod_test.rs b/duckscript_sdk/src/sdk/std/var/set_by_name/mod_test.rs new file mode 100644 index 0000000..52686bf --- /dev/null +++ b/duckscript_sdk/src/sdk/std/var/set_by_name/mod_test.rs @@ -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"); +} diff --git a/test/std/var/set_by_name_test.ds b/test/std/var/set_by_name_test.ds new file mode 100644 index 0000000..6ad11ac --- /dev/null +++ b/test/std/var/set_by_name_test.ds @@ -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 +