yudai / gojsondiff

Go JSON Diff
Other
535 stars 81 forks source link

Confusing Array Diff Output #24

Open blaskovicz opened 7 years ago

blaskovicz commented 7 years ago

Given two arrays of objects, if the objects appear in a different order, reflect.DeepEqual(expected, actual) will return false, whereas differ.CompareArrays(expected, actual) will contain an unexpected diff.

Reproducer:

package main

import (
  "fmt"

  "github.com/yudai/gojsondiff"
  "github.com/yudai/gojsondiff/formatter"
)

func main() {
  a := map[string]interface{}{"key": "bobby", "val": "tables", "foo": false}
  b := map[string]interface{}{"key": "how", "val": "outage", "foo": true}
  c := map[string]interface{}{"key": "stuffs", "val": "sample", "foo": true}

  expected := []interface{}{a, b, c}
  //actual := []interface{}{a, b, c}
  actual := []interface{}{b, c, a}

  differ := gojsondiff.New()
  diff := differ.CompareArrays(expected, actual)
  //diff := differ.CompareObjects(map[string]interface{}{"_": expected}, map[string]interface{}{"_": actual})
  //formatter := formatter.NewDeltaFormatter()
  formatter := formatter.NewAsciiFormatter(expected, formatter.AsciiFormatterConfig{ShowArrayIndex: true})
  str, err := formatter.Format(diff)

  if err != nil {
    panic(err)
  }
  fmt.Printf("%s\n", str)
}

Diff Results:

$ go run diff.go
 [
   0: {
     "foo": false,
     "key": "bobby",
     "val": "tables"
   },
   1: {
     "foo": true,
     "key": "how",
     "val": "outage"
   },
 ]

Diff Results from jsondiff.com: image

yudai commented 7 years ago

@blaskovicz Thank you for the report.

It looks a bug. I'll take a look more when I have time.