Skip to content

Commit

Permalink
feat: add map_value transmutator
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Oct 29, 2024
1 parent 3fb8b56 commit e1c5e4d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
7 changes: 1 addition & 6 deletions ckanext/transmute/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ def get_replace_from(self) -> Union[list[str], str]:

return self._get_sibling_field_name(self.replace_from)

def _get_sibling_field_name(self, field_name: str) -> Optional[Any]:
field = self.definition["fields"].get(field_name)

if not field:
raise SchemaFieldError(f"Field: sibling field is not exists: {field_name}")

def _get_sibling_field_name(self, field_name: str) -> str:
return field_name


Expand Down
26 changes: 25 additions & 1 deletion ckanext/transmute/transmutators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ckanext.transmute.types import Field

_transmutators: dict[str, Callable[..., Any]] = {}

SENTINEL = object()

def get_transmutators():
return _transmutators
Expand Down Expand Up @@ -288,3 +288,27 @@ def list_mapper(
field.value = result

return field

@transmutator
def map_value(
field: Field,
test_value: Any,
if_same: Any,
if_different: Any = SENTINEL,
) -> Field:
"""Replace value with other value.
Args:
field: Field object
test_value: value that will be compared to field value
if_same: value to use if test_value matches the field value
if_different: value to use if test_value does not matche the field value.
Leave empty to keep original value of the field.
"""
if field.value == test_value:
field.value = if_same

elif if_different is not SENTINEL:
field.value = if_different

return field

0 comments on commit e1c5e4d

Please sign in to comment.