native for resolvables

This commit is contained in:
Jesse Bannon 2023-11-23 22:05:20 -08:00
parent 6a459ab114
commit 01fcad2561
5 changed files with 36 additions and 14 deletions

View file

@ -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]

View file

@ -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()}

View file

@ -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}"

View file

View file

@ -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,
]