hashable built in func

This commit is contained in:
Jesse Bannon 2024-04-28 10:05:30 -07:00
parent da14fe072d
commit 17634dac0a
5 changed files with 31 additions and 8 deletions

View file

@ -498,6 +498,8 @@ class _Parser:
raise MAP_KEY_WITH_NO_VALUE
if isinstance(key, NonHashable):
raise MAP_KEY_NOT_HASHABLE
if isinstance(key, BuiltInFunction) and issubclass(key.output_type(), NonHashable):
raise MAP_KEY_NOT_HASHABLE
if len(value_args) > 1:
raise MAP_KEY_MULTIPLE_VALUES

View file

@ -291,3 +291,6 @@ class BuiltInFunction(Function, BuiltInFunctionType):
raise FunctionRuntimeException(
f"Runtime error occurred when executing the function %{self.name}: {str(exc)}"
) from exc
def __hash__(self):
return hash((self.name, *self.args))

View file

@ -66,7 +66,9 @@ class ScriptUtils:
ast = parse(text=value).ast
if len(ast) == 1:
return ast[0]
return BuiltInFunction(name="concat", args=ast)
return BuiltInFunction(
name="concat", args=[BuiltInFunction(name="string", args=[arg]) for arg in ast]
)
if isinstance(value, int):
return Integer(value)
if isinstance(value, float):

View file

@ -198,3 +198,16 @@ class TestMap:
"key_variable": "{['non-hashable']}",
}
).resolve()
def test_map_key_is_function(self):
assert Script(
{
"dict": "{{ %concat('hi', %string(' world')) : 'value' }}",
"key_variable": "hashable",
}
).resolve() == ScriptOutput(
{
"key_variable": String("hashable"),
"dict": Map(value={String(value="hi world"): String(value="value")}),
}
)

View file

@ -97,6 +97,7 @@ class TestUnstructuredDictFormatterValidator(object):
key5_bool = True
key6_map = {"{variable}_key": "value", "static_key": "{variable}_value"}
key7_list = ["list_1", "list_{variable_2}"]
key8_many_vars = "string {variable1} with multiple {variable2}"
validator = dict_validator_class(
name="validator",
value={
@ -107,17 +108,19 @@ class TestUnstructuredDictFormatterValidator(object):
"key5": key5_bool,
"key6": key6_map,
"key7": key7_list,
"key8": key8_many_vars,
},
)
assert len(validator.dict) == 7
assert len(validator.dict) == 8
assert all(isinstance(val, expected_formatter_class) for val in validator.dict.values())
assert validator.dict_with_format_strings == {
"key1": "string with {variable}",
"key1": "{ %concat( %string( '''string with ''' ), %string( variable ) ) }",
"key2": "no variables",
"key3": "{%int(3)}",
"key4": "{%float(4.132)}",
"key5": "{%int(True)}",
"key6": '{%from_json(\'\'\'{"static_key": "{variable}_value", "{variable}_key": "value"}\'\'\')}',
"key7": "{%from_json('''[\"list_1\", \"list_{variable_2}\"]''')}",
"key3": "{ %int(3) }",
"key4": "{ %float(4.132) }",
"key5": "{ %int(True) }",
"key6": "{ { %concat( %string( variable ), %string( '''_key''' ) ): '''value''', '''static_key''': %concat( %string( variable ), %string( '''_value''' ) ) } }",
"key7": "{ [ '''list_1''', %concat( %string( '''list_''' ), %string( variable_2 ) ) ] }",
"key8": "{ %concat( %string( '''string ''' ), %string( variable1 ), %string( ''' with multiple ''' ), %string( variable2 ) ) }",
}