Skip to content

Commit

Permalink
add to function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Nov 15, 2024
1 parent 3f5b5d0 commit 982ebeb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/dsp_tools/commands/xmlupload/models/value_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from typing import Union
from typing import assert_never

from dsp_tools.utils.date_util import Date

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


@dataclass(frozen=True)
class ValueTransformer(ABC):
"""Class used to transform an input value."""

@abstractmethod
def transform(self, input_value: InputTypes) -> OutputTypes: ...

Expand Down
24 changes: 15 additions & 9 deletions src/dsp_tools/commands/xmlupload/resource_create_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]:
"uri": SerialiseURI,
}

value_with_transformer_mapper = {"decimal": (SerialiseDecimal, DecimalTransformer)}
value_with_transformer_mapper = {
"decimal": (SerialiseDecimal, DecimalTransformer()),
}

for prop in resource.properties:
match prop.valtype:
Expand All @@ -168,12 +170,16 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]:
transformed_prop = _transform_into_serialise_prop(
prop=prop,
permissions_lookup=self.permissions_lookup,
seraliser=str_value_to_serialiser_mapper[val_type],
serialiser=str_value_to_serialiser_mapper[val_type],
)
properties_serialised.update(transformed_prop.serialise())
case "decimal":
transformed_prop = _transform_into_decimal_prop(
prop=prop, permissions_lookup=self.permissions_lookup
case "decimal" as val_type:
serialiser, transformer = value_with_transformer_mapper[val_type]
transformed_prop = _transform_into_serialise_prop_with_transformer(
prop=prop,
permissions_lookup=self.permissions_lookup,
serialiser=serialiser,
transformer=transformer,
)
properties_serialised.update(transformed_prop.serialise())
case "geometry":
Expand Down Expand Up @@ -469,11 +475,11 @@ def _transform_into_richtext_value(
def _transform_into_serialise_prop_with_transformer(
prop: XMLProperty,
permissions_lookup: dict[str, Permissions],
seraliser: Callable[[str, str | None, str | None], SerialiseValue],
serialiser: Callable[[str, str | None, str | None], SerialiseValue],
transformer: Callable[[str], ValueTransformer],
) -> SerialiseProperty:
serialised_values = [
_transform_into_serialise_value_with_transformer(v, permissions_lookup, seraliser, transformer)
_transform_into_serialise_value_with_transformer(v, permissions_lookup, serialiser, transformer)
for v in prop.values
]
prop_serialise = SerialiseProperty(
Expand All @@ -497,9 +503,9 @@ def _transform_into_serialise_value_with_transformer(
def _transform_into_serialise_prop(
prop: XMLProperty,
permissions_lookup: dict[str, Permissions],
seraliser: Callable[[str, str | None, str | None], SerialiseValue],
serialiser: Callable[[str, str | None, str | None], SerialiseValue],
) -> SerialiseProperty:
serialised_values = [_transform_into_serialise_value(v, permissions_lookup, seraliser) for v in prop.values]
serialised_values = [_transform_into_serialise_value(v, permissions_lookup, serialiser) for v in prop.values]
prop_serialise = SerialiseProperty(
property_name=prop.name,
values=serialised_values,
Expand Down

0 comments on commit 982ebeb

Please sign in to comment.