slimdump icon indicating copy to clipboard operation
slimdump copied to clipboard

Add Replacement element for conditional value replacements

Open karkowg opened this issue 4 years ago • 1 comments

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.

karkowg avatar Jan 17 '21 23:01 karkowg

make sure to do this

yuvi3005 avatar Dec 31 '21 11:12 yuvi3005