r3labs / diff

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

修复切片数据索引位置交换导致的bug #44

Closed robbinhan closed 3 years ago

robbinhan commented 3 years ago
[]string{"web.business.image/202009165d0d6aeb03ec6ecb494897d8", "web.business.image/202009045d0d1788b05942ca42988dca"}

[]string{"web.business.image/202009045d0d1788b05942ca42988dca", "web.business.image/202009165d0d6aeb03ec6ecb494897d8"}

fix index exchange diff error

no index will output type :

from :web.business.image/202009165d0d6aeb03ec6ecb494897d8,to:<nil>,path: []string{"xxx", "xxx", "1"},type:delete
from :<nil>,to:web.business.image/202009045d0d25b79ba5011f438dafb7,path: []string{"xxx", "xxx", "0"},type:create
purehyperbole commented 3 years ago

Hi @robbinhan , thanks for submitting this contribution!

Tests are failing for your PR as this changes the behaviour of diffing slices for all use cases.

I'm not sure if that change is actually necessary, as there is already diff.SliceOrdering(), which should produce more or less the same result?

func main() {
    a := []string{"web.business.image/202009165d0d6aeb03ec6ecb494897d8", "web.business.image/202009045d0d1788b05942ca42988dca"}

    b := []string{"web.business.image/202009045d0d1788b05942ca42988dca", "web.business.image/202009165d0d6aeb03ec6ecb494897d8"}

    cl, _ := diff.Diff(a, b)

    // produces '[]'
    fmt.Println(cl)

    cl, _ = diff.Diff(a, b, diff.SliceOrdering(true))

    // produces: '[{update [0] web.business.image/202009165d0d6aeb03ec6ecb494897d8 web.business.image/202009045d0d1788b05942ca42988dca <nil>} {update [1] web.business.image/202009045d0d1788b05942ca42988dca web.business.image/202009165d0d6aeb03ec6ecb494897d8 <nil>}]'
    fmt.Println(cl)
}
robbinhan commented 3 years ago

ok,I will try again