diff --git a/src/ytdl_sub/script/types/array.py b/src/ytdl_sub/script/types/array.py index 13c4f99d..e6633f86 100644 --- a/src/ytdl_sub/script/types/array.py +++ b/src/ytdl_sub/script/types/array.py @@ -1,4 +1,5 @@ from dataclasses import dataclass +from typing import Any from typing import Dict from typing import List @@ -47,4 +48,6 @@ class UnresolvedArray(Array, VariableDependency, AnyArgument): @dataclass(frozen=True) class ResolvedArray(Array, ResolvableToJson): - pass + @property + def native(self) -> Any: + return [val.native for val in self.value] diff --git a/src/ytdl_sub/script/types/map.py b/src/ytdl_sub/script/types/map.py index 60b99b0b..9024306b 100644 --- a/src/ytdl_sub/script/types/map.py +++ b/src/ytdl_sub/script/types/map.py @@ -1,5 +1,6 @@ import itertools from dataclasses import dataclass +from typing import Any from typing import Dict from typing import List @@ -55,4 +56,6 @@ class UnresolvedMap(Map, VariableDependency, AnyArgument): @dataclass(frozen=True) class ResolvedMap(Map, ResolvableToJson): - pass + @property + def native(self) -> Any: + return {key.native: value.native for key, value in self.value.items()} diff --git a/src/ytdl_sub/script/types/resolvable.py b/src/ytdl_sub/script/types/resolvable.py index cbda1fc7..7282393b 100644 --- a/src/ytdl_sub/script/types/resolvable.py +++ b/src/ytdl_sub/script/types/resolvable.py @@ -75,6 +75,10 @@ class Resolvable(AnyArgument, ABC): def __str__(self) -> str: return str(self.value) + @property + def native(self) -> Any: + return self.value + @dataclass(frozen=True) class Hashable(Resolvable, ABC): @@ -88,19 +92,8 @@ class NonHashable(ABC): @dataclass(frozen=True) class ResolvableToJson(Resolvable, ABC): - @classmethod - def _to_native(cls, to_convert: Resolvable) -> Any: - if isinstance(to_convert.value, list): - return [cls._to_native(val) for val in to_convert.value] - if isinstance(to_convert.value, dict): - return { - cls._to_native(key): cls._to_native(value) - for key, value in to_convert.value.items() - } - return to_convert.value - def __str__(self): - return json.dumps(self._to_native(self)) + return json.dumps(self.native) @dataclass(frozen=True) @@ -158,3 +151,6 @@ class TypeHintedFunctionType(FunctionType, ABC): @dataclass(frozen=True) class Lambda(Resolvable): value: str + + def native(self) -> Any: + return f"%{self.value}" diff --git a/tests/unit/script/functions/__init__.py b/tests/unit/script/functions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/script/functions/test_array_functions.py b/tests/unit/script/functions/test_array_functions.py new file mode 100644 index 00000000..6a316787 --- /dev/null +++ b/tests/unit/script/functions/test_array_functions.py @@ -0,0 +1,20 @@ +from ytdl_sub.script.script import Script + + +class TestArrayFunctions: + def test_array_extend(self): + assert Script( + { + "array1": "{['a', 3.14]}", + "array2": "{['b', 8.8]}", + "array3": "{['c', 3.17]}", + "array_extended_output": "{%array_extend(array1, array2, array3)}", + } + ).resolve(update=True).get("array_extended_output").native == [ + "a", + 3.14, + "b", + 8.8, + "c", + 3.17, + ]