mirror of
https://github.com/OMGeeky/pytiled_parser.git
synced 2026-02-14 15:21:14 +01:00
refactor: changed RawProperties in properties.py to be a TypedDict
This commit is contained in:
@@ -1,25 +1,36 @@
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, NamedTuple, Optional, Union
|
||||
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
from .common_types import Color
|
||||
|
||||
Property = Union[int, float, Path, str, bool, Color]
|
||||
class
|
||||
Property = Union[float, Path, str, bool, Color]
|
||||
RawProperty = Union[float, str, bool]
|
||||
|
||||
RawProperties = List[Dict[str, Property]]
|
||||
|
||||
class RawProperties(TypedDict):
|
||||
"""A dictionary of raw properties."""
|
||||
|
||||
name: str
|
||||
type: str
|
||||
value: RawProperty
|
||||
|
||||
|
||||
Properties = Dict[str, Property]
|
||||
|
||||
|
||||
def cast(raw: RawProperties) -> Dict[str, Property]:
|
||||
def cast(raw: List[RawProperties]) -> Dict[str, Property]:
|
||||
final: Properties = {}
|
||||
value: Property
|
||||
|
||||
for prop in raw:
|
||||
value = prop["value"]
|
||||
if prop["type"] == "file":
|
||||
value = Path(str(value))
|
||||
value = Path(str(prop["value"]))
|
||||
elif prop["type"] == "color":
|
||||
value = Color(str(value))
|
||||
value = Color(str(prop["value"]))
|
||||
else:
|
||||
value = prop["value"]
|
||||
final[str(prop["name"])] = value
|
||||
|
||||
return final
|
||||
|
||||
@@ -1100,6 +1100,6 @@ OBJECTS = ELLIPSES + RECTANGLES + POINTS + TILES + POLYGONS + POLYLINES + TEXTS
|
||||
@pytest.mark.parametrize("raw_object_json,expected", OBJECTS)
|
||||
def test_parse_layer(raw_object_json, expected):
|
||||
raw_object = json.loads(raw_object_json)
|
||||
result = tiled_object._cast_tiled_object(raw_object)
|
||||
result = tiled_object.cast(raw_object)
|
||||
|
||||
assert result == expected
|
||||
|
||||
Reference in New Issue
Block a user