diff --git a/src/tools/collections.rs b/src/tools/collections.rs index 5f504e8..1bacac9 100644 --- a/src/tools/collections.rs +++ b/src/tools/collections.rs @@ -18,27 +18,22 @@ impl Tool for Transform { } fn run(&self, argument: &Value) -> Result { - let argument = self.check_type(argument)?; + let argument = self.check_type(argument)?.as_list()?; + let list = argument[0].as_list()?; + let function = argument[1].as_function()?; + let mut mapped_list = Vec::with_capacity(list.len()); - if let Value::List(list) = argument { - let list = list[0].as_list()?; - let function = list[1].as_function()?; - let mut mapped_list = Vec::with_capacity(list.len()); + for value in list { + let mut context = VariableMap::new(); - for value in list { - let mut context = VariableMap::new(); + context.set_value("input", value.clone())?; - context.set_value("input", value.clone())?; + let mapped_value = function.run_with_context(&mut context)?; - let mapped_value = function.run_with_context(&mut context)?; - - mapped_list.push(mapped_value); - } - - return Ok(Value::List(mapped_list)); + mapped_list.push(mapped_value); } - self.fail(argument) + return Ok(Value::List(mapped_list)); } } @@ -84,7 +79,12 @@ impl Tool for Count { identifier: "count", description: "Return the number of items in a collection.", group: "collections", - inputs: vec![ValueType::Any], + inputs: vec![ + ValueType::String, + ValueType::List, + ValueType::Map, + ValueType::Table, + ], } } diff --git a/tests/collections.ds b/tests/collections.ds index 936aaa3..4099e71 100644 --- a/tests/collections.ds +++ b/tests/collections.ds @@ -1,10 +1,11 @@ - # transform + list = (1, 2, 3); test = transform(list, 'input + 1'); assert_equal((2, 3, 4), test); # string + test = string(42); assert_equal("42", test); @@ -14,9 +15,100 @@ assert_equal("42.42", test); test = string(false); assert_equal("false", test); +# create_table + +table = create_table( + ("text", "num"), + ( + ("foo", 1), + ("bar", 2) + ) +); + +# rows + +test = rows(table); +assert_equal( + ( + ("foo", 1), + ("bar", 2) + ), + test +); + +# insert + +test = insert( + table, + ( + ("foo", 1), + ("bar", 2) + ) +); +assert_equal( + create_table( + ("text", "num"), + ( + ("foo", 1), + ("bar", 2), + ("foo", 1), + ("bar", 2) + ) + ), + test +); + +# select + +table = create_table( + ("text", "number", "bool"), + ( + ("a", 1, true), + ("b", 2, true), + ("a", 3, true) + ) +); + +test_table = create_table( + ("text", "bool"), + ( + ("a", true), + ("b", true), + ("a", true) + ) +); + +assert_equal(select(table, ("text", "bool")), test_table); + +test_table = create_table( + ("text", "number", "bool"), + ( + ("a", 1, true), + ("a", 3, true) + ) +); + +assert_equal(where(table, 'text == "a"'), test_table); + # count +table = create_table( + ("text", "number", "bool"), + ( + ("a", 1, true), + ("b", 2, true), + ("a", 3, true) + ) +); +test = count(table); +assert_equal(3, test); + test = count("123"); assert_equal(3, test); -# create_table +test = count(1, 2, 3); +assert_equal(3, test); + +map.x.z.y = 1; +test = count(map); +assert_equal(1, test);