Add set_if_not_equals_and to ActiveValue
#2888
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Info
I did not start a separate issue for this change since I knew it would be easy enough to quickly implement myself. It fixes an issue I'm running into at work where I have a "base"
ActiveModelcreated from one stream of data (in my case, weather data from a virtual provider, which are essentially just estimates) needs to be merged with anActiveModelcreated from a second stream (in my case, weather data from a physical station that is more accurate but also drops data much more often). TheActiveValuesI'm working with wrap anOption<T>. The "base"ActiveModelvalue is always set, acting as a sort of fall-back, and I want to replace it's values with those from the newActiveModelonly if those new values areSome. Currently, this has to be done usingif letblocks, which gets really verbose when doing this patching over and over again. This PR makes it a lot more ergonomic, introducingActiveValue::set_if_not_equals_and, as a sort of mixed analog ofOption::is_some_andandOption::map_or. It works like this:instead of
which saves an immense amount of visual noise when doing this operation repeatedly. I imagine it has many other use cases other than the one I made it for, and in any case this change brings the
sea-ormmore into alignment with the APIs available on the wrapper enums from the standard library.Breaking Changes
No breaking changes to existing APIs, only additions.
Changes
Addition of
pub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)toActiveValue