Map future resolvable

This commit is contained in:
Jesse Bannon 2023-12-06 23:15:44 -08:00
parent abf60f5052
commit 9004fba5cb
4 changed files with 22 additions and 15 deletions

View file

@ -1,3 +1,4 @@
from abc import ABC
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
from typing import Dict from typing import Dict
@ -15,7 +16,7 @@ from ytdl_sub.script.types.variable_dependency import VariableDependency
@dataclass(frozen=True) @dataclass(frozen=True)
class _Array(NonHashable): class _Array(NonHashable, ABC):
value: List[Resolvable] value: List[Resolvable]
@classmethod @classmethod

View file

@ -1,11 +1,14 @@
import itertools import itertools
from abc import ABC
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import Type
from ytdl_sub.script.types.resolvable import AnyArgument from ytdl_sub.script.types.resolvable import AnyArgument
from ytdl_sub.script.types.resolvable import Argument from ytdl_sub.script.types.resolvable import Argument
from ytdl_sub.script.types.resolvable import FutureResolvable
from ytdl_sub.script.types.resolvable import Hashable from ytdl_sub.script.types.resolvable import Hashable
from ytdl_sub.script.types.resolvable import NonHashable from ytdl_sub.script.types.resolvable import NonHashable
from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.resolvable import Resolvable
@ -16,7 +19,7 @@ from ytdl_sub.script.utils.exceptions import KeyNotHashableRuntimeException
@dataclass(frozen=True) @dataclass(frozen=True)
class Map(NonHashable): class _Map(NonHashable, ABC):
value: Dict[Hashable, Resolvable] value: Dict[Hashable, Resolvable]
@classmethod @classmethod
@ -25,7 +28,7 @@ class Map(NonHashable):
@dataclass(frozen=True) @dataclass(frozen=True)
class UnresolvedMap(Map, VariableDependency, AnyArgument): class UnresolvedMap(_Map, VariableDependency, FutureResolvable):
value: Dict[Argument, Argument] value: Dict[Argument, Argument]
@property @property
@ -51,11 +54,14 @@ class UnresolvedMap(Map, VariableDependency, AnyArgument):
arg=value, resolved_variables=resolved_variables, custom_functions=custom_functions arg=value, resolved_variables=resolved_variables, custom_functions=custom_functions
) )
return ResolvedMap(output) return Map(output)
def future_resolvable_type(self) -> Type[Resolvable]:
return Map
@dataclass(frozen=True) @dataclass(frozen=True)
class ResolvedMap(Map, ResolvableToJson): class Map(_Map, ResolvableToJson):
@property @property
def native(self) -> Any: def native(self) -> Any:
return {key.native: value.native for key, value in self.value.items()} return {key.native: value.native for key, value in self.value.items()}

View file

@ -1,5 +1,5 @@
from ytdl_sub.script.script import Script from ytdl_sub.script.script import Script
from ytdl_sub.script.types.map import ResolvedMap from ytdl_sub.script.types.map import Map
from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.resolvable import String
@ -30,7 +30,7 @@ class TestScript:
def test_partial_update_script(self): def test_partial_update_script(self):
# to be resolved later # to be resolved later
entry_map = ResolvedMap({String("title"): String("the title")}) entry_map = Map({String("title"): String("the title")})
script = Script( script = Script(
{ {

View file

@ -10,7 +10,7 @@ from ytdl_sub.script.parser import MAP_KEY_WITH_NO_VALUE
from ytdl_sub.script.parser import MAP_MISSING_KEY from ytdl_sub.script.parser import MAP_MISSING_KEY
from ytdl_sub.script.parser import ParsedArgType from ytdl_sub.script.parser import ParsedArgType
from ytdl_sub.script.script import Script from ytdl_sub.script.script import Script
from ytdl_sub.script.types.map import ResolvedMap from ytdl_sub.script.types.map import Map
from ytdl_sub.script.types.resolvable import Float from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import String from ytdl_sub.script.types.resolvable import String
from ytdl_sub.script.utils.exceptions import InvalidSyntaxException from ytdl_sub.script.utils.exceptions import InvalidSyntaxException
@ -20,7 +20,7 @@ from ytdl_sub.script.utils.exceptions import KeyNotHashableRuntimeException
class TestMap: class TestMap:
def test_return(self): def test_return(self):
assert Script({"map": "{{'a': 3.14}}"}).resolve() == { assert Script({"map": "{{'a': 3.14}}"}).resolve() == {
"map": ResolvedMap({String("a"): Float(3.14)}) "map": Map({String("a"): Float(3.14)})
} }
def test_return_as_str(self): def test_return_as_str(self):
@ -45,13 +45,13 @@ class TestMap:
}""" }"""
assert Script({"map": map_str}).resolve() == { assert Script({"map": map_str}).resolve() == {
"map": ResolvedMap( "map": Map(
{ {
String("level1"): ResolvedMap( String("level1"): Map(
{ {
String("level2"): ResolvedMap( String("level2"): Map(
{ {
String("level3"): ResolvedMap( String("level3"): Map(
{String("level4_key"): String("level4_value")} {String("level4_key"): String("level4_value")}
), ),
String("level3_key"): String("level3_value"), String("level3_key"): String("level3_value"),
@ -75,7 +75,7 @@ class TestMap:
], ],
) )
def test_empty_map(self, empty_map: str): def test_empty_map(self, empty_map: str):
assert Script({"map": empty_map}).resolve() == {"map": ResolvedMap({})} assert Script({"map": empty_map}).resolve() == {"map": Map({})}
@pytest.mark.parametrize( @pytest.mark.parametrize(
"map", "map",
@ -168,7 +168,7 @@ class TestMap:
} }
).resolve() == { ).resolve() == {
"key_variable": String("hashable"), "key_variable": String("hashable"),
"map": ResolvedMap({String("hashable"): String("value")}), "map": Map({String("hashable"): String("value")}),
} }
def test_map_key_is_non_hashable_variable(self): def test_map_key_is_non_hashable_variable(self):