Commit b16d4a24 authored by Kent Murra's avatar Kent Murra Committed by Tao Feng

Check for a valid string on the regex transformer. If not a valid string,...

Check for a valid string on the regex transformer.  If not a valid string, then the value is ignored and the original record returned. (#136)
parent d5a65ee8
......@@ -18,6 +18,8 @@ class RegexStrReplaceTransformer(Transformer):
"""
Generic string replacement transformer using REGEX.
User can pass list of tuples where tuple contains regex and replacement pair.
Any non-string values will be ignored.
"""
def init(self, conf):
# type: (ConfigTree) -> None
......@@ -27,7 +29,12 @@ class RegexStrReplaceTransformer(Transformer):
def transform(self, record):
# type: (Any) -> Any
val = getattr(record, self._attribute_name)
if six.PY2 and isinstance(val, six.text_type):
if val is None or not isinstance(val, six.string_types):
return record
# Encode unicode string
if six.PY2:
val = val.encode('utf-8', 'ignore')
for regex_replace_tuple in self._regex_replace_tuples:
......
......@@ -2,7 +2,7 @@ import os
from setuptools import setup, find_packages
__version__ = '1.4.3'
__version__ = '1.4.4'
requirements_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'requirements.txt')
......
......@@ -9,6 +9,33 @@ class TestRegexReplacement(unittest.TestCase):
def test(self):
# type: () -> None
transformer = self._default_test_transformer()
foo = Foo('abc')
actual = transformer.transform(foo)
self.assertEqual('bba', actual.val)
def test_numeric_val(self):
# type: () -> None
transformer = self._default_test_transformer()
foo = Foo(6)
actual = transformer.transform(foo)
self.assertEqual(6, actual.val)
def test_none_val(self):
# type: () -> None
transformer = self._default_test_transformer()
foo = Foo(None)
actual = transformer.transform(foo)
self.assertEqual(None, actual.val)
def _default_test_transformer(self):
# type: () -> RegexStrReplaceTransformer
config = ConfigFactory.from_dict({
'regex_replace_tuple_list': [('a', 'b'), ('c', 'a')],
'attribute_name': 'val'
......@@ -17,10 +44,7 @@ class TestRegexReplacement(unittest.TestCase):
transformer = RegexStrReplaceTransformer()
transformer.init(config)
foo = Foo('abc')
actual = transformer.transform(foo)
self.assertEqual('bba', actual.val)
return transformer
class Foo(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment