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

Add Replacement element for conditional value replacements #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

karkowg
Copy link

@karkowg karkowg commented Jan 17, 2021

I found myself in a scenario where I only wanted to replace column values if a condition was met (e.g. replace email column value only if domain is not 'example.com').

So I hacked together a Replacement element that you can use as child of a Column element. Example:

<?xml version="1.0" ?>
<column name="test" dump="replace">
    <replacement value="updated"/>
</column>

For testing the column value against a condition, the "strategy" and "constraint" attributes are available. In the example below, columns with value "baby yoda" would be replaced by "grogu":

<?xml version="1.0" ?>
<column name="test" dump="replace">
    <replacement strategy="eq" constraint="baby yoda" value="grogu"/>
</column>

At the moment, 3 comparison strategies are defined: "eq" (equals), "neq" (not equals), and "regex".

A Column can have many Replacement elements. The first one where the column value matches the constraint defines the replacement value. In the example below, "pattern" would be replaced by "A", "something" by "B" and all other values by "fallback".

<?xml version="1.0" ?>
<column name="test" dump="replace">
    <replacement strategy="regex" constraint="/^pattern$/" value="A"/>
    <replacement strategy="eq" constraint="something" value="B"/>
    <replacement value="fallback"/>
</column>

A Column with "replacement" attribute still works like it used to and it overrides any Replacement elements.

I am not sure about the API (Replacement elements with strategy and constraint attributes) nor if more people will find this useful, but it did the trick for me and I decided to share it here. Please let me know what you think.

Best.

@yuvi3005

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants