Skip to content

Commit

Permalink
add transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Nov 15, 2024
1 parent 982ebeb commit 768c517
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/dsp_tools/commands/xmlupload/models/value_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from typing import Any
from typing import TypeAlias
from typing import Union
from typing import assert_never

InputTypes: TypeAlias = Union[str]
from dsp_tools.commands.xmlupload.models.formatted_text_value import FormattedTextValue
from dsp_tools.models.exceptions import BaseError

InputTypes: TypeAlias = Union[str, FormattedTextValue]
OutputTypes: TypeAlias = Union[str]


Expand All @@ -19,15 +23,17 @@ class ValueTransformer(ABC):
def transform(self, input_value: InputTypes) -> OutputTypes: ...

@staticmethod
def _assert_is_string(value: str) -> str:
def _assert_is_string(value: str | FormattedTextValue) -> str:
match value:
case str() as s:
return s
case FormattedTextValue() as xml:
raise BaseError(f"Expected string value, but got XML value: {xml.as_xml()}")
case _:
assert_never(value)


class DecimalTransformer(ValueTransformer):
def transform(self, input_value: str) -> str:
def transform(self, input_value: Any) -> str:
val = self._assert_is_string(input_value)
return str(float(val))
4 changes: 2 additions & 2 deletions src/dsp_tools/commands/xmlupload/resource_create_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _transform_into_serialise_prop_with_transformer(
prop: XMLProperty,
permissions_lookup: dict[str, Permissions],
serialiser: Callable[[str, str | None, str | None], SerialiseValue],
transformer: Callable[[str], ValueTransformer],
transformer: ValueTransformer,
) -> SerialiseProperty:
serialised_values = [
_transform_into_serialise_value_with_transformer(v, permissions_lookup, serialiser, transformer)
Expand All @@ -493,7 +493,7 @@ def _transform_into_serialise_value_with_transformer(
value: XMLValue,
permissions_lookup: dict[str, Permissions],
serialiser: Callable[[str, str | None, str | None], SerialiseValue],
transformer: Callable[[str], ValueTransformer],
transformer: ValueTransformer,
) -> SerialiseValue:
transformed = transformer.transform(value.value)
permission_str = _get_permission_str(value.permissions, permissions_lookup)
Expand Down

0 comments on commit 768c517

Please sign in to comment.