r3labs / diff

A library for diffing golang structures
Mozilla Public License 2.0
888 stars 80 forks source link

New feature for custom differ - allows custom differs to pass control back to default differs. #29

Closed nehbit closed 4 years ago

nehbit commented 4 years ago

I was trying to create a custom differ for a struct that might have had other structs in it — it seems as it stands that is impossible. This allows custom differs to be used as intermediate steps. Essentially, as it stands, once you go into a custom differ, you can't ever go back — there is no way to pass the control back to the standard differs. This adds another method to the interface that diff.Differ uses to pass the internal diff() function into the custom differ. By calling that internal function, a custom differ can give back control, and diffing can continue normally.

A test case with a recursive struct (my use case) is also added, which uses this feature. In the test case, the custom differ gives back control to the slice differ, and slice differ in turn calls the custom differ again, so as to be able to deal with recursive trees.

Not sure if this works with your design, you might want to review it for your own purposes — there might be a better way of doing this. Mind that it does change how custom differs are delivered in as well, from a concrete value to a pointer.

Lastly, make sure to remove the name change in the go.mod file. I had to push that out so I can use the changes myself, otherwise go get doesn't work. 🙂

purehyperbole commented 4 years ago

Sorry for the (very long) delay in getting this reviewed. Thanks for submitting this PR, looks like a good edition to the feature!