Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a TextTransformer to replace strings with special chars #6

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions localstack_snapshot/snapshots/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,19 @@ def _transform_dict(self, input_data: dict, ctx: TransformContext = None) -> dic

def _transform_list(self, input_data: list, ctx: TransformContext = None) -> list:
return [self._transform(e, ctx=ctx) for e in input_data]


class TextTransformer:
def __init__(self, text: str, replacement: str):
self.text = text
self.replacement = replacement

def transform(self, input_data: dict, *, ctx: TransformContext) -> dict:
def replace_val(s):
return s.replace(self.text, self.replacement)

ctx.register_serialized_replacement(replace_val)
SNAPSHOT_LOGGER.debug(
f"Registering text pattern '{self.text}' in snapshot with '{self.replacement}'"
)
return input_data
14 changes: 14 additions & 0 deletions localstack_snapshot/snapshots/transformer_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
JsonpathTransformer,
KeyValueBasedTransformer,
RegexTransformer,
TextTransformer,
)


Expand Down Expand Up @@ -67,3 +68,16 @@ def regex(regex: str | Pattern[str], replacement: str):
:return: RegexTransformer
"""
return RegexTransformer(regex, replacement)

@staticmethod
def text(text: str, replacement: str):
"""Creates a new TextTransformer. All occurrences in the string-converted dict will be replaced.
Useful if the text contains special characters that would confuse the RegexTransformer, like '+' or '('.
:param text: the text that should be replaced
:param replacement: the value which will replace the original value.
:return: TextTransformer
"""
return TextTransformer(text, replacement)
25 changes: 25 additions & 0 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ def test_nested_sorting_transformer(self):
output = transformer.transform(input, ctx=ctx)
assert output == expected

@pytest.mark.parametrize(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test cases 👏

"value",
[
"a+b",
"question?",
"amount: $4.00",
"emoji: ^^",
"sentence.",
"others (like so)",
"special {char}",
],
)
def test_text(self, value):
input = {"key": f"some {value} with more text"}

expected = {"key": "some <value> with more text"}

transformer = TransformerUtility.text(value, "<value>")

ctx = TransformContext()
output = transformer.transform(json.dumps(input), ctx=ctx)
for sr in ctx.serialized_replacements:
output = sr(output)
assert json.loads(output) == expected


class TestTimestampTransformer:
def test_generic_timestamp_transformer(self):
Expand Down
Loading