From 814aa4f92eeb58b971d3057355e52085dde719f0 Mon Sep 17 00:00:00 2001 From: Quentin Muller Date: Mon, 20 Nov 2023 16:04:17 +0000 Subject: [PATCH] :sparkles: Add optional option --- magicparse/fields.py | 11 +++++++---- tests/test_fields.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/magicparse/fields.py b/magicparse/fields.py index cc0aa14..9a3ab89 100644 --- a/magicparse/fields.py +++ b/magicparse/fields.py @@ -18,12 +18,18 @@ def __init__(self, options: dict) -> None: PostProcessor.build(item) for item in options.get("post-processors", []) ] + self.optional = False + if options.get("optional", False): + self.optional = True if options["optional"] else False + self.transforms = ( pre_processors + [type_converter] + validators + post_processors ) def _process_raw_value(self, raw_value: str): value = raw_value + if self.optional and not raw_value: + return None for transform in self.transforms: value = transform.apply(value) return value @@ -34,10 +40,7 @@ def _read_raw_value(self, row) -> str: def read_value(self, row): raw_value = self._read_raw_value(row) - value = raw_value - for transform in self.transforms: - value = transform.apply(value) - return value + return self._process_raw_value(raw_value) @abstractmethod def error(self, exception: Exception): diff --git a/tests/test_fields.py b/tests/test_fields.py index e1f09b1..14d7185 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -86,3 +86,33 @@ def test_columnar_error_format(): "error": "value 'hello' is not a valid decimal", "field-key": "ratio", } + + +def test_optional_field(): + field = DummyField( + { + "key": "ratio", + "type": "decimal", + "optional": True, + "pre-processors": [ + { + "name": "replace", + "parameters": {"pattern": "XXX", "replacement": "000"}, + } + ], + "post-processors": [{"name": "divide", "parameters": {"denominator": 100}}], + } + ) + assert field.read_value("XXX150") == Decimal("1.50") + assert field.read_value("") is None + + +def test_non_optional_field(): + field = DummyField( + { + "key": "ratio", + "type": "decimal", + "optional": False, + } + ) + assert field.read_value("1.5") == Decimal("1.50")