-
Notifications
You must be signed in to change notification settings - Fork 1
Allow key value based transformers with custom replacement function #10
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great addition @dfangl, thank you! Love the flexibility - example (and maybe a typical use` is for length, but your implementation allows for a more complex use cases as well. 👍
I suggested a test enhancement in a separate PR, would love to know your thoughts.
def key_value_replacement_function( | ||
key: str, | ||
replacement_function: Callable[[str, Any], str] = None, | ||
reference_replacement: bool = True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: so far I've seen reference_replacement
as not too intuitive when defaulted to True
, especially because it can change anything in the snapshot, including keys. Many times it goes like this: adding key_value
transformer, see that comparison breaks, set reference_replacement
to False
.
Maybe we shouldn't set a default at all to this new enhanced function so that with each use the user thinks about whether they need a reference replacement. I see a drawback that the behavior would then differ from existing transformers, but also have an opinion that the default makes more harm than good.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I made a different observation - usually we want to have reference replacements where possible. We just have to avoid reference replacing values in predefined keys - usually this happens if the transformer matches too much, as random data is not usually in our snapshot keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks for sharing!. I do see value in reference replacements by default, just not in keys. This is a separate discussion though, happy to leave current behavior for this transformer. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the key replacement, this is more a side effect than anything else. Happy to work together to fix this in the future!
"hello": "<placeholder(5):1>", | ||
"hello2": "again", | ||
"path": { | ||
"to": {"anotherkey": "hi", "<placeholder(6):1>": {"hello": "<placeholder(6):1>"}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: test it with another same-length value to see if we get :2
in replacement correctly. Also for case without reference replacement would be great to test what happens when function gives the same result for two different strings.
I added a PR for that: #11
I'd expect such behavior from reference replacement, don't see a problem here tbh. With kv function transformer you basically explicitly add a pre-processor before the transformer, so you should expect it to give two different replacements for two different function results. Length example can be a bit misleading though, because two different strings can have the same length: I show that in test change in #11. But since the flexibility of a function goes beyond just length I don't see a problem. |
Motivation
Currently our KeyValueBasedTransformer only allows static values for replacement.
However, this limits us in the kind of transformers we can build - for me, I wanted to build a key value based transformer where the length of the value was part of the transformation.
Changes
Open questions