Skip to content

Commit

Permalink
update: update tsm_mapping transmutator
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Feb 29, 2024
1 parent 70bf325 commit 12a54d7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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.
Expand Down
32 changes: 28 additions & 4 deletions ckanext/transmute/tests/test_transmutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand All @@ -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"}

Expand All @@ -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"],
],
},
}
Expand All @@ -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"
11 changes: 5 additions & 6 deletions ckanext/transmute/transmutators.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 12a54d7

Please sign in to comment.