Skip to content

Commit

Permalink
✨ Add optional option
Browse files Browse the repository at this point in the history
  • Loading branch information
qmullersmartway committed Nov 20, 2023
1 parent c6f4318 commit 814aa4f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
11 changes: 7 additions & 4 deletions magicparse/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 814aa4f

Please sign in to comment.