From 2d49a8c43e18d3dd3c3ef50bb04099177acb16ef Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Sat, 1 Feb 2020 11:00:18 +0000 Subject: [PATCH] New map command --- CHANGELOG.md | 3 +- docs/sdk.md | 32 ++++++++++ .../src/sdk/std/collections/map/help.md | 24 ++++++++ .../src/sdk/std/collections/map/mod.rs | 60 +++++++++++++++++++ .../src/sdk/std/collections/map/mod_test.rs | 25 ++++++++ duckscript_sdk/src/sdk/std/collections/mod.rs | 2 + 6 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 duckscript_sdk/src/sdk/std/collections/map/help.md create mode 100755 duckscript_sdk/src/sdk/std/collections/map/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/collections/map/mod_test.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b2e3fb..c1cacf8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ### v0.1.9 -* The set command now supports 'or' condition +* New map command. +* The set command now supports 'or' condition. * New base64 command #79 * New write_binary_file command #78 * New read_binary_file command #78 diff --git a/docs/sdk.md b/docs/sdk.md index 82b61b4..225cdd9 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -21,6 +21,7 @@ * [std::collections::ArrayPop (array_pop)](#std__collections__ArrayPop) * [std::collections::ArrayPush (array_push)](#std__collections__ArrayPush) * [std::collections::IsArray (is_array)](#std__collections__IsArray) +* [std::collections::Map (map)](#std__collections__Map) * [std::collections::Range (range)](#std__collections__Range) * [std::collections::ReadProperties (read_properties)](#std__collections__ReadProperties) * [std::collections::WriteProperties (write_properties)](#std__collections__WriteProperties) @@ -1036,6 +1037,37 @@ echo Array length: ${len} released: ${released} #### Aliases: is_array + +## std::collections::Map +```sh +handle = map +``` + +Creates an empty map and returns a handle to that array.
+This handle can be passed to other commands which support maps using handles.
+Once the map is no longer used, it should be released using the **release** command. + +#### Parameters + +None + +#### Return Value + +A handle to the map. + +#### Examples + +```sh +handle = map + +# once done we should release the handle +release ${handle} +``` + + +#### Aliases: +map + ## std::collections::Range ```sh diff --git a/duckscript_sdk/src/sdk/std/collections/map/help.md b/duckscript_sdk/src/sdk/std/collections/map/help.md new file mode 100644 index 0000000..9d5cc09 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map/help.md @@ -0,0 +1,24 @@ +```sh +handle = map +``` + +Creates an empty map and returns a handle to that array.
+This handle can be passed to other commands which support maps using handles.
+Once the map is no longer used, it should be released using the **release** command. + +#### Parameters + +None + +#### Return Value + +A handle to the map. + +#### Examples + +```sh +handle = map + +# once done we should release the handle +release ${handle} +``` diff --git a/duckscript_sdk/src/sdk/std/collections/map/mod.rs b/duckscript_sdk/src/sdk/std/collections/map/mod.rs new file mode 100755 index 0000000..2ac531d --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map/mod.rs @@ -0,0 +1,60 @@ +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, "Map") + } + + fn aliases(&self) -> Vec { + vec!["map".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 map = HashMap::new(); + + let key = put_handle(state, StateValue::SubState(map)); + + 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/collections/map/mod_test.rs b/duckscript_sdk/src/sdk/std/collections/map/mod_test.rs new file mode 100644 index 0000000..f07a69f --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map/mod_test.rs @@ -0,0 +1,25 @@ +use super::*; +use crate::test; +use crate::test::CommandValidation; +use crate::utils::state::get_handles_sub_state; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} + +#[test] +fn run_no_args() { + let mut context = test::run_script_and_validate( + vec![create("")], + "out = map", + CommandValidation::Contains("out".to_string(), "handle:".to_string()), + ); + + let state = get_handles_sub_state(&mut context.state); + let map_value = state.get(context.variables.get("out").unwrap()).unwrap(); + match map_value { + StateValue::SubState(map) => assert!(map.is_empty()), + _ => panic!("Invalid handle type."), + } +} diff --git a/duckscript_sdk/src/sdk/std/collections/mod.rs b/duckscript_sdk/src/sdk/std/collections/mod.rs index 48b302e..4cac3c0 100755 --- a/duckscript_sdk/src/sdk/std/collections/mod.rs +++ b/duckscript_sdk/src/sdk/std/collections/mod.rs @@ -6,6 +6,7 @@ pub(crate) mod array_length; pub(crate) mod array_pop; mod array_push; mod is_array; +mod map; mod range; mod read_properties; mod write_properties; @@ -27,6 +28,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(array_length::create(&package))?; commands.set(array_pop::create(&package))?; commands.set(is_array::create(&package))?; + commands.set(map::create(&package))?; commands.set(range::create(&package))?; commands.set(read_properties::create(&package))?; commands.set(write_properties::create(&package))?;