wI2L/jsondiff

[feature request]: Show old value for replace operations

tjdavis3 opened this issue · 1 comments

Factorize() only shows from on move and copy operations. In our use case we are not generating the diffs for use with a PATCH but to show changes. We'd like to be able to generate the diff so that it shows both the old and new values of the changed fields.

We tried using Factorize() but that doesn't change the output for replace operations. It appears the append method explicitly sets the from value to emptyPtr. It looks like there may be a few other places that changes would be needed to set the old value.

Ideally we'd like to see a replace able to be renders as

{"op":"replace","from":"oldValue","path":"/key","value":"newValue"}
wI2L commented

The from location is specified in the spec for move and copy operations only. For the other type of operations, there is no notion of an "old location". A location is not a value, but the path that points to the previous value, and it's applicable to only the two operations mentionned before.

I think you are perhaps confusing that field with OldValue field, which sounds like it's what you're interested in. See the differ.replace method. It does pass the "old value" for a replace operation. However, since the rfc6902 doesn't mention it, the field is not marshaled by default if you use encoding/json.Marshal (because of json:"-"), but it's present and you can consume it to show the difference of values.