diff --git a/ckanext/transmute/schema.py b/ckanext/transmute/schema.py index 1ed38fa..3c9372c 100644 --- a/ckanext/transmute/schema.py +++ b/ckanext/transmute/schema.py @@ -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 diff --git a/ckanext/transmute/transmutators.py b/ckanext/transmute/transmutators.py index 04b66c2..5475637 100644 --- a/ckanext/transmute/transmutators.py +++ b/ckanext/transmute/transmutators.py @@ -10,7 +10,7 @@ from ckanext.transmute.types import Field _transmutators: dict[str, Callable[..., Any]] = {} - +SENTINEL = object() def get_transmutators(): return _transmutators @@ -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