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

Allow hook for compare function + Ignore order #5

Open
flip111 opened this issue Oct 30, 2018 · 2 comments
Open

Allow hook for compare function + Ignore order #5

flip111 opened this issue Oct 30, 2018 · 2 comments

Comments

@flip111
Copy link

flip111 commented Oct 30, 2018

Would be nice if a comparison function could be passed. And also be able to ignore the order of things.

In my program i read in json ["hello", "world"] and ["world", "hello"] this will be picked up as two changes, but i don't really care for the order in the array. Would be nice to be able to set per array if it needs to compare indices (the default) or not.

Another thing is that instead of array of strings i will have an array of objects and one property of an object should be ignored during comparison. [{ignore_this:"timestamp", compare_this: "important data"}, { .. another object ..}]

I think to design this in the library will not be so easy because the passed in function probably needs knowledge of what it is looking at (some path from the root struct) + access to parents + access to children (and therefor implicit access to siblings).

I can not fix this by manipulating the data i have up front. For example sorting the data and removing that one property from an object. Consider this these two arrays (string example, not with object example): ["hello", "world"] and ["hello", "lol", "world"]. The diff is as follows:

Unchanged([Index(0)], String("hello"))
Modified([Index(1)], String("world"), String("lol"))
Added([Index(2)], String("world"))

So i would need index insensitive diff and it would change to:

Unchanged(String("hello"))
Added(String("lol"))
Unchanged(String("world"))

Maybe there are already facilities in the library that have some of the things i asked for. But i'm not sure what to look for.

@Byron
Copy link
Owner

Byron commented Dec 21, 2018

Thanks for the detailed write-up, I believe this makes it easy for others to pick up if there is demand.

Indeed the algorithm itself makes certain assumptions and can't be affected by the passed in delegate.

The only way to fix this would be to have entirely different diff-functions, which I believe you could implement yourself based on your needs.

@flip111
Copy link
Author

flip111 commented Dec 21, 2018

Well i have the suspicion it's not so much the algorithm as it's the chosen data structure. I read here:

https://doc.rust-lang.org/std/collections/struct.BTreeSet.html

It is a logic error for an item to be modified in such a way that the item's ordering relative to any other item, as determined by the Ord trait, changes while it is in the set.

Maybe it's possible to change the diff function to make it generic over a type so people can choose a structure that cares about order or not. I don't know if there is a candidate data structure that doesn't care about order, but i could look or ask around.

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

No branches or pull requests

2 participants