-
Notifications
You must be signed in to change notification settings - Fork 88
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
Changelog for slices inconsistent when using pointer values #59
Comments
Hi @chbiel, thanks for raising the issue and sorry for the late response! There a few things to note here about the behaviour of Firstly, if you compare a pointer to a concrete type to This is expected behaviour as we cannot infer any type information from a As the library is unlikely to support this reflection due to the addition complexity, you may be better off not comparing to nil, but rather a empty, but instantiated value like so: var emptyData = TestData{&TestStruct{}}
func diff(a, b *TestData) {
if a == nil {
a = &emptyData{}
}
if b == nil {
b = &emptyData{}
}
changelog, _ := diff.Diff(a, b)
} Which when one of the values is nil, will produce: [
{
"type": "delete",
"path": [
"TS",
"Slice",
"0"
],
"from": "1",
"to": null
},
{
"type": "delete",
"path": [
"TS",
"Slice",
"1"
],
"from": "4",
"to": null
},
{
"type": "delete",
"path": [
"TS",
"Slice",
"2"
],
"from": "3",
"to": null
}
] As for your second point, when comparing values in a slice, the reason you see changes to an item as an Just a note that the option you are using in your example, s1 := TestData{&TestStruct{Slice: []string{"1", "2", "3"}}}
s2 := TestData{&TestStruct{Slice: []string{"3", "2", "1"}}}
d, err := diff.NewDiffer(diff.SliceOrdering(false))
if err != nil {
panic(err)
}
// produces an empty changelog
changes, _ := d.Diff(&s1, &s2) |
We want to compare slices of strings.
Following example code:
With this example there are three different Types of changes visible:
changes
contains acreate
changes2
contains aupdate
changes3
contains adelete
The
create
anddelete
take the whole interface as the From/To values. So thepath
isTS
.The
update
on the other side lists all individual changes by index and thepath
isTS, Slice, 1
.My expectation would be, that the lists of changes are consistent, what means
create
anddelete
also go down to the index levelupdate
also shows the whole slice diffmaybe an option to make on or the other behaviour optional, would be the best case.
Do I miss something here or is this intended behaviour?
My previous concern is still value:
with the above example, the
update
(examplechanges2
) has one record in the changes, that sayswhat actually (from my perspective) is wrong, as here we have an
create
anddelete
of values, and not anupdate
.Note: when changing the type of
TestData.TS
to a value and remove the pointer, the diff is consistent and all items are listed with changes based on indices.I personally prefer the diff as a whole, like given in
create
anddelete
.EDIT: after some investigation, more info added
The text was updated successfully, but these errors were encountered: