Skip to content

Commit

Permalink
Replace optional_str() with converter chain
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Oct 22, 2017
1 parent fc80f2f commit 793df2e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
43 changes: 43 additions & 0 deletions netsgiro/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,46 @@ def value_or_none_converter(value):
return converter(value)

return value_or_none_converter


def truthy_or_none(converter):
"""Converter that returns a truthy value or ``None``.
``converter`` is called to further convert non-``None`` values.
"""

def truthy_or_none_converter(value):
if not value:
return None
return converter(value)

return truthy_or_none_converter


def stripped_spaces_around(converter):
"""Converter that returns a string stripped of leading and trailing spaces or
``None`` if the value isn't .
``converter`` is called to further convert non-``None`` values.
"""

def stripped_text_converter(value):
if value is None:
return None
return converter(value.strip())

return stripped_text_converter


def stripped_newlines(converter):
"""Converter that returns a string stripped of newlines or ``None``.
``converter`` is called to further convert non-``None`` values.
"""

def single_line_converter(value):
if value is None:
return None
return converter(value.replace('\r', '').replace('\n', ''))

return single_line_converter
31 changes: 16 additions & 15 deletions netsgiro/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from attr.validators import instance_of, optional

import netsgiro
from netsgiro.converters import value_or_none
from netsgiro.converters import (
stripped_newlines,
stripped_spaces_around,
truthy_or_none,
value_or_none,
)
from netsgiro.validators import str_of_length, str_of_max_length


Expand Down Expand Up @@ -68,15 +73,8 @@ def to_bool(value: Union[bool, str]) -> bool:
raise ValueError("Expected 'J' or 'N', got {!r}".format(value))


def optional_str(value: Optional[str]) -> Optional[str]:
value = value and value.strip()
if not value:
return None
return (
value
.replace('\r', '')
.replace('\n', '')
)
to_safe_str_or_none = value_or_none(
stripped_newlines(stripped_spaces_around(truthy_or_none(str))))


@attr.s
Expand Down Expand Up @@ -394,7 +392,8 @@ class TransactionAmountItem1(TransactionRecord):
nets_date = attr.ib(convert=to_date)
amount = attr.ib(convert=int)
kid = attr.ib(
convert=optional_str, validator=optional(str_of_max_length(25)))
convert=to_safe_str_or_none,
validator=optional(str_of_max_length(25)))

# Only OCR Giro
centre_id = attr.ib(default=None, validator=optional(str_of_length(2)))
Expand Down Expand Up @@ -484,7 +483,7 @@ class TransactionAmountItem2(TransactionRecord):
"""

# TODO Validate `reference` length, which depends on service code
reference = attr.ib(convert=optional_str)
reference = attr.ib(convert=to_safe_str_or_none)

# Only OCR Giro
form_number = attr.ib(default=None, validator=optional(str_of_length(10)))
Expand All @@ -496,7 +495,7 @@ class TransactionAmountItem2(TransactionRecord):
_filler = attr.ib(default=None)

# Only AvtaleGiro
payer_name = attr.ib(default=None, convert=optional_str)
payer_name = attr.ib(default=None, convert=to_safe_str_or_none)

RECORD_TYPE = netsgiro.RecordType.TRANSACTION_AMOUNT_ITEM_2
_PATTERNS = [
Expand Down Expand Up @@ -579,7 +578,8 @@ class TransactionAmountItem3(TransactionRecord):
"""

text = attr.ib(
convert=optional_str, validator=optional(str_of_max_length(40)))
convert=to_safe_str_or_none,
validator=optional(str_of_max_length(40)))

RECORD_TYPE = netsgiro.RecordType.TRANSACTION_AMOUNT_ITEM_3
_PATTERNS = [
Expand Down Expand Up @@ -734,7 +734,8 @@ class AvtaleGiroAgreement(TransactionRecord):

registration_type = attr.ib(convert=to_avtalegiro_registration_type)
kid = attr.ib(
convert=optional_str, validator=optional(str_of_max_length(25)))
convert=to_safe_str_or_none,
validator=optional(str_of_max_length(25)))
notify = attr.ib(convert=to_bool)

RECORD_TYPE = netsgiro.RecordType.TRANSACTION_AGREEMENTS
Expand Down

0 comments on commit 793df2e

Please sign in to comment.