add map type
This commit is contained in:
parent
ec62c7fe67
commit
7483a1315b
4 changed files with 39 additions and 2 deletions
|
|
@ -1,10 +1,11 @@
|
|||
from typing import Optional
|
||||
|
||||
from ytdl_sub.script.functions.array_functions import ArrayFunctions
|
||||
from ytdl_sub.script.functions.map_functions import MapFunctions
|
||||
from ytdl_sub.script.functions.numeric_functions import NumericFunctions
|
||||
from ytdl_sub.script.functions.special_functions import SpecialFunctions
|
||||
from ytdl_sub.script.functions.string_functions import StringFunctions
|
||||
|
||||
|
||||
class Functions(StringFunctions, NumericFunctions, SpecialFunctions, ArrayFunctions):
|
||||
class Functions(StringFunctions, NumericFunctions, SpecialFunctions, ArrayFunctions, MapFunctions):
|
||||
pass
|
||||
|
|
|
|||
21
src/ytdl_sub/script/functions/map_functions.py
Normal file
21
src/ytdl_sub/script/functions/map_functions.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from typing import List, Dict
|
||||
|
||||
from ytdl_sub.script.types.resolvable import Array, Map
|
||||
from ytdl_sub.script.types.resolvable import Resolvable
|
||||
from ytdl_sub.utils.exceptions import StringFormattingException
|
||||
|
||||
|
||||
class MapFunctions:
|
||||
@staticmethod
|
||||
def map(*key_values: Array) -> Map:
|
||||
output: Dict[Resolvable, Resolvable] = {}
|
||||
|
||||
for key_value in key_values:
|
||||
if len(key_value.value) != 2:
|
||||
raise StringFormattingException(
|
||||
"%map must take Arrays containing pairs of keys and values"
|
||||
)
|
||||
|
||||
output[key_value.value[0]] = key_value.value[1]
|
||||
|
||||
return Map(output)
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
from abc import ABC
|
||||
from abc import abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
from typing import Any, Dict
|
||||
from typing import Generic
|
||||
from typing import List
|
||||
from typing import TypeVar
|
||||
|
|
@ -66,3 +66,10 @@ class Array(Resolvable):
|
|||
|
||||
def __str__(self) -> str:
|
||||
return f"[{', '.join([val.value for val in self.value])}]"
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Map(Resolvable):
|
||||
value: Dict[Resolvable, Resolvable]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"[{', '.join([val.value for val in self.value])}]"
|
||||
|
|
@ -35,6 +35,14 @@ class TestParser:
|
|||
parsed_extend = parse("hi {%extend(['elem1', 'elem2'], ['elem3'], [], ['elem4'])}")
|
||||
assert False
|
||||
|
||||
def test_map(self):
|
||||
parsed = parse("hello {%map(['elem1', 'elem2'])}")
|
||||
parsed_empty = parse("hello {%map()}")
|
||||
parsed_with_var = parse("hello {%map([variable_name, 'elem2'])}")
|
||||
parsed_extend = parse("hi {%map([variable_name, 'elem2'], ['elem3', variable_name])}")
|
||||
parsed_extend.resolve({})
|
||||
assert False
|
||||
|
||||
def test_conditional(self):
|
||||
parsed = parse("hello {%if(True, 'hi', 3.4)}")
|
||||
assert parsed == SyntaxTree(
|
||||
|
|
|
|||
Loading…
Reference in a new issue