From e64effc131cc4ffc771d8d5abce7fa1ab0446c98 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Sun, 3 May 2020 20:57:59 +0000 Subject: [PATCH] New map_contains_key command --- CHANGELOG.md | 1 + docs/sdk.md | 57 +++++++++++++++++++ .../std/collections/map_contains_key/help.md | 22 +++++++ .../std/collections/map_contains_key/mod.rs | 22 +++++++ .../collections/map_contains_key/mod_test.rs | 7 +++ .../collections/map_contains_key/script.ds | 19 +++++++ duckscript_sdk/src/sdk/std/collections/mod.rs | 2 + test/std/collections/map_contains_key_test.ds | 57 +++++++++++++++++++ 8 files changed, 187 insertions(+) create mode 100644 duckscript_sdk/src/sdk/std/collections/map_contains_key/help.md create mode 100755 duckscript_sdk/src/sdk/std/collections/map_contains_key/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/collections/map_contains_key/mod_test.rs create mode 100644 duckscript_sdk/src/sdk/std/collections/map_contains_key/script.ds create mode 100644 test/std/collections/map_contains_key_test.ds diff --git a/CHANGELOG.md b/CHANGELOG.md index 65f191d..b7a2bfc 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### v0.3.4 * \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands #103 +* New map_contains_key command. * New get_all_var_names command #100 * New get_by_name command. * New set_by_name command. diff --git a/docs/sdk.md b/docs/sdk.md index 7f5c163..24b332a 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -19,6 +19,7 @@ * [std::collections::IsMap (is_map)](#std__collections__IsMap) * [std::collections::Map (map)](#std__collections__Map) * [std::collections::MapClear (map_clear)](#std__collections__MapClear) +* [std::collections::MapContainsKey (map_contains_key)](#std__collections__MapContainsKey) * [std::collections::MapGet (map_get)](#std__collections__MapGet) * [std::collections::MapIsEmpty (map_is_empty)](#std__collections__MapIsEmpty) * [std::collections::MapKeys (map_keys)](#std__collections__MapKeys) @@ -888,6 +889,62 @@ release ${handle} #### Aliases: map_clear + +## std::collections::MapContainsKey + +```sh +var = map_contains_key handle key +``` + +Returns true if the provided key was found in the map. + +#### Parameters + +* The map handle. +* The key + +#### Return Value + +True if the key was found in the map. + +#### Examples + +```sh +handle = map +map_put ${handle} key value +found = map_contains_key ${handle} key +``` + + +#### Source: + +```sh + +scope::map_contains_key::found = set false +scope::map_contains_key::not_empty = not map_is_empty ${scope::map_contains_key::argument::1} + +if ${scope::map_contains_key::not_empty} + scope::map_contains_key::key = set ${scope::map_contains_key::argument::2} + scope::map_contains_key::key_array_handle = map_keys ${scope::map_contains_key::argument::1} + + for scope::map_contains_key::item in ${scope::map_contains_key::key_array_handle} + scope::map_contains_key::found = equals ${scope::map_contains_key::item} ${scope::map_contains_key::key} + + if ${scope::map_contains_key::found} + release ${scope::map_contains_key::key_array_handle} + end + end +end + +release ${scope::map_contains_key::key_array_handle} +set ${scope::map_contains_key::found} + +``` + + +#### Aliases: +map_contains_key + ## std::collections::MapGet ```sh diff --git a/duckscript_sdk/src/sdk/std/collections/map_contains_key/help.md b/duckscript_sdk/src/sdk/std/collections/map_contains_key/help.md new file mode 100644 index 0000000..861596c --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map_contains_key/help.md @@ -0,0 +1,22 @@ +```sh +var = map_contains_key handle key +``` + +Returns true if the provided key was found in the map. + +#### Parameters + +* The map handle. +* The key + +#### Return Value + +True if the key was found in the map. + +#### Examples + +```sh +handle = map +map_put ${handle} key value +found = map_contains_key ${handle} key +``` diff --git a/duckscript_sdk/src/sdk/std/collections/map_contains_key/mod.rs b/duckscript_sdk/src/sdk/std/collections/map_contains_key/mod.rs new file mode 100755 index 0000000..0ff3601 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map_contains_key/mod.rs @@ -0,0 +1,22 @@ +use crate::types::command::create_alias_command; +use crate::utils::pckg; +use duckscript::types::command::Command; +use duckscript::types::error::ScriptError; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +pub(crate) fn create(package: &str) -> Result, ScriptError> { + let name = pckg::concat(package, "MapContainsKey"); + let command = create_alias_command( + name, + vec!["map_contains_key".to_string()], + include_str!("help.md").to_string(), + "map_contains_key".to_string(), + include_str!("script.ds").to_string(), + 2, + )?; + + Ok(Box::new(command)) +} diff --git a/duckscript_sdk/src/sdk/std/collections/map_contains_key/mod_test.rs b/duckscript_sdk/src/sdk/std/collections/map_contains_key/mod_test.rs new file mode 100644 index 0000000..cbc4367 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map_contains_key/mod_test.rs @@ -0,0 +1,7 @@ +use super::*; +use crate::test; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("").unwrap()); +} diff --git a/duckscript_sdk/src/sdk/std/collections/map_contains_key/script.ds b/duckscript_sdk/src/sdk/std/collections/map_contains_key/script.ds new file mode 100644 index 0000000..74ccb1a --- /dev/null +++ b/duckscript_sdk/src/sdk/std/collections/map_contains_key/script.ds @@ -0,0 +1,19 @@ + +scope::map_contains_key::found = set false +scope::map_contains_key::not_empty = not map_is_empty ${scope::map_contains_key::argument::1} + +if ${scope::map_contains_key::not_empty} + scope::map_contains_key::key = set ${scope::map_contains_key::argument::2} + scope::map_contains_key::key_array_handle = map_keys ${scope::map_contains_key::argument::1} + + for scope::map_contains_key::item in ${scope::map_contains_key::key_array_handle} + scope::map_contains_key::found = equals ${scope::map_contains_key::item} ${scope::map_contains_key::key} + + if ${scope::map_contains_key::found} + release ${scope::map_contains_key::key_array_handle} + end + end +end + +release ${scope::map_contains_key::key_array_handle} +set ${scope::map_contains_key::found} diff --git a/duckscript_sdk/src/sdk/std/collections/mod.rs b/duckscript_sdk/src/sdk/std/collections/mod.rs index 35f19e5..35e3872 100755 --- a/duckscript_sdk/src/sdk/std/collections/mod.rs +++ b/duckscript_sdk/src/sdk/std/collections/mod.rs @@ -11,6 +11,7 @@ mod is_array; mod is_map; mod map; mod map_clear; +mod map_contains_key; mod map_get; mod map_is_empty; mod map_keys; @@ -45,6 +46,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(is_map::create(&package))?; commands.set(map::create(&package))?; commands.set(map_clear::create(&package))?; + commands.set(map_contains_key::create(&package)?)?; commands.set(map_get::create(&package))?; commands.set(map_is_empty::create(&package)?)?; commands.set(map_keys::create(&package))?; diff --git a/test/std/collections/map_contains_key_test.ds b/test/std/collections/map_contains_key_test.ds new file mode 100644 index 0000000..7eea02e --- /dev/null +++ b/test/std/collections/map_contains_key_test.ds @@ -0,0 +1,57 @@ + +fn test_empty + handle = map + + found = map_contains_key ${handle} key + release ${handle} + + assert_false ${found} +end + +fn test_not_found + handle = map + map_put ${handle} key1 value + + found = map_contains_key ${handle} key2 + release ${handle} + + assert_false ${found} +end + +fn test_not_found + handle = map + map_put ${handle} key1 value + + found = map_contains_key ${handle} key2 + release ${handle} + + assert_false ${found} +end + +fn test_found + handle = map + map_put ${handle} key value + + found = map_contains_key ${handle} key + release ${handle} + + assert ${found} +end + +fn test_both + handle = map + map_put ${handle} key1 value + map_put ${handle} key2 value + + found = map_contains_key ${handle} key1 + assert ${found} + + found = map_contains_key ${handle} key2 + assert ${found} + + found = map_contains_key ${handle} key3 + assert_false ${found} + + release ${handle} +end +