diff --git a/README.md b/README.md index 4841e3c..d3c65df 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ schema = ... ``` - `tsm_concat` - Trim string with max lenght. Use `$self` to point on field value. Example: ``` -data = {"id": "dataset-1} +data = {"id": "dataset-1"} schema = ... "package_url": { @@ -198,7 +198,25 @@ There is a possibility to provide more arguments to a validator like in `tsm_get - `tsm_mapper` - Map current value to the mapping dict -Map a value to another value. The current value must serve as a key within the mapping dictionary, while the new value will represent the updated value +Map a value to another value. The current value must serve as a key within the mapping dictionary, while the new value will represent the updated value. + +The default value to be used when the key is not found in the mapping. If the default value is not provided, the current value will be used as it. + +``` +data = {"language": "English"} + +schema = ... + "language": { + "validators": [ + [ + "tsm_mapper", + {"English": "eng"}, + "English", + ] + ], + }, + ... +``` ### Keywords 1. `map_to` (`str`) - changes the `field.name` in result dict. diff --git a/ckanext/transmute/tests/test_transmutators.py b/ckanext/transmute/tests/test_transmutators.py index e298a17..00994e3 100644 --- a/ckanext/transmute/tests/test_transmutators.py +++ b/ckanext/transmute/tests/test_transmutators.py @@ -368,6 +368,9 @@ def test_default_allows_falsy_values(self, default): assert result == {"field_name": default} + +@pytest.mark.ckan_config("ckan.plugins", "scheming_datasets") +class TestMapperTransmutator: def test_mapper_with_mapped_value(self): data: dict[str, Any] = {"language": "eng"} @@ -390,7 +393,6 @@ def test_mapper_with_mapped_value(self): assert result["language"] == "English" - def test_mapper_without_mapped_value(self): data: dict[str, Any] = {"language": "ua"} @@ -413,14 +415,14 @@ def test_mapper_without_mapped_value(self): assert result["language"] == "Spanish" - def test_mapper_without_default(self): + def test_mapper_without_mapping(self): data: dict[str, Any] = {"language": "ua"} tsm_schema = build_schema( { "language": { "validators": [ - ["tsm_mapper", {"eng": "English"}], + ["tsm_mapper"], ], }, } @@ -434,4 +436,26 @@ def test_mapper_without_default(self): root="Dataset", ) - assert e.value.error == "mapper() missing 1 required positional argument: 'default'" + assert e.value.error == "Arguments for validator weren't provided" + + def test_mapper_without_default(self): + data: dict[str, Any] = {"language": "ua"} + + tsm_schema = build_schema( + { + "language": { + "validators": [ + ["tsm_mapper", {"eng": "English"}], + ], + }, + } + ) + + result = call_action( + "tsm_transmute", + data=data, + schema=tsm_schema, + root="Dataset", + ) + + assert result["language"] == "ua" diff --git a/ckanext/transmute/transmutators.py b/ckanext/transmute/transmutators.py index fbe7555..d517d5e 100644 --- a/ckanext/transmute/transmutators.py +++ b/ckanext/transmute/transmutators.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import Callable, Any +from typing import Callable, Any, Optional from datetime import datetime from dateutil.parser import parse, ParserError @@ -219,21 +219,20 @@ def unique_only(field: Field) -> Field: @transmutator -def mapper(field: Field, mapping: dict[Any, Any], default: Any) -> Field: +def mapper(field: Field, mapping: dict[Any, Any], default: Optional[Any] = None) -> Field: """Map a value with a new value. The initial value must serve as a key within - a mapping dictionary, while the dic value will represent the updated value. - - In cases where the key does not exist within the mapping, a default value will be employed. + a mapping dictionary, while the dict value will represent the updated value. Args: field (Field): Field object mapping (dict[Any, Any]): A dictionary representing the mapping of values. default (Any): The default value to be used when the key is not found in the mapping. + If the default value is not provided, the current value will be used as it. Returns: Field: the same Field with new value """ - new_value = mapping.get(field.value, default) + new_value = mapping.get(field.value, default or field.value) field.value = new_value