Support replace operations with value of null
kingtaj opened this issue · 7 comments
When setting a json field to null, the patch produces a replace operation like this:
{"op":"replace","path":"/test_int_ptr"}
with no value
field.
Is there perhaps a configuration option to produce an operation with null/nil instead?
{"op":"replace","path":"/test_int_ptr",value:null}
Thank you.
This is not intended, the value
field should reflect what has changed.
Unfortunately, the field has the struct field tag omitempty
: https://github.com/wI2L/jsondiff/blob/master/operation.go#L37, which causes its omission from the marshaled operation, bummer.
This requires some kind of "magic" to know if the field is nil
because its not an operation that uses the field value
(a remove
op for example), or because its the actual JSON null
value. I have an idea for that, I'll push a fix ASAP.
Thanks for reporting the issue.
Thanks for the quick turnaround! Will test once the change is merged.
@kingtaj I just realised that there's one case where the current implementation will fail to met the expected output once an operation is marshaled to JSON; when the value (which has type interface{}
) is not nil
but a typed-nil value (nil
vs (*int)(nil)
for example).
I'll amend with a generic check that will pass cases such as (type=*int,value=nil)
.
thanks, good catch. I'm interested to see how you do this since the underlying type is unknown
@kingtaj Updated the PR with a new commit, if you want to check it out!
I'm using unsafe
to avoid reflect
. One could check if reflect.ValueOf(o.Value).IsNil()
is true
, but that would require to ensure that the underlying type of o.Value
is nill-able (map, slice, pointer or func/chan -- even tho the last two can be excluded since not supported by encoding/json
).
👍 Nice! Yeah I didn't think you'd want to use reflect
if it could be avoided.
@kingtaj Released with v0.3.0
. Thanks again for reporting the issue. The playground has been updated accordingly.